#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi servo motor
Servo myServo;
// Inisialisasi LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C biasanya 0x27 untuk LCD
// Konfigurasi keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Pin baris pada Arduino
byte colPins[COLS] = {5, 4, 3, 2}; // Pin kolom pada Arduino
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Kode rahasia untuk membuka kunci
String password = "1234";
String inputPassword = "";
// Pin untuk Buzzer
const int buzzerPin = 11;
void setup() {
// Mengatur pin LCD
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Mengatur pin servo motor
myServo.attach(10); // Pin sinyal servo
myServo.write(0); // Posisi awal servo (pintu tertutup)
// Mengatur pin buzzer
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#') {
if (inputPassword == password) {
lcd.setCursor(0, 1);
lcd.print("Kunci Terbuka! ");
myServo.write(90);
delay(1000);
myServo.write(0);
} else {
lcd.setCursor(0, 1);
lcd.print("Password Salah! ");
tone(buzzerPin, 100);
delay(500);
noTone(buzzerPin);
}
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Hapus setelah pengecekan
} else if (key == '*') {
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" ");
} else {
inputPassword += key;
lcd.setCursor(inputPassword.length() - 1, 1);
lcd.print(key); // Tampilkan input karakter pada LCD
}
}
}