#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buzzerPin = 25;
const int ledPin = 26;
const byte ROWS = 4; //jumlah baris keypad
const byte COLS = 4; //jumlah kolom keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {14, 12, 19, 18}; //R1, R2, R3, R4
byte colPins[COLS] = {5, 4, 2, 15}; //C1, C2, C3, C4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char *password = "1234";
char enteredPassword[5];
int passwordIndex = 0;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Input password:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Tombol untuk memeriksa password
lcd.setCursor(0, 1);
lcd.print(" "); // Menghapus baris sebelumnya
lcd.setCursor(0, 1);
if (strcmp(enteredPassword, password) == 0) {
lcd.print("Access Accepted");
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
delay(100);
noTone(buzzerPin);
} else {
lcd.print("Invalid password");
digitalWrite(ledPin, LOW);
tone(buzzerPin, 500);
delay(500);
noTone(buzzerPin);
}
delay(2000);
lcd.clear();
lcd.setCursor(0, 0); // Tambahkan ini untuk kembali ke posisi awal pada baris pertama
lcd.print("Input password:");
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
} else if (key == '*') { // Tombol untuk menghapus layar
lcd.clear();
lcd.setCursor(0, 0); // Tambahkan ini untuk kembali ke posisi awal pada baris pertama
lcd.print("Input password:");
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
} else {
if (passwordIndex < 4) {
lcd.setCursor(passwordIndex, 1);
lcd.print('*');
enteredPassword[passwordIndex] = key;
passwordIndex++;
}
}
}
}