#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
// Define your keypad layout
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 };
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Password setup
String correctPassword = "1234"; // Set your password here
String enteredPassword = "";
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1); // Move to the second row for input
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') { // Check if the key is a number
enteredPassword += key; // Append the entered key to the password string
lcd.print("*"); // Display asterisk for each digit
}
else if (key == '#') { // '#' as 'Enter' to submit the password
if (enteredPassword == correctPassword) {
lcd.clear();
lcd.print("Access Granted");
lcd.setCursor(0, 1);
lcd.print("Unlocked!");
analogWrite(A0, 255); // Example: Set A0 to HIGH for unlocking
delay(3000); // Display the unlocked message for 3 seconds
lcd.clear();
lcd.print("Enter Password:");
} else {
lcd.clear();
lcd.print("Access Denied");
lcd.setCursor(0, 1);
lcd.print("Try Again");
delay(3000); // Display access denied for 3 seconds
lcd.clear();
lcd.print("Enter Password:");
}
enteredPassword = ""; // Clear the entered password after checking
}
else if (key == '*') { // '*' as 'Clear' to reset the input
enteredPassword = "";
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
}
}
}