Advertisement
pleasedontcode

"Sensor Readings" rev_02

Feb 15th, 2025
85
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 Readings"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-15 20:57:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project shall continuously read data from */
  21.     /* connected sensors and actuate outputs based on */
  22.     /* predefined thresholds, ensuring real-time */
  23.     /* responsiveness to environmental changes. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t myDHT22_DHT22_DOUT_PIN_D2 = 2;
  37.  
  38. /***** DHT SENSOR CONFIGURATION *****/
  39. #define DHTTYPE DHT22   // DHT 22 (AM2302)
  40. DHT dht(myDHT22_DHT22_DOUT_PIN_D2, DHTTYPE); // Create DHT object
  41.  
  42. void setup(void)
  43. {
  44.     // put your setup code here, to run once:
  45.     pinMode(myDHT22_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
  46.     dht.begin(); // Initialize the DHT sensor
  47. }
  48.  
  49. void loop(void)
  50. {
  51.     // put your main code here, to run repeatedly:
  52.     delay(2000); // Wait a few seconds between measurements
  53.  
  54.     // Read humidity and temperature
  55.     float humidity = dht.readHumidity();
  56.     float temperature = dht.readTemperature();
  57.  
  58.     // Check if any reads failed and exit early (to try again).
  59.     if (isnan(humidity) || isnan(temperature)) {
  60.         Serial.println("Failed to read from DHT sensor!");
  61.         return;
  62.     }
  63.  
  64.     // Print the results to the Serial Monitor
  65.     Serial.print("Humidity: ");
  66.     Serial.print(humidity);
  67.     Serial.print(" %\t");
  68.     Serial.print("Temperature: ");
  69.     Serial.print(temperature);
  70.     Serial.println(" *C");
  71.  
  72.     // Here you can add conditions to actuate outputs based on thresholds
  73.     // For example:
  74.     if (temperature > 30) {
  75.         // Code to actuate a cooling system
  76.     }
  77.     else if (temperature < 15) {
  78.         // Code to actuate a heating system
  79.     }
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement