/*
NOTE THAT THE CODE WILL NOTICE THE UPDATE EVERY 7.5 SECONDS
*/
//-------Libraries-------------
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
//-------Pins-------------
const uint8_t Bed_LED_1 = 11;
const uint8_t Bed_LED_2 = 10;
const uint8_t Bathroom_LED = 9;
const uint8_t Hot_LED = 8;
const uint8_t Cool_LED = 7;
const uint8_t Bed_Switch = 12;
const uint8_t Bathroom_Switch = 6;
const uint8_t Motion_sensor = 3;
const uint8_t Potentiometer = A0;
//-------Define DHT-------------
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//-------Define LCD-------------
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
//-------Variables-------------
int brightness = 0; // Stores the brightness level
const uint8_t Set_temperature = 25; // The Setpoint Temperature
float humidity = 0.0; // Stores humidity value
float temperature = 0.0; // Store room temperature value
bool BedSwitchState = LOW; // Store the state of the switch in bedroom
bool BathroomSwitchState = LOW; // Store the state of the switch in bathroom
void setup() {
//-------Pins Mode-------------
pinMode(Bed_LED_1,OUTPUT);
pinMode(Bed_LED_2,OUTPUT);
pinMode(Bathroom_LED,OUTPUT);
pinMode(Hot_LED,OUTPUT);
pinMode(Cool_LED,OUTPUT);
pinMode(Bed_Switch,INPUT);
pinMode(Bathroom_Switch,INPUT);
pinMode(Motion_sensor,INPUT);
pinMode(Potentiometer,INPUT);
//-------Initialise LCD-------------
lcd.init();
lcd.backlight();
//-------Initialise DHT-------------
dht.begin();
//-------Initialise serial communication-------------
Serial.begin(9600);
}
void loop() {
delay(2000); // Wait a few seconds between reading
Bedroom();
Bathroom();
Air_condition();
//-------Print-------------
lcd.setCursor(0, 1);
lcd.print("Temp is ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 2);
lcd.print("Bed light is ");
lcd.print(BedSwitchState == HIGH ? "ON" : "OFF");
lcd.setCursor(0, 3);
lcd.print("Bath light is ");
lcd.print(BathroomSwitchState == HIGH ? "ON" : "OFF");
}
void Bedroom() {
// Read the potentiometer value and map it to PWM range (0-255)
brightness = map(analogRead(Potentiometer), 0, 1023, 0, 255);
if (digitalRead(Bed_Switch) == HIGH) { // If switch is ON (HIGH)
if (brightness > 0) {
analogWrite(Bed_LED_1, brightness); // Adjust brightness based on potentiometer
analogWrite(Bed_LED_2, brightness);
BedSwitchState = HIGH;
} else {
analogWrite(Bed_LED_1, 0); // Turn off LEDs if potentiometer is at 0
analogWrite(Bed_LED_2, 0);
BedSwitchState = LOW;
}
} else { // If switch is OFF (LOW)
analogWrite(Bed_LED_1, 0); // Turn off LEDs
analogWrite(Bed_LED_2, 0);
BedSwitchState = LOW;
}
delay(100); // Short delay for debounce and smooth control
}
void Bathroom(){
// If switch is ON (HIGH) or motion is detected
if (digitalRead(Bathroom_Switch) == HIGH || digitalRead(Motion_sensor) == HIGH)
{
digitalWrite(Bathroom_LED, HIGH); //Turn on the LED
delay(5000);// wait five seconds
BathroomSwitchState = HIGH;
}
else //If switch is off or no motion
{
digitalWrite(Bathroom_LED, LOW); //Turn off the LED
BathroomSwitchState = LOW;
}
}
void Air_condition(){
// Reading humidity
humidity = dht.readHumidity();
// Read temperature as Celsius
temperature = dht.readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
if (temperature > (Set_temperature + 1))
{
// Cooling mode
digitalWrite(Cool_LED,HIGH);
digitalWrite(Hot_LED,LOW);
lcd.print("AC MODE : CM");
}
else if (temperature < (Set_temperature - 1))
{
// Heating mode
digitalWrite(Cool_LED,LOW);
digitalWrite(Hot_LED,HIGH);
lcd.print("AC MODE : HM");
}
else
{
// room Temp = set Temp , Turn off the AC
digitalWrite(Cool_LED,LOW);
digitalWrite(Hot_LED,LOW);
lcd.print("AC MODE : OFF");
}
}