#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Konfigurasi pin untuk relay
const int relayPin = 15; // Pin GPIO untuk relay
// Konfigurasi keypad
const byte ROWS = 4; // Empat baris
const byte COLS = 4; // Empat kolom
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {13, 12, 14, 27}; // Pin untuk baris
byte colPins[COLS] = {26, 25, 33, 32}; // Pin untuk kolom
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi password (contoh: "1234")
String password = "1234";
String inputPassword = "";
// Konfigurasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C 0x27, 16 kolom, 2 baris
void setup() {
Serial.begin(115200);
// Set pin relay sebagai output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Matikan relay di awal
// Inisialisasi keypad
Serial.println("Keypad siap!");
// Inisialisasi LCD
lcd.begin();
lcd.backlight();
lcd.print("Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
lcd.setCursor(0, 1);
if (key == '#') { // Tombol '#' untuk submit password
if (inputPassword == password) {
Serial.println("Password benar!");
lcd.print("Password benar! ");
digitalWrite(relayPin, HIGH); // Nyalakan relay
delay(5000); // Relay aktif selama 5 detik
digitalWrite(relayPin, LOW); // Matikan relay
lcd.clear();
lcd.print("Enter Password:");
} else {
Serial.println("Password salah!");
lcd.print("Password salah! ");
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
}
inputPassword = ""; // Reset input password
} else if (key == '*') { // Tombol '*' untuk reset input
inputPassword = "";
lcd.print(" "); // Clear line
} else {
inputPassword += key;
lcd.print(key);
}
}
}