Advertisement
pleasedontcode

**Temperature Control** rev_01

Feb 7th, 2025
76
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: **Temperature Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-07 10:07:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* turn on a relay on temperature 20 and turn off */
  21.     /* relay with temperature 60 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DHT.h>
  28. #include <Relay.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. // DHT Sensor setup
  35. #define DHTPIN 2          // Pin where the DHT sensor is connected
  36. #define DHTTYPE DHT11     // DHT 11
  37. DHT dht(DHTPIN, DHTTYPE);  // Create an instance of the DHT class
  38.  
  39. // Relay setup
  40. #define RELAY_PIN 3       // Pin where the relay is connected
  41. Relay relay(RELAY_PIN, 1); // Create an instance of the Relay class
  42.  
  43. void setup(void)
  44. {
  45.     // Initialize the DHT sensor
  46.     dht.begin();
  47.    
  48.     // Initialize the relay
  49.     relay.setRelayMode(relayModeAutomatic);
  50. }
  51.  
  52. void loop(void)
  53. {
  54.     // Read temperature as Celsius
  55.     float temperature = dht.readTemperature();
  56.  
  57.     // Check if the reading is valid
  58.     if (isnan(temperature)) {
  59.         Serial.println("Failed to read from DHT sensor!");
  60.         return;
  61.     }
  62.  
  63.     // Control the relay based on temperature
  64.     if (temperature >= 20 && temperature < 60) {
  65.         relay.setRelayPosition(relayPositionClosed); // Turn on the relay
  66.     } else if (temperature >= 60) {
  67.         relay.setRelayPosition(relayPositionOpen);   // Turn off the relay
  68.     }
  69.  
  70.     // Add a delay before the next loop iteration
  71.     delay(2000); // Wait for 2 seconds before the next reading
  72. }
  73.  
  74. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement