#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN 14 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor type
DHT dht(DHTPIN, DHTTYPE);
#define TRIGGER_PIN 15 // Pin for the ultrasound motion sensor
#define LED_AC 5 // Pin for the AC LED
#define LED_LIGHTS 18 // Pin for the lights LED
#define POTENTIOMETER_PIN 34 // Pin for the potentiometer
// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool motionDetected = false;
bool lightsOn = false;
bool acOn = false;
float thermostatSetting = 0.0;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(TRIGGER_PIN, INPUT);
pinMode(LED_AC, OUTPUT);
pinMode(LED_LIGHTS, OUTPUT);
}
void loop() {
// Read temperature and humidity from DHT sensor
float temperature = dht.readTemperature();
// Read motion status from ultrasound sensor
motionDetected = digitalRead(TRIGGER_PIN) == HIGH;
// Read potentiometer value for thermostat setting
int potValue = analogRead(POTENTIOMETER_PIN);
thermostatSetting = map(potValue, 0, 4095, 16.0, 30.0); // Map the potentiometer value to a temperature range
// Display information on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.setCursor(0, 1);
lcd.print("AC: ");
lcd.print(acOn ? "On" : "Off");
lcd.print(" Thermostat: ");
lcd.print(thermostatSetting);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("Lights: ");
lcd.print(lightsOn ? "On" : "Off");
lcd.print(" Motion: ");
lcd.print(motionDetected ? "Detected" : "Not Detected");
// Check and control AC and lights
controlAC();
controlLights();
delay(1000); // Update every second
}
void controlAC() {
// Simulate AC control using an LED
if (temperature > thermostatSetting && !acOn) {
acOn = true;
digitalWrite(LED_AC, HIGH); // Turn on AC LED
// Add code to turn on actual AC here if available
} else if (temperature <= thermostatSetting && acOn) {
acOn = false;
digitalWrite(LED_AC, LOW); // Turn off AC LED
// Add code to turn off actual AC here if available
}
}
void controlLights() {
// Simulate lights control using an LED
if (motionDetected && !lightsOn) {
lightsOn = true;
digitalWrite(LED_LIGHTS, HIGH); // Turn on lights LED
// Add code to turn on actual lights here if available
} else if (!motionDetected && lightsOn) {
lightsOn = false;
digitalWrite(LED_LIGHTS, LOW); // Turn off lights LED
// Add code to turn off actual lights here if available
}
}