// Keypad
#include <Keypad.h>
// LCD
#include <LiquidCrystal_I2C.h>
// Servo
#include <Servo.h>
// Deklarasi jumlah baris dan kolom pada keypad
const byte ROW = 4;
const byte COL = 4;
// Deklarasi posisi angka, huruf & simbol pada keypad
const char arr[ROW][COL] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// Deklarasi pin keypad yang terhubung dengan Arduino
byte pinCol[COL] = { 5, 4, 3, 2 };
byte pinRow[ROW] = { 9, 8, 7, 6 };
Keypad matriks = Keypad(makeKeymap(arr), pinRow, pinCol, ROW, COL);
// lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo
Servo myservo;
int servoPin = 10;
// Password yang diinginkan
const String password = "0987654";
// Variabel untuk menyimpan password yang dimasukkan
String inputPassword = "";
// Tekan apa saja untuk memasukkan password
// Jika inputan lebih dari 7 karakter, maka karakter akan kembali dari awal
void setup() {
Serial.begin(9600);
lcd.init();
myservo.attach(servoPin);
}
void loop() {
static int currentRow = 1;
static int currentCol = 0;
char button = matriks.getKey();
lcd.setCursor(0,0);
lcd.print("Input password:");
if (button != NO_KEY) {
Serial.println(button);
lcd.setCursor(currentCol, currentRow);
lcd.print(button);
currentCol++;
if(currentCol > 7) {
currentCol = 0;
lcd.clear();
if (inputPassword.length() == 7) {
if (inputPassword == password) {
Serial.println("Password Benar!");
myservo.write(180);
delay(1000);
myservo.write(90);
} else {
Serial.println("Password Salah!");
}
inputPassword = "";
}
} else {
inputPassword += button;
}
}
}