#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define rgbLedRedPin 4 // Red pin of RGB LED
#define rgbLedBluePin 5 // Blue pin of RGB LED
#define buzzerPin 6 // Buzzer pin
#define pirPin 2 // PIR sensor pin
#define inputPin 7 // Input pin to receive the stop signal
unsigned long motionStartTime = 0; // Variable to store the time when motion is detected
bool motionDetected = false; // Flag to track motion detection
// Define LCD address and dimensions
#define LCD_ADDR 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
// Initialize LCD
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLS, LCD_ROWS);
void setup() {
pinMode(rgbLedRedPin, OUTPUT);
pinMode(rgbLedBluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(inputPin, INPUT);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on backlight
}
void loop() {
// Check PIR sensor
if (digitalRead(pirPin) == HIGH) {
if (!motionDetected) {
// Motion detected for the first time, record the start time
motionStartTime = millis();
motionDetected = true;
// Display motion detected message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detected!");
lcd.setCursor(0, 1);
lcd.print("Hands UP :D");
}
// Turn on red LED and buzzer for the first half of the time
digitalWrite(rgbLedRedPin, HIGH);
digitalWrite(rgbLedBluePin, LOW);
tone(buzzerPin, 550);
// Wait for 0.5 seconds
delay(500);
// Turn on blue LED and buzzer for the second half of the time
digitalWrite(rgbLedRedPin, LOW);
digitalWrite(rgbLedBluePin, HIGH);
tone(buzzerPin, 1500);
// Wait for another 0.5 seconds
delay(500);
} else {
// No motion detected
digitalWrite(rgbLedRedPin, LOW);
digitalWrite(rgbLedBluePin, LOW);
noTone(buzzerPin);
motionDetected = false; // Reset motion detection flag
// Display message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Motion Detected");
lcd.setCursor(0, 1);
lcd.print("System Armed");
delay(1000); // Delay to display the message
}
}