#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 3;
char Keys[4][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPin[4] = {2, 3, 4, 5};
byte colsPin[3] = {6, 7, 8};
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPin, colsPin, 4, 3);
int password = 1234; // Password yang benar
int inputPassword = 0; // Variabel untuk menyimpan input pengguna
int JumlahPercobaan = 0; // Menghitung jumlah percobaan
bool isEntering = false;
const int maxAttempts = 3; // Jumlah percobaan maksimum
const int buzzerPin = 9; // Pin untuk buzzer
const int servoPin = 10; // Pin untuk servo
void setup() {
myServo.attach(10); // Menghubungkan servo
pinMode(9, OUTPUT);
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Hidupkan lampu latar LCD
lcd.print(" Masukkan PIN:");
lcd.setCursor(0,1);
lcd.print(" 4 ANGKA");
myServo.write(0);
}
void playSuccessMelody() {
int melody[] = {493, 329, 493, 523, 440, 493};
int noteDurations[] = {200, 200, 200, 500, 200, 1000}; // Durasi nada (ms)
for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
tone(9, melody[thisNote], noteDurations[thisNote]);
delay(noteDurations[thisNote]); // Tunggu durasi nada
noTone(9); // Matikan nada
delay(50); // Tunggu sedikit sebelum nada berikutnya
}
}
void playErrorMelody() {
tone(9, 622);
delay(300);
tone(9, 587);
delay(300);
tone(9, 554);
delay(300);
tone(9, 523);
delay(1000);
noTone(9);
}
void lockoutMelody() {
for (byte i = 0; i < 10; i++){
tone(9, 660);
delay(400);
noTone(9);
delay(30);
tone(9, 860);
delay(400);
noTone(9);
delay(30);
i++;
if (i == 2){
noTone(9);
}
}
}
void loop() {
char key = customKeypad.getKey(); // Gunakan customKeypad
if (key) {
if (!isEntering) {
isEntering = true;
lcd.setCursor(0, 1);
lcd.print(" "); // Menghapus baris kedua
} if (key >= '0' && key <= '9') {
inputPassword = inputPassword * 10 + (key - '0'); // Membaca input
lcd.setCursor(0, 1);
lcd.print(inputPassword); // Tampilkan PIN yang dimasukkan
} else if (key == '*') { // Hapus semua input jika `*` ditekan
lcd.clear();
delay(50);
lcd.print("MENGULANG INPUT");
delay(1000);
lcd.clear();
delay(50);
inputPassword = 0;
isEntering = false;
lcd.print(" Masukkan PIN:");
lcd.setCursor(0,1);
lcd.print(" 4 ANGKA");
} else if (key == '#') { // Jika tombol '#' ditekan
if (inputPassword == password) {
lcd.clear();
lcd.print("Akses diterima!");
myServo.write(90); // Buka kunci
playSuccessMelody();// Mainkan nada setelah berhasil
delay(100); // Tunggu 2 detik
lcd.clear();
delay(100);
lcd.print(" Pintu Dibuka");
delay(5000);
lcd.clear();
delay(500);
lcd.print(" Pintu Kembali");
lcd.setCursor(0,1);
lcd.print(" Ditutup ");
myServo.write(0); // Kunci kembali
inputPassword = 0; // Reset input
JumlahPercobaan = 0; // Reset hitungan percobaan
delay(2000);
lcd.clear();
lcd.print(" Masukkan PIN:");
lcd.setCursor(0,1);
lcd.print(" 4 ANGKA");
isEntering = false;
} else {
JumlahPercobaan++;
lcd.clear();
lcd.print("Password salah!");
playErrorMelody(); // Mainkan nada saat password salah
delay(1000); // Tampilkan pesan selama 1 detik
lcd.clear();
lcd.print(" Masukkan PIN:");
lcd.setCursor(0,1);
lcd.print(" 4 ANGKA");
isEntering = false;
inputPassword = 0; // Reset input
if (JumlahPercobaan >= maxAttempts) {
lcd.clear();
lcd.print("Terlalu Banyak");
lcd.setCursor(0, 1);
lcd.print(" Percobaan!");
lockoutMelody();
lockoutMelody();
delay(2);
lcd.clear();
lockoutMelody();
delay(2);
lcd.print(" MEMANGGIL");
lcd.setCursor(0,1);
lcd.print(" SECURITY");
lockoutMelody();
lockoutMelody();
lcd.clear();
lcd.print(" Masukkan PIN:");
lcd.setCursor(0,1);
lcd.print(" 4 ANGKA");
isEntering = false;
JumlahPercobaan
= 0; // Reset hitungan percobaan
}
}
}
}
}