CODE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHTPIN 4. // Pin where the DHT sensor is connected
#define DHTTYPE DHT11 // Change to DHT22 if you're using that instead
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Setup Buttons and Relay
int Setvariable = 40;
int Upbutton = 6;
int Downbutton = 7;
int relayPin = 3;
const unsigned long Interval = 1000;
unsigned long previousTime = 0;
const unsigned long Interval2 = 3000;
unsigned long previousTime2 = 0;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" WELLCOME");
delay(1000);
dht.begin(); // Start DHT sensor
delay(1000);
lcd.clear();
pinMode(Upbutton, INPUT_PULLUP);
pinMode(Downbutton, INPUT_PULLUP);
}
void loop() {
unsigned long currentTime = millis();
unsigned long currentTime2 = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (currentTime2 - previousTime2 >= Interval2) {
Serial.print("Current humidity= ");
Serial.print(h, 1);
Serial.print("% ");
Serial.print("temperature= ");
Serial.print(t, 1);
Serial.print((char)223);
Serial.println("C");
lcd.setCursor(0, 0);
lcd.print("Hum: ");
lcd.print(h, 0);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.print(t, 0);
lcd.print((char)223);
lcd.print("C");
previousTime2 = currentTime2;
}
if (t > Setvariable) {
Serial.print(" OFF ");
lcd.setCursor(9, 0);
lcd.print("SW: OFF ");
digitalWrite(relayPin, LOW);
} else if (t < Setvariable) {
Serial.print(" ON");
lcd.setCursor(9, 0);
lcd.print("SW: ON");
digitalWrite(relayPin, HIGH);
}
lcd.setCursor(10, 1);
lcd.print("Set:");
lcd.print(Setvariable);
if (currentTime - previousTime >= Interval) {
if (digitalRead(Upbutton) == LOW) Setvariable++;
if (digitalRead(Downbutton) == LOW) Setvariable--;
previousTime = currentTime;
}
}