#include <Keypad.h>
#include <ESP32Servo.h> // ✅ Make sure this is installed in Wokwi
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
byte rowPins[ROWS] = {25, 26, 27, 14};
byte colPins[COLS] = {32, 33, 34, 35};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo lockServo; // ✅ Changed from ESP32Servo to Servo
const int servoPin = 5;
const int LOCK_POS = 0;
const int UNLOCK_POS = 90;
String correctPIN = "1234";
String enteredPIN = "";
bool isUnlocked = false;
unsigned long unlockTime = 0;
const unsigned long LOCK_DELAY = 5000; // Auto-lock in 5 sec
void setup() {
Serial.begin(115200);
lockServo.attach(servoPin, 500, 2500); // ✅ Use attach with PWM range
lockServo.write(LOCK_POS);
Serial.println("System Locked.");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (key == '#') checkPIN();
else if (key == '*') resetPIN();
else enteredPIN += key;
}
if (isUnlocked && millis() - unlockTime > LOCK_DELAY) autoLock();
}
void checkPIN() {
if (enteredPIN == correctPIN) {
Serial.println("\nUnlocked!");
lockServo.write(UNLOCK_POS);
isUnlocked = true;
unlockTime = millis();
} else {
Serial.println("\nIncorrect PIN!");
}
enteredPIN = "";
}
void resetPIN() {
enteredPIN = "";
Serial.println("\nPIN Reset");
}
void autoLock() {
Serial.println("Auto-locking...");
lockServo.write(LOCK_POS);
isUnlocked = false;
}