#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD dengan alamat 0x27 dan ukuran 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4; // Jumlah baris pada keypad
const byte COLS = 4; // Jumlah kolom pada keypad
// Pemetaan tombol pada keypad 4x4
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Hubungkan baris keypad ke pin Arduino
byte rowPins[ROWS] = {2, 3, 4, 5}; // Pin untuk baris: D2, D3, D4, D5
// Hubungkan kolom keypad ke pin Arduino
byte colPins[COLS] = {6, 7, 8, 9}; // Pin untuk kolom: D6, D7, D8, D9
// Membuat objek Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Servo untuk membuka/tutup pintu
Servo myServo;
// Password yang benar
String password = "1234";
String inputPassword = "";
void setup() {
Serial.begin(9600); // Memulai komunikasi serial
myServo.attach(10); // Servo terhubung ke pin D10
myServo.write(0); // Servo di posisi awal (pintu tertutup)
// Inisialisasi LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Masukkan Pass:");
Serial.println("Masukkan password:");
}
void loop() {
char key = keypad.getKey(); // Membaca input dari keypad
if (key) { // Jika ada tombol yang ditekan
Serial.print("Tombol ditekan: ");
Serial.println(key); // Tampilkan tombol yang ditekan di Serial Monitor
if (key == '#') { // Tombol '#' untuk memeriksa password
if (inputPassword == password) {
Serial.println("Password benar! Pintu terbuka.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password benar!");
lcd.setCursor(0, 1);
lcd.print("Pintu terbuka");
myServo.write(90); // Membuka pintu (putar servo ke 90 derajat)
delay(500); // Tahan pintu terbuka selama 5 detik
myServo.write(0); // Menutup kembali pintu
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Pass:");
} else {
Serial.println("Password salah! Coba lagi.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password salah!");
delay(200); // Tampilkan pesan selama 2 detik
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Pass:");
}
inputPassword = ""; // Reset input setelah mencoba password
}
else if (key == '*') { // Tombol '*' untuk menghapus input
inputPassword = "";
Serial.println("Input direset.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Input direset");
delay(100); // Tampilkan pesan reset selama 1 detik
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Pass:");
}
else {
inputPassword += key; // Tambahkan karakter ke inputPassword
lcd.setCursor(0, 1);
lcd.print(inputPassword); // Tampilkan input password pada baris kedua LCD
}
}
}