#include <Keypad.h>
#include <ESP32Servo.h> // Use ESP32Servo library
#define SERVO_PIN 18
const String password = "1234"; // Set your password here
String inputPassword = "";
Servo myServo;
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] = {13, 12, 14, 27};
byte colPins[COLS] = {26, 25, 33, 32};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
myServo.attach(SERVO_PIN);
myServo.write(0); // Initial locked position
Serial.println("Enter password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (key == '*') {
inputPassword = "";
Serial.println("\nCleared input");
return;
}
if (key == '#') {
Serial.println();
if (inputPassword == password) {
Serial.println("Password correct! Unlocking...");
unlockServo();
} else {
Serial.println("Incorrect password!");
}
inputPassword = "";
} else {
inputPassword += key;
}
}
}
void unlockServo() {
myServo.write(90); // Unlock position
delay(3000);
myServo.write(0); // Lock position
}