#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define LED 13
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] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16, 2);
String password1 = "1234";
String password2 = "5678";
String password3 = "2468";
String keluar = "####";
String inputPassword;
void setup(){
lcd.init();
lcd.backlight();
pinMode(LED, OUTPUT);
}
void loop(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password 1:");
inputPassword = getPassword();
if(inputPassword == password1){
showPassword2();
} else {
accessDenied();
}
}
String getPassword(){
String password = "";
while(password.length() < 4){
char key = keypad.getKey();
if(key){
password += key;
lcd.print(key);
}
}
return password;
}
void showPassword2(){
lcd.clear();
lcd.print("Password 2:");
inputPassword = getPassword();
if(inputPassword == password2){
showPassword3();
} else {
accessDenied();
}
}
void showPassword3(){
lcd.clear();
lcd.print("Password 3:");
inputPassword = getPassword();
if(inputPassword == password3){
//while(1){
baca_ultra();
delay(2000);
//}
}
else {
accessDenied();
}
}
void accessDenied(){
lcd.clear();
lcd.print("Password Salah");
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
lcd.clear();
}
void baca_ultra(){
int cm = 0.0344/2 * readUltrasonicDistance(3, 2);
lcd.clear();
lcd.print("Jarak: ");
lcd.print(cm);
lcd.println(" cm");
delay(100);
}
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}