#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
#define DHTPIN 4 // Digital pin connected to the DHT sensor / 4? 2?
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
int redPin = 3; // Red LED connected to digital pin 3
int greenPin = 4; // Green LED connected to digital pin 4
int yellowPin = 5; // Yellow LED connected to digital pin 5
int buzzerPin = 6; // Buzzer connected to digital pin 6
int photoPin = A0; // Photoresistor connected to analog pin A0
int joyPinX = A1; // Joystick X-axis connected to analog pin A1
int joyPinY = A2; // Joystick Y-axis connected to analog pin A2
Servo servo; // Create servo object to control the window
LiquidCrystal_I2C lcd(0x27,16,2); // Initialize LCD screen
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
servo.attach(9); // Attach servo to pin 9
lcd.init(); // Initialize LCD screen
lcd.backlight();
dht.begin(); // Initialize DHT sensor
}
void loop() {
float humidity = dht.readHumidity(); // Read humidity from DHT sensor
float temperature = dht.readTemperature(); // Read temperature from DHT sensor
int lightLevel = analogRead(photoPin); // Read light level from photoresistor
int joyX = analogRead(joyPinX); // Read joystick X-axis value
int joyY = analogRead(joyPinY); // Read joystick Y-axis value
// Check if light level is high enough
if (lightLevel >= 700) {
digitalWrite(yellowPin, HIGH); // Turn on yellow LED
}
else {
digitalWrite(yellowPin, LOW); // Turn off yellow LED
}
// Check temperature range and turn on appropriate LED
if ((temperature >= 5) && (temperature <= 25)) {
digitalWrite(greenPin, HIGH); // Turn on green LED
digitalWrite(redPin, LOW); // Turn off red LED
}
else if (temperature >= 25) {
digitalWrite(greenPin, LOW); // Turn off green LED
digitalWrite(redPin, HIGH); // Turn on red LED
}
else {
digitalWrite(greenPin, LOW); // Turn off green LED
digitalWrite(redPin, HIGH); // Turn on red LED
}
// Check humidity and turn on/off buzzer
if (humidity >= 60) {
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
}
else {
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
// Move servo with joystick
int servoPos = map(joyX, 0, 1023, 0, 180); // Map joystick X-axis value to servo position
servo.write(servoPos); // Move servo to mapped position
// Display readings on LCD screen
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("% ");
lcd.setCursor(0,2);
lcd.print("Light level: ");
lcd.print(lightLevel);
lcd.print(" ");
delay(1000); // Wait for 1 second before checking again
}