#include <Keypad.h>
#include <ESP32Servo.h> // ✅ Ensure this library is installed in Wokwi
// Keypad setup
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, 12, 13};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// ✅ Use "Servo" instead of "ESP32Servo"
Servo lockServo;
// ✅ Use a PWM-supported GPIO (change if needed)
const int servoPin = 4; // Can try GPIO4 or GPIO13 if issues persist
const int LOCK_POS =90;
const int UNLOCK_POS = 0;
// Lock System Variables
String correctPIN = "2042";
String enteredPIN = "";
bool isUnlocked = false;
unsigned long unlockTime = 0;
const unsigned long LOCK_DELAY = 5000;
void setup() {
Serial.begin(115200);
// ✅ Correct servo attachment for ESP32
lockServo.attach(servoPin, 500, 2500); // Attach with correct PWM range
lockServo.write(LOCK_POS); // Start locked
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("\n✅ Correct PIN! Unlocking...");
lockServo.write(UNLOCK_POS);
Serial.println("🔄 Servo moved to: UNLOCK_POS");
isUnlocked = true;
unlockTime = millis();
} else {
Serial.println("\n❌ Incorrect PIN!");
}
enteredPIN = "";
}
void resetPIN() {
enteredPIN = "";
Serial.println("\nPIN Reset");
}
void autoLock() {
Serial.println("🔒 Auto-locking...");
lockServo.write(LOCK_POS);
isUnlocked = false;
}