#include <Keypad.h>
#define PIR_PIN 11
#define BUZZER_PIN 12
#define LED_PIN 13
const byte ROWS = 4; // Number of keypad rows
const byte COLS = 4; // Number of keypad columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int alarmState = 0;
char password[5] = "1234"; // Change the password as needed
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
Serial.begin(9600);
}
void loop() {
if (alarmState == 0) {
checkPassword(); // Check if password is entered to arm the alarm
} else {
checkMotion(); // Check for motion to trigger the alarm
}
}
void checkPassword() {
char key = keypad.getKey();
if (key) {
if (key == password[strlen(password) - 1]) {
if (strlen(password) == 4) {
disarmAlarm();
} else {
password[strlen(password)] = key;
}
} else {
resetPassword();
}
}
}
void disarmAlarm() {
alarmState = 1;
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
}
void resetPassword() {
memset(password, 0, sizeof(password));
}
void checkMotion() {
int motion = digitalRead(PIR_PIN);
if (motion == HIGH) {
activateAlarm();
}
}
void activateAlarm() {
digitalWrite(BUZZER_PIN, HIGH);
delay(2000); // Adjust the duration of the alarm as needed
digitalWrite(BUZZER_PIN, LOW);
}