#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define I2C_ADDR 0x27 // Change to your address if needed
LiquidCrystal_I2C lcd(I2C_ADDR, 20, 4); // Initialize LCD for 20x4
// 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'} // Using '#' as Enter key and '*' as Backspace key
};
// Pin assignments
byte rowPins[ROWS] = {14, 27, 26, 25}; // Adjust these pins as per your ESP32 model
byte colPins[COLS] = {33, 32, 18, 19}; // Adjust these pins as per your ESP32 model
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Security code
const char* password = "1234"; // Your defined security code
char enteredPassword[5]; // Array to store the entered password
int buzzerPin = 23; // Pin for the buzzer
int redLedPin = 16; // Pin for the red LED
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
digitalWrite(redLedPin, LOW); // Ensure LED is off at the start
lcd.begin(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0); // Set cursor to column 0, row 0
lcd.print("Enter the code"); // Print message on LCD
}
void loop() {
char key = keypad.getKey(); // Get the key pressed
if (key) {
Serial.println(key); // Display the pressed key for debugging
// If the enter key '#' is pressed
if (key == '#') {
enteredPassword[4] = '\0'; // Null-terminate the string
if (strcmp(enteredPassword, password) == 0) {
// If the password is correct
digitalWrite(redLedPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
Serial.println("Correct Password!"); // Successful message
delay(2000); // Optional delay before reset
lcd.clear(); // Clear the LCD
lcd.print("Access Granted"); // Show message
} else {
// If the password is incorrect
triggerAlarm(); // Call the alarm function
Serial.println("Incorrect Password!"); // Failure message
lcd.clear(); // Clear the LCD
lcd.print("Access Denied"); // Show message
}
// Reset the password entry
memset(enteredPassword, 0, sizeof(enteredPassword)); // Clear the entered password
return; // Exit the loop
}
// If the backspace key '*' is pressed
else if (key == '*') {
for (int i = 3; i >= 0; i--) {
if (enteredPassword[i] != 0) { // Find the last entered character
enteredPassword[i] = 0; // Clear the last character
break; // Exit the loop after removing one character
}
}
} else {
// Add the entered key to the enteredPassword array
for (int i = 0; i < 4; i++) {
if (enteredPassword[i] == 0) { // Find the first empty spot
enteredPassword[i] = key; // Add the key to the enteredPassword
break;
}
}
}
}
}
// Function to handle incorrect password entry
void triggerAlarm() {
while (true) {
digitalWrite(redLedPin, HIGH); // Turn on the red LED
tone(buzzerPin, 1000); // Start buzzer sound (1000 Hz)
delay(300); // Duration buzzer and LED ON
digitalWrite(redLedPin, LOW); // Turn off the red LED
noTone(buzzerPin); // Stop buzzer sound
delay(300); // Duration buzzer and LED OFF
Serial.println("Incorrect Password!");
// Check for keypad input to reset alarm
char key = keypad.getKey();
if (key == '*') { // Press '*' to reset the alarm
digitalWrite(redLedPin, LOW); // Turn off the LED
noTone(buzzerPin); // Turn off the buzzer
break; // Exit the alarm routine
}
}
}