#include <Keypad.h>
#include <LiquidCrystal.h>
// Initialize the LCD (pin numbers may vary depending on your setup)
const int rs = 7, en = 8, d4 = 9, d5 = 10, d6 = 11, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Define the keypad configuration
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
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}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; // Connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Predefined password
String password = "1234";
String inputPassword = "";
void setup() {
lcd.begin(16, 2);
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (inputPassword == password) {
lcd.clear();
lcd.print("Access Granted");
} else {
lcd.clear();
lcd.print("Access Denied");
delay(2000); // Wait for 2 seconds
lcd.clear();
lcd.print("Enter Password:");
}
inputPassword = ""; // Clear the input
} else if (key == '*') {
inputPassword = ""; // Clear the input if '*' is pressed
lcd.clear();
lcd.print("Enter Password:");
} else {
inputPassword += key;
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print(inputPassword);
}
}
}