CCCPaste

"Sensor Monitoring" rev_02

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Sensor Monitoring"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-21 14:44:49

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onButtonPressed();

/****** CONSTANTS AND GLOBAL VARIABLES *****/
const int buttonPin = 2; // Pin where the button is connected
const int dhtPin = 3;    // Pin where the DHT sensor is connected
const int dhtType = DHT11; // DHT sensor type (DHT11, DHT22, etc.)

EasyButton button(buttonPin); // Create an EasyButton object
DHT dht(dhtPin, dhtType);     // Create a DHT object

void setup(void)
{
    // Initialize serial communication
    Serial.begin(9600);

    // Initialize the button
    button.begin();
    button.onPressed(onButtonPressed); // Attach the callback function

    // Initialize the DHT sensor
    dht.begin();
}

void loop(void)
{
    // Continuously update the button state
    button.read();

    // Read temperature and humidity
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    // Check if any reads failed and exit early (to try again).
    if (isnan(temperature) || isnan(humidity)) {
        Serial.println(F("Failed to read from DHT sensor!"));
        return;
    }

    // Print the results to the Serial Monitor
    Serial.print(F("Humidity: "));
    Serial.print(humidity);
    Serial.print(F("%  Temperature: "));
    Serial.print(temperature);
    Serial.println(F("°C"));

    // Wait a few seconds between measurements.
    delay(2000);
}

void onButtonPressed()
{
    // Action to perform when the button is pressed
    Serial.println(F("Button was pressed!"));
}

/* END CODE */