#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <ESP32Servo.h>
#define LEDM 25
#define LEDH 33
#define SERVO_PIN 26
#define BUZZER_PIN 23
Servo servo;
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {12, 13, 14, 15};
byte pin_column[COLUMN_NUM] = {16, 17, 18, 19};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
LiquidCrystal_I2C lcd(0x27, 20, 4);
const String password_1 = "ABC1234";
const String password_2 = "1917";
const String password_3 = "9765";
String input_password;
int angle = 0;
unsigned long lastTime;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
input_password.reserve(32);
servo.attach(SERVO_PIN);
servo.write(0);
lastTime = millis();
pinMode(LEDM, OUTPUT);
pinMode(LEDH, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Selamat Datang!");
lcd.setCursor(0, 2);
lcd.print("Masukkan Password");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = "";
} else if (key == '#') {
if (input_password == password_1 || input_password == password_2 || input_password == password_3) {
digitalWrite(LEDH, HIGH);
Serial.println("Password benar, Membuka Palang");
tone(BUZZER_PIN, 1000, 500);
angle = 90;
servo.write(angle);
lastTime = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password Benar");
lcd.setCursor(0, 2);
lcd.print("Silahkan Masuk");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang!");
lcd.setCursor(0, 2);
lcd.print("Masukkan Password");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password Salah!");
lcd.setCursor(0, 2);
lcd.print("Coba Lagi");
digitalWrite(LEDM, HIGH);
tone(BUZZER_PIN, 500, 500);
delay(5000);
digitalWrite(LEDM, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Selamat Datang!");
lcd.setCursor(0, 2);
lcd.print("Masukkan Password");
Serial.println("Password salah, harap dicoba lagi");
}
input_password = "";
} else {
input_password += key;
}
}
if (angle == 90 && (millis() - lastTime) > 3000) {
angle = 0;
servo.write(angle);
Serial.println("Menutup Palang");
digitalWrite(LEDH, LOW);
}
}