////Advanced burglar alarm security system with the help of PIR sensor, buzzer &
//keypad. (Alarm gets disabled if correct keypad password is entered)
#include <Keypad.h>
const int PIR_PIN = 1;
const int BUZZER_PIN = 13;
const int LED_PIN = 12;
const int LEDG_PIN = 11;
const int LEDY_PIN = 10;
const int PASSWORD_LENGTH = 4;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char password[PASSWORD_LENGTH + 1] = "4321";
char enteredPassword[PASSWORD_LENGTH + 1];
bool alarmEnabled = true;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(LEDG_PIN, OUTPUT);
pinMode(LEDY_PIN, OUTPUT);
digitalWrite(LEDY_PIN, HIGH);
Serial.begin(9600);
}
void loop() {
// Check PIR sensor for motion
if (digitalRead(PIR_PIN) == HIGH && alarmEnabled) {
digitalWrite(LEDY_PIN, LOW);
triggerAlarm();
}
// Check for keypad input
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') { // Check if enter key is pressed
if (checkPassword()) {
alarmEnabled = false; // Disable the alarm
digitalWrite(LEDY_PIN, LOW); // Turn off LED indicating alarm status
digitalWrite(LEDG_PIN, HIGH);
delay(2000);
digitalWrite(LEDG_PIN, LOW);
digitalWrite(LEDY_PIN, HIGH);
Serial.println("Alarm disabled");
} else {
digitalWrite(LEDY_PIN, LOW); // Turn off LED indicating alarm status
digitalWrite(LED_PIN, HIGH);
delay(2000);
digitalWrite(LED_PIN, LOW);
digitalWrite(LEDY_PIN, HIGH);
Serial.println("Incorrect password");
}
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear entered password buffer
} else {
if (strlen(enteredPassword) < PASSWORD_LENGTH) {
enteredPassword[strlen(enteredPassword)] = key; // Add entered key to the buffer
Serial.print("Entered: ");
Serial.println(enteredPassword);
}
}
}
}
void triggerAlarm() {
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED_PIN, HIGH);
Serial.println("Intruder detected! Alarm activated!");
delay(5000); // Alarm duration
digitalWrite(BUZZER_PIN, LOW);
}
bool checkPassword() {
return strcmp(password, enteredPassword) == 0;
}