Advertisement
pleasedontcode

**Temperature Control** rev_02

Feb 7th, 2025
86
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:24:02
  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. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* Activate a relay when the temperature reaches 20°C */
  24.     /* and deactivate it at 60°C, ensuring precise */
  25.     /* temperature monitoring and control for optimal */
  26.     /* performance. Utilize compatible temperature */
  27.     /* sensors and relay modules for effective operation. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /* START CODE */
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <DHT.h> // Include the DHT library for temperature and humidity sensor
  34. #include <Relay.h> // Include the Relay library for controlling the relay
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. // Define constants
  41. #define DHTPIN 2 // Pin where the DHT sensor is connected
  42. #define DHTTYPE DHT11 // Define the type of DHT sensor (DHT11, DHT22, etc.)
  43. #define RELAY_PIN 3 // Pin where the relay is connected
  44.  
  45. // Create instances of the DHT and Relay classes
  46. DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
  47. Relay relay(RELAY_PIN, 1); // Create a Relay object with a 1-second period
  48.  
  49. void setup(void)
  50. {
  51.     // Initialize the DHT sensor
  52.     dht.begin();
  53.    
  54.     // Set the relay mode to automatic
  55.     relay.setRelayMode(relayModeAutomatic);
  56. }
  57.  
  58. void loop(void)
  59. {
  60.     // Read temperature as Celsius
  61.     float temperature = dht.readTemperature();
  62.  
  63.     // Check if the reading is successful
  64.     if (isnan(temperature)) {
  65.         // Handle error
  66.         return;
  67.     }
  68.  
  69.     // Control the relay based on temperature
  70.     if (temperature >= 20 && relay.getRelayPosition() == relayPositionOpen) {
  71.         relay.setRelayPosition(relayPositionClosed); // Activate relay
  72.     } else if (temperature >= 60 && relay.getRelayPosition() == relayPositionClosed) {
  73.         relay.setRelayPosition(relayPositionOpen); // Deactivate relay
  74.     }
  75.  
  76.     // Call the relay loop to manage relay timing
  77.     relay.loop();
  78. }
  79.  
  80. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement