#include <Keypad.h>
#include <LiquidCrystal.h>
// === Setup LCD (16x2) ===
// RS -> 12, E -> 11, D4 -> 10, D5 -> A0, D6 -> A1, D7 -> A2
LiquidCrystal lcd(12, 11, 10, A0, A1, A2);
// === Setup Keypad 4x4 ===
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}; // R1..R4
byte colPins[COLS] = {5, 4, 3, 2}; // C1..C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// === Password ===
String password = "1234";
String input = "";
// === LED ===
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
// Setup LCD
lcd.begin(16, 2);
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
// Setup Serial Console
Serial.begin(9600);
Serial.println("System Ready - Enter Password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// cek password
if (input == password) {
lcd.clear();
lcd.print("Access Granted");
Serial.println("Access Granted ✅");
digitalWrite(ledPin, HIGH); // LED ON
delay(3000);
digitalWrite(ledPin, LOW); // LED OFF
lcd.clear();
lcd.print("Enter Password:");
} else {
lcd.clear();
lcd.print("Access Denied");
Serial.println("Access Denied ❌");
digitalWrite(ledPin, LOW);
delay(2000);
lcd.clear();
lcd.print("Enter Password:");
}
input = ""; // reset
lcd.setCursor(0, 1);
}
else if (key == '*') {
// reset input
input = "";
lcd.clear();
lcd.print("Enter Password:");
lcd.setCursor(0, 1);
Serial.println("Input Cleared");
}
else {
// input password
input += key;
lcd.print("*"); // tampil bintang
Serial.print("Key pressed: ");
Serial.println(key);
}
}
}