#include <ESP32Servo.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo myservo;
const int BARIS = 4;
const int KOLOM = 4;
char keys[BARIS] [KOLOM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[BARIS] = {14,12,19,18};//connect to the row pinouts of the keypad
byte colPins[KOLOM] = {5,4,2,15};
Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, BARIS, KOLOM);
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 1270; //Password
#define buzzer 25
void setup() {
// put your setup code here, to run once;
Serial.begin(9600);
myservo.attach(15);
lcd.init();
lcd.backlight();
pinMode(26, OUTPUT);
}
void loop() {
lcd.setCursor(0,0);
lcd.print("input password");
customKey = keypad.getKey();
switch(customKey){
case'0' ... '9':
lcd.setCursor(0,1);
number = number * 10 + (customKey - '0');
lcd.print(number);
Serial.print(customKey);
Serial.print(",");
Serial.println(number);
break;
case'#':
//jika password benar, maka
if(number == password){
digitalWrite(26, HIGH); // LED menyala
lcd.setCursor(0,1);
lcd.print("Password Benar"); //Tampilan LCD
myservo.write(0);
//Reset variabel number
number = 0;
delay(5000);
lcd.clear(); //Menghapus tampilan LCD
}
//jika password salah, maka
else{
lcd.setCursor(0,1);
lcd.print("Password Salah"); //Tampilan LCD
digitalWrite(26, LOW); // LED mati
tone(buzzer, 250, 500); //PIN, FREKUENSI, WAKTU (durasi suara)
myservo.write(90);
//Reset variabel number
number = 0;
delay(5000);
lcd.clear(); //Menghapus tampilan LCD
}
break;
//............Jika input '*' maka hapus tampilan............//
case '*':
number = 0; //Reset variabel number
lcd.clear(); //Menghapus tampilan LCD
break;
}
}