#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <dht.h>
dht DHT;
#define DHT22_PIN 5
#define ldr 2
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
const int led = 9;
LiquidCrystal_I2C lcd(0x27, 20, 2); // 20 columns and 2 rows
void setup() {
Serial.begin(9600);
Serial.println(F("DHT22 test!"));
lcd.init();
lcd.backlight();
myservo.attach(8);
pinMode(led, OUTPUT);
pinMode(ldr, INPUT);
}
void loop() {
int chk = DHT.read22(DHT22_PIN);
float temperature = DHT.temperature;
float humidity = DHT.humidity;
// Check if any reads failed and exit early (to try again).
if (chk != DHTLIB_OK) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Display temperature and humidity
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Move the servo based on temperature
if (temperature > 25) {
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
//lcd.print(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
//lcd.print(pos);
delay(15);
}
} else {
myservo.write(0); // Keep servo at 0 position if temperature is <= 25
}
delay(2000); // Wait a bit before refreshing the display
lcd.clear();
// Check LDR and control LED
if (digitalRead(ldr) == LOW) {
digitalWrite(led, HIGH);
lcd.setCursor(4, 0);
lcd.print("Light ON!");
} else{
digitalWrite(led, LOW);
lcd.setCursor(4, 0);
lcd.print("Light OFF!");
}
delay(2000); // Wait a bit before repeating the loop
lcd.clear();
}