#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Wire.h>
// Set the LCD address (usually 0x27 or 0x3F for most I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2); // 16 columns and 2 rows
// Define pin connections
const int trigPin = 9;
const int echoPin = 11;
const int buzzerPin = 8;
const int ledPin = 7; // Pin for the LED
long duration;
int distance;
// Define keypad rows and columns
const byte ROWS = 4;
const byte COLS = 4;
// Define keypad layout
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, 13, 12};
// Create keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define password and threshold distance
char password[] = "1234";
const int thresholdDistance = 20; // Adjust as needed
// Buffer to store the entered password
char passwordBuffer[5];
int bufferIndex = 0;
void setup() {
lcd.begin(16, 2); // Initialize the LCD
lcd.setBacklight(1); // Turn on the backlight
lcd.print("Enter Password");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600);
}
void loop() {
// Get distance measurement
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0343 / 2;
// Get password input
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
// Check the entered password against the correct password
if (strcmp(password, passwordBuffer) == 0) {
lcd.clear();
lcd.print("Password Correct");
digitalWrite(ledPin, HIGH); // Turn on the LED
} else {
lcd.clear();
lcd.print("Incorrect Password");
tone(buzzerPin, 1000, 500);
delay(500);
noTone(buzzerPin);
lcd.clear();
lcd.print("Enter Password");
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Clear the password buffer
bufferIndex = 0;
} else {
// Add the pressed key to the password buffer and clear the LCD
passwordBuffer[bufferIndex] = key;
bufferIndex++;
lcd.clear();
lcd.print(passwordBuffer);
}
}
// Check distance and trigger alarm if necessary
if (distance < thresholdDistance) {
if (strcmp(password, passwordBuffer) == 0) {
lcd.clear();
lcd.print("Alarm Activated");
tone(buzzerPin, 1000, 500);
delay(500);
noTone(buzzerPin);
}
}
}