#include <Keypad.h> //Library keypad
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo Servo1; //Library LCD I2C
LiquidCrystal_I2C lcd(0x27,16,2); //Alamat I2C
const byte ROWS = 4; //Jumlah baris keypad
const byte COLS = 4; //Jumlah kolom keypad
char Keys[ROWS][COLS] = { //Membuat array keypad
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //Deklarasi pin keypad
byte colPins[COLS] = {6, 7, 8, 9};
Keypad customKeypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); //Masukkan info keypad pada library
#define ledpin1 10
#define ledpin2 12 //LED pada pin D13
#define naik 177
#define turun 90
char customKey; //Variabel penampung input keypad
int number = 0; //Variabel penampung nilai angka
int password = 2018; //Password
void setup() {
lcd.init (); //Mulai LCD
lcd.setBacklight(HIGH); //Nyalakan backlight
pinMode(ledpin1,OUTPUT);
pinMode(ledpin2,OUTPUT);
Servo1.attach(11); //Setting I/O
}
void loop() {
lcd.setCursor(0,0);
lcd.print("Input Password"); //Tampilan pada layar LCD
customKey = customKeypad.getKey(); //Baca input keypad
//------------Prosedur jika input berupa angka------------//
switch(customKey){
case '0' ... '9':
lcd.setCursor(0,1);
number = number * 10 + (customKey - '0');
lcd.print(number);
break;
//------------Jika input '#' maka cek password------------//
case 'A':
if(number == password){ //Jika password benar, maka
lcd.setCursor(0,1);
lcd.print("bener kali"); //Tampilan LCD
Servo1.write (naik);
digitalWrite(ledpin1, HIGH); //LED Menyala
delay(2000);
Servo1.write(turun);
digitalWrite(ledpin1, LOW);
number = 0;
lcd.clear();
}
else{ //Jika salah, maka
lcd.setCursor(0,1);
lcd.print("salah kali"); //Tampilan LCD
Servo1.write (turun);
digitalWrite(ledpin2, HIGH); //LED Menyala
delay(2000);
Servo1.write(turun);
digitalWrite(ledpin2, LOW);
delay(2000);
number = 0;
lcd.clear();
}
break;
//------------Jika input '*' maka hapus tampilan------------//
case '*':
number = 0;
lcd.clear();
break;
}
}