#include <LiquidCrystal.h>
// Pin assignments
const int PIR_PIN = 7;
const int LDR_PIN = A0;
const int LASER_PIN = 13;
const int ULTRASONIC_TRIG_PIN = 8;
const int ULTRASONIC_ECHO_PIN = 10;
const int BUZZER_PIN = 9;
const int LCD_RS_PIN = 11;
const int LCD_EN_PIN = 12;
const int LCD_D4_PIN = 5;
const int LCD_D5_PIN = 4;
const int LCD_D6_PIN = 3;
const int LCD_D7_PIN = 2;
const int SWITCH_PIN = 6;
// System state
boolean alarmTriggered = false;
boolean alarmStopped = false;
// Sensor readings
int PIR_reading = 0;
int LDR_reading = 0;
long ultrasonic_distance = 0;
int Laser_reading = 0;
// LCD display
LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN);
// Function to turn on the alarm
void triggerAlarm() {
alarmTriggered = true;
digitalWrite(BUZZER_PIN, HIGH);
}
void laserPinCheckFirst() {
alarmTriggered = true;
// Laser_reading = digitalRead(LASER_PIN);
// if(Laser_reading == HIGH){
digitalWrite(BUZZER_PIN, HIGH);
// }
// else {
// digitalWrite(BUZZER_PIN, LOW);
// }
}
// Function to turn off the alarm
void stopAlarm() {
alarmTriggered = false;
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LASER_PIN, LOW);
}
// Function to measure the distance to an object using the ultrasonic sensor
long measureDistance() {
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
long pulseDuration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
long distance = pulseDuration / 58.2;
return distance;
}
void setup() {
// Set up the PIR sensor
pinMode(PIR_PIN, INPUT);
// Set up the LDR sensor
pinMode(LDR_PIN, INPUT);
// Set up the laser module
pinMode(LASER_PIN, OUTPUT);
// Set up the ultrasonic sensor
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode(ULTRASONIC_ECHO_PIN, INPUT);
// Set up the buzzer
pinMode(BUZZER_PIN, OUTPUT);
// Set up the LCD display
lcd.begin(16, 2);
// Turn off the alarm
// stopAlarm();
}
void loop() {
// Read the PIR sensor
PIR_reading = digitalRead(PIR_PIN);
// Read the LDR sensor
LDR_reading = analogRead(LDR_PIN);
// Read the ultrasonic sensor
ultrasonic_distance = measureDistance();
lcd.print("Security Theft Defender");
// Check if any sensor detects an object or motion
if (ultrasonic_distance < 200 || PIR_reading == HIGH) {
// Trigger the alarm
triggerAlarm();
}
if (LDR_reading < 500) {
// Turn on the laser module
laserPinCheckFirst();
}
// Check if the alarm is triggered
if (alarmTriggered) {
// Display alarm message on LCD
lcd.clear();
lcd.print("Alarm Triggered!");
// Show the sensor name that triggered the alarm on LCD
if (ultrasonic_distance < 200) {
lcd.print("\nUltrasonic Sensor");
} else if (PIR_reading == HIGH) {
lcd.print("\nPIR Sensor");
}
// Show warning message that someone enters on LCD
lcd.print("\nSomeone Enters!");
}
// Check if the alarm is stopped
else {
// Display message that the alarm is stopped
lcd.clear();
lcd.print("System Armed");
}
// Check if the switch is pressed
boolean switchState = digitalRead(SWITCH_PIN);
if (switchState == LOW) {
// Stop the alarm
stopAlarm();
}
}