#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD display with I2C address 0x27 for 20x4 characters
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Define pins for various components
int buzzer = 2; // Pin connected to the buzzer
int greenLED = 11; // Pin connected to the green LED
int redLED = 12; // Pin connected to the red LED
int pirSensor = 13; // Pin connected to the PIR motion sensor
// Initial states for motion detection
int state = LOW; // Initially, no motion is detected
int sensorValue = 0; // Variable to store the PIR sensor's output
// Custom character arrays for displaying animations on the LCD
uint8_t pacman[8] = {
0b00000,
0b00000,
0b01110,
0b11011,
0b11111,
0b01110,
0b00000,
0b00000
};
uint8_t pacmanOpen[8] = {
0b00000,
0b00000,
0b01110,
0b11011,
0b11100,
0b01110,
0b00000,
0b00000
};
uint8_t dot[8] = {
0b00000,
0b00000,
0b00000,
0b00110,
0b00110,
0b00000,
0b00000,
0b00000
};
void setup() {
// Configure pin modes
pinMode(buzzer, OUTPUT);
pinMode(pirSensor, INPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
// Initialize the LCD
lcd.begin(20, 4);
lcd.backlight();
lcd.clear();
// Display welcome message
lcd.setCursor(2, 1);
lcd.print("WELCOME HOME");
lcd.setCursor(2, 2);
lcd.print("SECURITY SYSTEM");
delay(1000); // Display the welcome message for 1 second
}
void loop() {
// Read the sensor value
sensorValue = digitalRead(pirSensor);
// Check if the sensor output is HIGH (motion detected)
if (sensorValue == HIGH) {
digitalWrite(redLED, HIGH); // Turn on the red LED
digitalWrite(greenLED, LOW); // Turn off the green LED
// If it's the first detection since last no motion
if (state == LOW) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Motion detected!");
lcd.setCursor(3, 1);
lcd.print("SETTING ALARM");
tone(buzzer, 1000); // Sound the buzzer at 1000 Hz
// Display "LOADING" with animation
lcd.setCursor(6, 2);
lcd.print("LOADING");
for (int i = 3; i < 16; i++) {
lcd.setCursor(i, 3);
lcd.print("\1");
for (int j = i + 1; j < 16; j++) {
lcd.setCursor(j, 3);
lcd.print("\2");
}
lcd.createChar(1, pacman);
delay(200);
lcd.createChar(1, pacmanOpen);
delay(200);
lcd.setCursor(i, 3);
lcd.print(" ");
}
state = HIGH; // Update the state to indicate motion has been detected
noTone(buzzer); // Stop the buzzer sound
}
}
else {
digitalWrite(redLED, LOW); // Turn off the red LED
digitalWrite(greenLED, HIGH); // Turn on the green LED
delay(200); // Brief delay to stabilize output
// Reset the system state when motion stops
if (state == HIGH) {
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("Motion stopped!");
state = LOW; // Update the state to indicate no motion is detected
}
}
}