#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad setup
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] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 2, 15};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Password
String password = "1234";
String inputPassword = "";
// Lock LED
#define LED_PIN 23
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Locked by default
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Enter
if (inputPassword == password) {
lcd.clear();
lcd.print("Access Granted");
digitalWrite(LED_PIN, HIGH); // Unlock (LED ON)
delay(2000);
} else {
lcd.clear();
lcd.print("Access Denied");
digitalWrite(LED_PIN, LOW); // Keep locked
delay(2000);
}
inputPassword = "";
lcd.clear();
lcd.print("Enter Password:");
}
else if (key == '*') { // Clear input
inputPassword = "";
lcd.clear();
lcd.print("Enter Password:");
}
else {
inputPassword += key;
lcd.setCursor(0,1);
lcd.print(inputPassword);
}
}
}