#include <DHT.h>
#include <LiquidCrystal.h>
#include <TimeLib.h>
#include <ArduinoJson.h>
#include <pitches.h>
// Pin Definitions
#define DHT_PIN 2
#define PHOTORESISTOR_PIN A0
#define PIR_PIN 3
#define RED_LED_PIN 4
#define GREEN_LED_PIN 5
#define BLUE_LED_PIN 6
#define BUZZER_PIN 7
#define LCD_RS 8
#define LCD_EN 9
#define LCD_D4 10
#define LCD_D5 11
#define LCD_D6 12
#define LCD_D7 13
#define BUTTON_PIN A1
// Constants
#define DHT_TYPE DHT22
#define TEMP_HIGH 30
#define TEMP_LOW 20
#define DARK_THRESHOLD 300
#define LCD_UPDATE_INTERVAL 1000
#define SERIAL_UPDATE_INTERVAL 10000
#define MOTION_TIMEOUT 30000
#define ALARM_DURATION 3000
#define BUTTON_DEBOUNCE_DELAY 200
// Global variables
DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
float temperature = 0;
float humidity = 0;
int lightLevel = 0;
bool motionDetected = false;
unsigned long lastLCDUpdate = 0;
unsigned long lastSerialUpdate = 0;
unsigned long lastMotionTime = 0;
unsigned long alarmStartTime = 0;
unsigned long lastButtonPress = 0;
bool alarmActive = false;
bool lcdBacklightOn = true;
int displayMode = 0;
bool nightMode = false;
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2);
lcd.print("Initializing...");
pinMode(PIR_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Set initial time (10:00 PM for testing night mode)
setTime(22, 0, 0, 1, 1, 2023);
delay(2000);
lcd.clear();
}
void loop() {
readSensors();
checkButton();
updateLCD();
updateLEDs();
checkAlarm();
sendSerialData();
checkPowerSaving();
updateNightMode();
}
void readSensors() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
lightLevel = analogRead(PHOTORESISTOR_PIN);
motionDetected = digitalRead(PIR_PIN) == HIGH;
if (motionDetected) {
lastMotionTime = millis();
}
}
void checkButton() {
if (digitalRead(BUTTON_PIN) == LOW && millis() - lastButtonPress > BUTTON_DEBOUNCE_DELAY) {
displayMode = (displayMode + 1) % 3; // Cycle through 3 display modes
lastButtonPress = millis();
}
}
void updateLCD() {
if (millis() - lastLCDUpdate >= LCD_UPDATE_INTERVAL) {
lcd.clear();
lcd.setCursor(0, 0);
switch (displayMode) {
case 0: // Temperature and Humidity
lcd.print("Temp: ");
lcd.print(temperature, 1);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity, 1);
lcd.print("%");
break;
case 1: // Light and Motion
lcd.print("Light: ");
lcd.print(lightLevel);
lcd.setCursor(0, 1);
lcd.print("Motion: ");
lcd.print(motionDetected ? "Detected" : "None");
break;
case 2: // Time and Mode
lcd.print("Time: ");
lcd.print(formatTime(now()));
lcd.setCursor(0, 1);
lcd.print("Mode: ");
lcd.print(nightMode ? "Night" : "Day");
break;
}
lastLCDUpdate = millis();
}
}
void updateLEDs() {
digitalWrite(RED_LED_PIN, temperature > TEMP_HIGH);
digitalWrite(GREEN_LED_PIN, temperature >= TEMP_LOW && temperature <= TEMP_HIGH);
digitalWrite(BLUE_LED_PIN, temperature < TEMP_LOW);
}
void checkAlarm() {
Serial.print(motionDetected);
Serial.print(lightLevel<DARK_THRESHOLD);
Serial.print(alarmActive);
Serial.println(nightMode);
if (motionDetected && lightLevel < DARK_THRESHOLD && !alarmActive &&nightMode) {
alarmActive = true;
alarmStartTime = millis();
tone(BUZZER_PIN, 1000);
}
if (alarmActive && (millis() - alarmStartTime >= ALARM_DURATION || nightMode)) {
alarmActive = false;
noTone(BUZZER_PIN);
}
}
void sendSerialData() {
if (millis() - lastSerialUpdate >= SERIAL_UPDATE_INTERVAL) {
// Serial.print("Temperature: ");
// Serial.print(temperature);
// Serial.print("C, Humidity: ");
// Serial.print(humidity);
// Serial.print("%, Light: ");
// Serial.print(lightLevel);
// Serial.print(", Motion: ");
// Serial.print(motionDetected ? "Detected" : "Not Detected");
// Serial.print(", Mode: ");
// Serial.println(nightMode ? "Night" : "Day");
StaticJsonDocument<200> doc;
doc["temperature"] = temperature;
doc["humidity"] = humidity;
doc["lightLevel"] = lightLevel;
doc["motionDetected"] = motionDetected;
char buffer[200];
serializeJson(doc, buffer);
Serial.println(buffer);
lastSerialUpdate = millis();
}
}
void checkPowerSaving() {
if (millis() - lastMotionTime >= MOTION_TIMEOUT) {
if (lcdBacklightOn) {
lcdBacklightOn = false;
lcd.noDisplay();
}
} else {
if (!lcdBacklightOn) {
lcdBacklightOn = true;
lcd.display();
}
}
}
void updateNightMode() {
int currentHour = hour(now());
nightMode = (currentHour >= 22 || currentHour < 6);
}
String formatTime(time_t t) {
char buffer[9];
sprintf(buffer, "%02d:%02d:%02d", hour(t), minute(t), second(t));
return String(buffer);
}