#include <Servo.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
byte val, time = 0;
float t, h;
float t_past = 0, h_past = 0;
DHT dht(2, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
myservo.attach(11);
}
void loop() {
t = dht.readTemperature();
h = dht.readHumidity();
if (((t != t_past) || (h != h_past)) && (time == 0)) {
time++;
if ((t > 10) && (h > 50)) {
val = map(h, 50, 100, 0, 180);
myservo.write(val);
} else {
val = 0;
myservo.write(val);
}
}
lcd.setCursor(0, 0); lcd.print("T:"); lcd.print(t); lcd.print(" ");
lcd.setCursor(0, 1); lcd.print("H:"); lcd.print(h); lcd.print(" ");
lcd.setCursor(10, 0); lcd.print("<="); lcd.print(val); lcd.print(" ");
if (time > 0) {
lcd.setCursor(10, 1); lcd.print("time "); lcd.print(2 - time); lcd.print("s ");
} else {
lcd.setCursor(10, 1); lcd.print("time "); lcd.print(0); lcd.print("s ");
}
Serial.print("Humidity: ");
Serial.print(h, 1);
Serial.print("% Temperature: ");
Serial.print(t, 1);
Serial.print("C & ");
Serial.print(time);
Serial.println("s");
delay(500);
if ((time >= 1) && (time < 2)) {
time++;
} else {
time = 0;
t_past = t;
h_past = h;
}
}