#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t Cpin[COLS] = {7, 6, 5, 4};
uint8_t Rpin[ROWS] = {12, 11, 10, 9};
Keypad keypad = Keypad(makeKeymap(keys), Rpin, Cpin, ROWS, COLS);
String input = "";
bool servostate = false;
uint8_t maxpw = 6;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
lcd.home();
lcd.print("PW Tester");
delay(2000);
lcd.clear();
servo.attach(3);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 0);
lcd.print("input : ");
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '#') {
if (input == "142536") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PW benar");
delay(1000);
lcd.clear();
servostate = true;
input = "";
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PW salah!!");
input = "";
delay(500);
lcd.clear();
servostate = false;
}
} else if (input.length() < maxpw && key > '0' && key <= '9') {
input += key;
lcd.setCursor(8, 0);
lcd.print(input);
} else if (input.length() > 0){
input = input.substring(0, input.length() - 1);
lcd.setCursor(8 + input.length(), 0);
lcd.print(" ");
}
}
if (servostate) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("membuka kunci");
lcd.setCursor(0, 1);
lcd.print("selama 3 detik");
servo.write(180);
delay(1500);
lcd.clear();
delay(1500);
lcd.clear();
servostate = false;
} else {
servo.write(0);
}
}