#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for I2C LCD
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//Mengatur jumlah baris dan kolom pada keypad
const int ROWS = 4;
const int COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17}; // Pins baris keypad
byte colPins[COLS] = {16, 4, 0, 2}; // Pins kolom keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int greenLedPin = 26; // Pin untuk LED hijau
const int redLedPin = 27; // Pin untuk LED merah
const String password = "7890"; // Password yang diharapkan
void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Masukkan Password:");
}
String enteredPassword = ""; // Variabel untuk menyimpan karakter yang dimasukkan
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') { // Menghapus karakter
if (enteredPassword.length() > 0) {
enteredPassword.remove(enteredPassword.length() - 1);
lcd.setCursor(enteredPassword.length(), 1);
lcd.print(" ");
}
} else if (key == '#') { // Memverifikasi password
if (enteredPassword == password) {
lcd.clear();
lcd.print("Password Benar!");
digitalWrite(greenLedPin, HIGH);
delay(2000);
digitalWrite(greenLedPin, LOW);
lcd.clear();
lcd.print("Masukkan Password:");
enteredPassword = "";
} else {
lcd.clear();
lcd.print("Password Salah!");
digitalWrite(redLedPin, HIGH);
delay(2000);
digitalWrite(redLedPin, LOW);
lcd.clear();
lcd.print("Masukkan Password:");
enteredPassword = "";
}
} else { // Menambahkan karakter
enteredPassword += key;
lcd.setCursor(enteredPassword.length() - 1, 1);
lcd.print("*");
}
}
}