#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin untuk baris dan kolom keypad
const int rowPins[4] = {9, 8, 7, 6};
const int colPins[4] = {5, 4, 3, 2};
// Password yang ditetapkan
const char password[5] = "1234";
char input[5];
int inputIndex = 0;
void setup() {
// Inisialisasi pin baris sebagai output dan kolom sebagai input
for (int i = 0; i < 4; i++) {
pinMode(rowPins[i], OUTPUT);
pinMode(colPins[i], INPUT_PULLUP);
}
// Inisialisasi LCD
lcd.begin(16, 2);
lcd.backlight(); // Menyalakan backlight LCD
lcd.print("Isi Password:");
}
void loop() {
char key = getKey();
if (key != '\0') {
lcd.setCursor(inputIndex, 1);
lcd.print('*'); // Tampilkan '*' setiap kali tombol ditekan
input[inputIndex] = key;
inputIndex++;
if (inputIndex == 4) {
input[inputIndex] = '\0';
if (strcmp(input, password) == 0) {
lcd.clear();
lcd.print("Password Benar");
} else {
lcd.clear();
lcd.print("Password Salah");
}
delay(2000);
lcd.clear();
lcd.print("Isi Password:");
inputIndex = 0;
}
}
}
char getKey() {
for (int row = 0; row < 4; row++) {
digitalWrite(rowPins[row], LOW);
for (int col = 0; col < 4; col++) {
if (digitalRead(colPins[col]) == LOW) {
while (digitalRead(colPins[col]) == LOW); // Debounce
digitalWrite(rowPins[row], HIGH);
return keyMap(row, col);
}
}
digitalWrite(rowPins[row], HIGH);
}
return '\0';
}
char keyMap(int row, int col) {
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
return keys[row][col];
}