#include <LiquidCrystal.h>
#include <Keypad.h>
// LCD pin configuration
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Keypad configuration
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] = {9, 8, 7, 6};
byte colPins[COLS] = {A5, A4, A3, A2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int trigPin = 10; // Ultrasonic sensor trigger pin
const int echoPin = A0; // Ultrasonic sensor echo pin
const int buzzerPin = 13;
const String password = "1234";
String input = "";
bool armed = false;
const int maxDistance = 100; // Maximum distance in cm to trigger the alarm
void setup() {
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
lcd.print("Alarm System");
lcd.setCursor(0, 1);
lcd.print("Enter code:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') {
input = "";
lcd.clear();
lcd.print("Enter code:");
lcd.setCursor(0, 1);
} else if (key == '#') {
if (input == password) {
armed = !armed;
lcd.clear();
lcd.print(armed ? "System Armed" : "System Disarmed");
delay(200);
lcd.clear();
lcd.print("Enter code:");
input = "";
} else {
lcd.clear();
lcd.print("Wrong Password");
delay(2000);
lcd.clear();
lcd.print("Enter code:");
input = "";
}
} else {
input += key;
lcd.setCursor(0, 1);
lcd.print(input);
}
}
if (armed && detectIntruder()) {
lcd.clear();
lcd.print("INTRUDER ALERT!");
tone(buzzerPin, 1000);
delay(15000);
noTone(buzzerPin);
lcd.clear();
lcd.print("Enter code:");
}
}
bool detectIntruder() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // Convert to centimeters
return (distance < maxDistance);
}