For block diagram
For code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial bt (7,8);
const int ldrPin = 10;
const int ledPin1 = 12;
const int ledPin2 = 11;
const int buttonPin1 = 3; // Assuming the first button is connected to pin 7
const int buttonPin2 = 4; // Assuming the second button is connected to pin 6
const int ledPin3 = 6; // LED 3
const int ledPin4 = 5; // LED 4
unsigned long lcdWarningStartTime = 0; // Variable to store the start time for the warning message
const unsigned long lcdWarningDuration = 2000; // Duration for the warning message in milliseconds
unsigned long darkSceneStartTime = 0;
const unsigned long darkSceneDelay = 2000; // 2-second delay
int lightSensorValue;
int darkSceneThreshold = 200; // Adjust this threshold based on your light conditions
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 chars by 2 lines display
void setup() {
Serial.begin(9600);
bt.begin (9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ldrPin, INPUT);
pinMode(buttonPin1, INPUT_PULLUP); // Using internal pull-up resistor for button 1
pinMode(buttonPin2, INPUT_PULLUP); // Using internal pull-up resistor for button 2
lcd.begin();
lcd.backlight();
}
void loop() {
unsigned long currentMillis = millis(); // Current time
if (digitalRead(ldrPin) == HIGH || digitalRead(buttonPin1) == LOW) {
// Existing dark scene LCD code
if (digitalRead(ldrPin) == HIGH && digitalRead(buttonPin2) == LOW) {
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("DARK SCENE : YES");
bt.print("DARK SCENE: YES ");
bt.print("OPEN LIGHT");
delay(1000);
lcd.setCursor(0, 1);
lcd.clear();
lcd.print("OPEN LIGHT");
delay(1000);
} else {
if (currentMillis - lcdWarningStartTime < lcdWarningDuration) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WARNING ");
delay(1000);
} else {
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("DARK SCENE : YES");
bt.print("DARK SCENE: YES ");
bt.print("OPEN LIGHT");
delay(1000);
lcd.setCursor(0, 1);
lcd.clear();
lcd.print("OPEN LIGHT");
}
}
} else {
// Existing LCD code for when not in a dark scene
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("DARK SCENE : NO");
bt.print("DARK SCENE: NO ");
bt.print("CLOSED LIGHT ");
delay(1000);
lcd.setCursor(0, 1);
lcd.clear();
lcd.print("CLOSED LIGHT");
lcdWarningStartTime = currentMillis;
}
// Existing LED control code
if (digitalRead(ldrPin) == HIGH || digitalRead(buttonPin1) == LOW) {
// Existing LED control code
if (darkSceneStartTime == 0) {
darkSceneStartTime = millis();
}
if (millis() - darkSceneStartTime >= darkSceneDelay) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
if (digitalRead(ldrPin) == HIGH && digitalRead(buttonPin2) == LOW) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
}
} else {
// Existing LED control code when not in a dark scene
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
darkSceneStartTime = 0;
}
// Ensure that the serial buffer is flushed before the delay to avoid issues
Serial.flush();
delay(1000); // Adding a delay of 1 second (1000 milliseconds)
}