#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ESP32Servo.h>;
const int rows = 4;
const int cols = 4;
char keys[rows][cols]{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colpins[cols] = {26, 25, 33, 32};
byte rowpins[rows] = {13, 12, 14, 27};
Keypad keypad = Keypad(makeKeymap(keys), rowpins, colpins, rows, cols);
Servo servoMotor;
int servoPin = 4;
int parked;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const String passwordA = "7890";
const String passwordB = "1234";
const String passwordC = "5647";
const String passwordD = "1010";
String input_password;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
servoMotor.attach(servoPin, 500, 2400); //attach the servo to esp pin
input_password.reserve(4); // maximum input characters is 33, change if needed
}
void AccessOK(){
lcd.setCursor(0,1);
lcd.print("ACCESS GRANTED");
Serial.println("The password is incorrect, ACCESS Granted!");
input_password = "";
delay(1000);
servoMotor.write(90);
delay(2000);
servoMotor.write(180);
lcd.clear();
parked++;
}
//Function for buzzer if access is denied
void AccessDenied(){
lcd.setCursor(0,1);
lcd.print("ACCESS DENIED");
Serial.println("The password is incorrect, ACCESS DENIED!");
input_password = "";
delay(1000);
delay(1000);
lcd.clear();
}
void loop() {
char key = keypad.getKey();
lcd.setCursor(0,0); lcd.print(4 - parked); lcd.print (" Slots Left");
lcd.setCursor(0,1);lcd.print("CODE:"); lcd.setCursor(7,1);lcd.print(input_password);
if (key) {
Serial.println(key);
if (key == '*') {
input_password = "";lcd.clear(); // clear input password
} else if (key == '#') {//If password is correct, Plays tone and resets input password
if (passwordA == input_password) {AccessOK();}
else if(passwordB == input_password){AccessOK();}
else if(passwordC == input_password){AccessOK();}
else {AccessDenied();}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
}