#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const bool SENSOR_INVERTED = true;
const int lightPin = 2;
const int buttonPin = 3;
const int led1 = 4;
const int buzzer1 = 6;
const int buzzer2 = 7;
const int buzzer3 = 8;
const int freq1 = 1000;
const int freq2 = 1500;
const int freq3 = 2000;
bool systemActive = true;
bool lastButtonState = HIGH;
unsigned long previousMillis = 0;
const long buzzerInterval = 500;
int buzzerStep = 0;
// BACKLIGHT CONTROL
unsigned long lastLightDetectedTime = 0;
const unsigned long backlightTimeout = 3000;
void setup() {
pinMode(lightPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
pinMode(buzzer3, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Security System ");
delay(1500);
lcd.clear();
digitalWrite(led1, HIGH); // system armed by default
}
void turnBuzzersOff() {
noTone(buzzer1);
noTone(buzzer2);
noTone(buzzer3);
}
void cycleBuzzers() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= buzzerInterval){
previousMillis = currentMillis;
buzzerStep = (buzzerStep + 1) % 3;
turnBuzzersOff();
if(buzzerStep == 0) tone(buzzer1, freq1);
else if(buzzerStep == 1) tone(buzzer2, freq2);
else tone(buzzer3, freq3);
}
}
void loop() {
unsigned long currentMillis = millis();
// -------- BUTTON TOGGLE --------
bool buttonState = digitalRead(buttonPin);
if(buttonState == LOW && lastButtonState == HIGH){
systemActive = !systemActive;
lcd.backlight(); // turn backlight on when button pressed
lcd.clear();
lastLightDetectedTime = currentMillis; // reset backlight timer
delay(200); // simple debounce
}
lastButtonState = buttonState;
digitalWrite(led1, systemActive ? HIGH : LOW);
// -------- READ LIGHT SENSOR --------
int rawLight = digitalRead(lightPin);
bool lightDetected = SENSOR_INVERTED ? (rawLight == LOW) : (rawLight == HIGH);
// reset backlight timer if light detected
if(lightDetected) {
lcd.backlight();
lastLightDetectedTime = currentMillis;
}
// -------- BACKLIGHT TIMEOUT --------
if(currentMillis - lastLightDetectedTime >= backlightTimeout){
lcd.noBacklight();
}
// -------- SYSTEM DISARMED --------
if(!systemActive){
turnBuzzersOff();
lcd.setCursor(0,0);
lcd.print(" SYSTEM DISARMED ");
lcd.setCursor(0,1);
lcd.print(" no alarm ");
lcd.setCursor(0,2);
lcd.print(" Press button to ARM ");
return;
}
// -------- NO LIGHT DETECTED --------
if(!lightDetected){
turnBuzzersOff();
lcd.setCursor(0,0);
lcd.print(" SECURED ");
lcd.setCursor(0,1);
lcd.print(" No Activity ");
lcd.setCursor(0,2);
lcd.print(" DOOR CLOSED ");
lcd.setCursor(0,3);
lcd.print("System: ARMED ");
}
// -------- LIGHT DETECTED (ALARM) --------
else{
lcd.setCursor(0,0);
lcd.print(" DOOR OPENED ");
lcd.setCursor(0,1);
lcd.print(" UNAUTHORIZED USE ");
cycleBuzzers();
lcd.setCursor(0,2);
if(buzzerStep == 0) lcd.print("Buzzer 1 ACTIVE ");
else if(buzzerStep == 1) lcd.print("Buzzer 2 ACTIVE ");
else lcd.print("Buzzer 3 ACTIVE ");
lcd.setCursor(0,3);
lcd.print("System: ARMED ");
}
}