#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Konfigurasi Keypad
const uint8_t ROWS = 4; // empat baris
const uint8_t COLS = 4; // empat kolom
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
uint8_t rowPins[ROWS] = {18, 5, 17, 16}; // pin-pin sambungan ke baris keypad
uint8_t colPins[COLS] = {4, 0, 2, 15}; // pin-pin sambungan ke kolom keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi LED
const int ledHijau = 13; // Pin LED Hijau
const int ledMerah = 12; // Pin LED Merah
// Password yang diharapkan
const char correctPassword[4] = {'1', '2', '3', '4'};
char inputPassword[4]; // Array untuk menyimpan input password
int passwordIndex = 0; // Index untuk input password
unsigned long lastKeyPressTime = 0; // Waktu terakhir tombol ditekan
const unsigned long passwordTimeout = 10000; // Timeout untuk input password (10 detik)
const int maxAttempts = 3; // Maksimal percobaan password
int attemptCount = 0; // Jumlah percobaan saat ini
unsigned long ledResetTime = 0; // Waktu untuk reset LED
void setup() {
// Inisialisasi komunikasi serial
Serial.begin(9600);
// Inisialisasi LCD
lcd.init();
lcd.backlight();
// Inisialisasi LED
pinMode(ledHijau, OUTPUT);
pinMode(ledMerah, OUTPUT);
// Menampilkan pesan awal di LCD
lcd.print("Masukkan Pass:");
}
void loop() {
if (millis() - lastKeyPressTime > passwordTimeout && passwordIndex > 0) {
// Reset input password karena timeout
passwordIndex = 0;
lcd.clear();
lcd.print("Timeout, ulangi:");
}
char key = keypad.getKey();
// Jika tombol ditekan
if (key) {
lastKeyPressTime = millis(); // Perbarui waktu terakhir tombol ditekan
// Jika tombol '#' ditekan, cek password
if (key == '#') {
if (checkPassword(inputPassword)) {
lcd.clear();
lcd.print("Pintu Terbuka");
digitalWrite(ledHijau, HIGH);
digitalWrite(ledMerah, LOW);
attemptCount = 0; // Reset jumlah percobaan
ledResetTime = millis(); // Set waktu untuk reset LED
} else {
attemptCount++;
if (attemptCount >= maxAttempts) {
lcd.clear();
lcd.print("Kunci Terblokir");
delay(10000); // Kunci selama 10 detik
attemptCount = 0; // Reset jumlah percobaan
} else {
lcd.clear();
lcd.print("Pass Salah");
digitalWrite(ledHijau, LOW);
digitalWrite(ledMerah, HIGH);
ledResetTime = millis(); // Set waktu untuk reset LED
}
// Reset input password
passwordIndex = 0;
}
} else if (key == '*') {
// Reset input password
passwordIndex = 0;
lcd.clear();
lcd.print("Masukkan Pass:");
} else {
// Tambahkan key ke input password
inputPassword[passwordIndex] = key;
passwordIndex++;
lcd.setCursor(passwordIndex, 1);
lcd.print('*'); // Tampilkan '*' untuk setiap digit
}
}
// Reset LED setelah beberapa detik
if (millis() - ledResetTime > 5000 && (digitalRead(ledHijau) == HIGH || digitalRead(ledMerah) == HIGH)) {
digitalWrite(ledHijau, LOW);
digitalWrite(ledMerah, HIGH);
}
}
// Fungsi untuk memeriksa password
bool checkPassword(char input[]) {
for (int i = 0; i < 4; i++) {
if (input[i] != correctPassword[i]) {
return false; // Password salah
}
}
return true; // Password benar
}