#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the I2C LCD (Set the correct I2C address and LCD dimensions)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD I2C address
// Define the keypad layout
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] = {12, 13, 14, 15}; // Connect to the row pins of the keypad
byte colPins[COLS] = {16, 17, 18, 19}; // Connect to the column pins of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define the correct password
const String correctPassword = "1234"; // Replace with your password
String enteredPassword = ""; // Buffer to store entered password
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Start Serial Monitor (optional for debugging)
Serial.begin(115200);
}
void loop() {
char key = keypad.getKey(); // Get a key press from the keypad
if (key) { // If a key is pressed
Serial.print(key); // Print the key to Serial Monitor for debugging
if (key == '#') { // '#' is the submit key
lcd.clear();
if (enteredPassword == correctPassword) {
lcd.print("Password Correct!");
Serial.println("\nPassword Correct!");
} else {
lcd.print("Password Incorrect!");
Serial.println("\nPassword Incorrect!");
}
delay(2000); // Show the result for 2 seconds
lcd.clear();
lcd.print("Enter Password:");
enteredPassword = ""; // Clear the entered password
} else if (key == '*') { // '*' is the clear key
enteredPassword = ""; // Clear the entered password buffer
lcd.clear();
lcd.print("Password Cleared!");
delay(1000); // Wait 1 second before showing the main screen
lcd.clear();
lcd.print("Enter Password:");
} else {
// Add the pressed key to the password buffer
enteredPassword += key;
// Display entered password as '*'
lcd.setCursor(0, 1);
for (int i = 0; i < enteredPassword.length(); i++) {
lcd.print('*');
}
}
}
}