Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: **Temperature Control**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-07 10:07:05
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* turn on a relay on temperature 20 and turn off */
- /* relay with temperature 60 */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <DHT.h>
- #include <Relay.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // DHT Sensor setup
- #define DHTPIN 2 // Pin where the DHT sensor is connected
- #define DHTTYPE DHT11 // DHT 11
- DHT dht(DHTPIN, DHTTYPE); // Create an instance of the DHT class
- // Relay setup
- #define RELAY_PIN 3 // Pin where the relay is connected
- Relay relay(RELAY_PIN, 1); // Create an instance of the Relay class
- void setup(void)
- {
- // Initialize the DHT sensor
- dht.begin();
- // Initialize the relay
- relay.setRelayMode(relayModeAutomatic);
- }
- void loop(void)
- {
- // Read temperature as Celsius
- float temperature = dht.readTemperature();
- // Check if the reading is valid
- if (isnan(temperature)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // Control the relay based on temperature
- if (temperature >= 20 && temperature < 60) {
- relay.setRelayPosition(relayPositionClosed); // Turn on the relay
- } else if (temperature >= 60) {
- relay.setRelayPosition(relayPositionOpen); // Turn off the relay
- }
- // Add a delay before the next loop iteration
- delay(2000); // Wait for 2 seconds before the next reading
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement