#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
// Inisialisasi LCD 2004 I2C dengan alamat 0x27
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Inisialisasi Keypad
const byte ROWS = 4; // Empat baris
const byte COLS = 4; // Empat kolom
// Definisi layout tombol keypad 4x4
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Pin yang terhubung ke baris dan kolom keypad
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Membuat keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variabel untuk password
String inputPassword = "";
String savedPassword = "";
int maxPasswordLength = 4; // Panjang password maksimal
// Alamat EEPROM untuk menyimpan password
int EEPROM_Address = 0;
// Fungsi untuk membaca password dari EEPROM
String readPasswordFromEEPROM() {
String password = "";
for (int i = 0; i < maxPasswordLength; i++) {
char storedChar = EEPROM.read(EEPROM_Address + i);
if (storedChar != 0xFF) {
password += storedChar;
}
}
return password;
}
// Fungsi untuk menyimpan password ke EEPROM
void savePasswordToEEPROM(String password) {
for (int i = 0; i < maxPasswordLength; i++) {
EEPROM.write(EEPROM_Address + i, password[i]);
}
}
// Fungsi setup
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Baca password yang disimpan di EEPROM
savedPassword = readPasswordFromEEPROM();
if (savedPassword == "") {
lcd.setCursor(0, 1);
lcd.print("Set Default Pass: 1234");
Serial.println(savedPassword);
delay(2000);
// Simpan password default jika belum ada password tersimpan
// savePasswordToEEPROM("1234");
// savedPassword = "1234";
}
}
// Fungsi loop
void loop() {
char key = keypad.getKey();
if (key) {
// Jika tombol '*' ditekan, reset input password
if (key == '*') {
inputPassword = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Mengosongkan baris
lcd.setCursor(0, 1);
lcd.print("Input Cleared");
delay(1000);
lcd.setCursor(0, 1);
lcd.print(" "); // Mengosongkan baris
}
// Jika tombol '#' ditekan, verifikasi password
else if (key == '#') {
lcd.setCursor(0, 1);
if (inputPassword == savedPassword) {
lcd.print("Password Accepted");
} else {
lcd.print("Wrong Password");
}
delay(2000);
lcd.setCursor(0, 1);
lcd.print(" "); // Mengosongkan baris
inputPassword = ""; // Reset password setelah verifikasi
}
// Jika tombol 'A' ditekan, simpan password baru
else if (key == 'A') {
if (inputPassword.length() == maxPasswordLength) {
lcd.setCursor(0, 1);
lcd.print("Saving New Pass");
savePasswordToEEPROM(inputPassword);
savedPassword = inputPassword;
delay(2000);
lcd.setCursor(0, 1);
lcd.print("New Pass Saved");
delay(2000);
lcd.setCursor(0, 1);
lcd.print(" "); // Mengosongkan baris
inputPassword = ""; // Reset input setelah menyimpan
} else {
lcd.setCursor(0, 1);
lcd.print("Invalid Length");
delay(2000);
lcd.setCursor(0, 1);
lcd.print(" ");
}
}
// Jika tombol selain '*' dan '#' ditekan, simpan ke input password
else {
if (inputPassword.length() < maxPasswordLength) {
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword); // Tampilkan password yang dimasukkan
}
}
}
}