Advertisement
pleasedontcode

"Sensor Monitoring" rev_02

Jun 21st, 2024
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Sensor Monitoring"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-21 14:44:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** END SYSTEM REQUIREMENTS *****/
  20.  
  21. /****** DEFINITION OF LIBRARIES *****/
  22. #include <EasyButton.h>
  23. #include <DHT.h>
  24.  
  25. /****** FUNCTION PROTOTYPES *****/
  26. void setup(void);
  27. void loop(void);
  28. void onButtonPressed();
  29.  
  30. /****** CONSTANTS AND GLOBAL VARIABLES *****/
  31. const int buttonPin = 2; // Pin where the button is connected
  32. const int dhtPin = 3;    // Pin where the DHT sensor is connected
  33. const int dhtType = DHT11; // DHT sensor type (DHT11, DHT22, etc.)
  34.  
  35. EasyButton button(buttonPin); // Create an EasyButton object
  36. DHT dht(dhtPin, dhtType);     // Create a DHT object
  37.  
  38. void setup(void)
  39. {
  40.     // Initialize serial communication
  41.     Serial.begin(9600);
  42.  
  43.     // Initialize the button
  44.     button.begin();
  45.     button.onPressed(onButtonPressed); // Attach the callback function
  46.  
  47.     // Initialize the DHT sensor
  48.     dht.begin();
  49. }
  50.  
  51. void loop(void)
  52. {
  53.     // Continuously update the button state
  54.     button.read();
  55.  
  56.     // Read temperature and humidity
  57.     float temperature = dht.readTemperature();
  58.     float humidity = dht.readHumidity();
  59.  
  60.     // Check if any reads failed and exit early (to try again).
  61.     if (isnan(temperature) || isnan(humidity)) {
  62.         Serial.println(F("Failed to read from DHT sensor!"));
  63.         return;
  64.     }
  65.  
  66.     // Print the results to the Serial Monitor
  67.     Serial.print(F("Humidity: "));
  68.     Serial.print(humidity);
  69.     Serial.print(F("%  Temperature: "));
  70.     Serial.print(temperature);
  71.     Serial.println(F("°C"));
  72.  
  73.     // Wait a few seconds between measurements.
  74.     delay(2000);
  75. }
  76.  
  77. void onButtonPressed()
  78. {
  79.     // Action to perform when the button is pressed
  80.     Serial.println(F("Button was pressed!"));
  81. }
  82.  
  83. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement