#include <Keypad.h>
#include <ESP32Servo.h>
Servo servo1;
int servoPosition;
//String lockStatus = "locked";
String code;
String pswd = "1596";
String pressedKey;
char keyMap[4][4] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
byte rowPins[4] = {14, 27, 26, 25};
byte columnPins[4] = {33, 32, 18, 19};
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, columnPins, 4, 4 );
void setup(){
Serial.begin(115200);
servo1.attach(17);
}
void loop(){
pressedKey = keypad.getKey();
if (pressedKey) {
code = code + pressedKey;
Serial.println(code);
}
//user unlock
if (code == String("1596A")) {
servo1.write(180);
Serial.println("Unlocked");
code = "";
}
//first method of checking if the variable contains something, in this case #
if (code.indexOf('#') != -1){
Serial.println("Code reset");
code = "";
servo1.write(90);
}
//second method of checking if the variable contains something, in this case *
if (pressedKey == String("*")) {
Serial.println("Code reset");
code = "";
servo1.write(90);
}
//admin unlock
if (code == pswd) {
servo1.write(180);
Serial.println("Unlocked");
code = "";
}
}