#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include <ESP32Servo.h>
#define Servo_pin 33
Servo myServo;
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] = {2, 0, 4, 5};
byte colPins[COLS] = {13, 12, 14, 27};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(19, 23, 18, 17, 16, 15);
const String password = "1234";
String inputPassword = "";
const int servoLockPos = 0;
const int servoUnlockPos = 90;
void setup() {
Serial.begin(115200);
Serial.println("4x4 Keypad Test");
Wire.begin();
lcd.begin(16,2);
myServo.attach(Servo_pin);
myServo.write(servoLockPos); // Lock position by default
Serial.println("Enter 4-digit password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
}
if (key) {
if (key == '#') { // '#' to confirm password
if (inputPassword == password) {
Serial.println("granted");
lcd.println("granted");
myServo.write(servoUnlockPos);
delay(5000); // Keep the servo at 90 degrees for 5 seconds
myServo.write(servoLockPos);
} else {
Serial.println("denied");
lcd.println("Denied");
}
inputPassword = ""; // Reset input
} else if (key == '*') { // '' to clear input
inputPassword = "";
Serial.println("Input cleared");
} else {
inputPassword += key; // Append key to password
Serial.print("*"); // Mask input
}
}
lcd.clear();
}