#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
uint8_t rowPins[ROWS] = {23, 19, 18, 5};
uint8_t colPins[COLS] = {17, 16, 4, 0};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String password = "1234";
String inputPassword = "";
bool isAuthenticated = false;
int currentPage = 0;
bool inMenu = true;
bool selectingLocker = false;
int lockerSelection = 0;
const int ledPins[9] = {32, 33, 25, 26, 27, 14, 12, 13, 2};
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Easy Clean Wash");
loadingAnimation();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Sandi:");
lcd.setCursor(0, 1);
lcd.blink();
// Initialize LED pins
for (int i = 0; i < 9; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
}
void loop() {
char key = keypad.getKey();
if (key) {
if (!isAuthenticated) {
if (key == 'A') {
confirmLogout();
} else {
handlePasswordInput(key);
}
} else {
if (selectingLocker) {
handleLockerSelection(key);
} else {
handleMenuNavigation(key);
}
}
}
}
void handlePasswordInput(char key) {
if (key == '#') {
if (inputPassword == password) {
isAuthenticated = true;
lcd.noBlink();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Akses Diterima");
loadingAnimation();
delay(1000); // Mengurangi delay agar lebih responsif
showMenu();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sandi Salah");
loadingAnimation();
delay(1000); // Mengurangi delay agar lebih responsif
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Sandi:");
lcd.blink();
}
inputPassword = "";
} else if (key == '*') {
if (inputPassword.length() > 0) {
inputPassword.remove(inputPassword.length() - 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Sandi:");
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
} else {
inputPassword += key;
lcd.setCursor(0, 1);
lcd.print(inputPassword);
}
}
void showMenu() {
lcd.clear();
lcd.noBlink();
lcd.noCursor();
inMenu = true;
selectingLocker = false;
displayMenuPage();
}
void handleMenuNavigation(char key) {
if (!isAuthenticated) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Masukkan Sandi");
return;
}
if (inMenu) {
if (key == 'D') {
if (currentPage < 3) currentPage++;
displayMenuPage();
} else if (key == 'C') {
if (currentPage > 0) currentPage--;
displayMenuPage();
} else if (key == 'B') {
showMenu();
} else if (key == '2') {
startLockerSelection();
} else {
displayMenu(key);
}
} else if (key == 'B') {
showMenu();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void startLockerSelection() {
lockerSelection = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pilih Loker:");
lcd.setCursor(0, 1);
lcd.print("(1-9)");
selectingLocker = true;
}
void handleLockerSelection(char key) {
lcd.noBlink();
if (key >= '1' && key <= '9') {
lockerSelection = key - '0';
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Loker ");
lcd.print(lockerSelection);
lcd.print(" Dipilih");
lcd.setCursor(0, 1);
} else if (key == '#') {
if (lockerSelection > 0) {
toggleLocker(lockerSelection);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Loker ");
lcd.print(lockerSelection);
lcd.print(" ");
lcd.print(isLockerOpen(lockerSelection) ? "Terbuka" : "Tertutup");
lcd.setCursor(0, 1);
delay(2000);
showMenu(); // Kembali ke menu utama setelah aksi selesai
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tidak ada loker");
lcd.setCursor(0, 1);
lcd.print("yang dipilih");
delay(2000);
startLockerSelection(); // Ulangi pilihan loker
}
} else if (key == '*') {
lockerSelection = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pilih Loker:");
lcd.setCursor(0, 1);
lcd.print("(1-9)");
} else if (key == 'B') {
showMenu();
} else if (key == 'C') {
startLockerSelection();
}
}
void toggleLocker(int locker) {
if (locker > 0 && locker <= 9) {
bool state = isLockerOpen(locker);
digitalWrite(ledPins[locker - 1], !state);
}
}
bool isLockerOpen(int locker) {
return digitalRead(ledPins[locker - 1]) == HIGH;
}
void openAllLockers() {
for (int i = 0; i < 9; i++) {
digitalWrite(ledPins[i], HIGH); // Nyalakan LED untuk semua loker
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Semua Loker");
lcd.setCursor(0, 1);
lcd.print("Terbuka");
delay(2000); // Tampilkan status selama 2 detik
}
void closeAllLockers() {
for (int i = 0; i < 9; i++) {
digitalWrite(ledPins[i], LOW); // Matikan LED untuk semua loker
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Semua Loker");
lcd.setCursor(0, 1);
lcd.print("Tertutup");
delay(2000); // Tampilkan status selama 2 detik
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void displayMenuPage() {
lcd.clear();
switch (currentPage) {
case 0:
lcd.setCursor(0, 0);
lcd.print("1.Cek Transaksi");
lcd.setCursor(0, 1);
lcd.print("2.Aksi Ke Loker");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("3.Open All Loker");
lcd.setCursor(0, 1);
lcd.print("4.Lock All Loker");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("5.Cek Timbangan");
lcd.setCursor(0, 1);
lcd.print("6.CekConecWIFI");
break;
case 3:
lcd.setCursor(0, 0);
lcd.print("7.GantiConecWIFI");
break;
}
}
void displayMenu(char key) {
lcd.clear();
inMenu = false;
switch (key) {
case '1':
lcd.setCursor(0, 0);
lcd.print("Menu 1 Dipilih");
break;
case '2':
startLockerSelection(); // Panggil fungsi untuk aksi ke loker tertentu
break;
case '3':
openAllLockers(); // Panggil fungsi untuk membuka semua loker
break;
case '4':
closeAllLockers(); // Panggil fungsi untuk menutup semua loker
break;
case '5':
lcd.setCursor(0, 0);
lcd.print("Menu 5 Dipilih");
break;
case '6':
lcd.setCursor(0, 0);
lcd.print("Menu 6 Dipilih");
break;
case '7':
lcd.setCursor(0, 0);
lcd.print("Menu 7 Dipilih");
break;
case 'A':
confirmLogout();
break;
default:
lcd.setCursor(0, 0);
lcd.print("Pilihan Salah");
delay(2000);
lcd.setCursor(0, 1);
lcd.print("B untuk Kembali");
while (keypad.getKey() != 'B');
showMenu();
break;
}
}
void confirmLogout() {
if (!isAuthenticated) {
return;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Yakin Logout?");
lcd.setCursor(0, 1);
lcd.print("B: Ya C: Tidak");
while (true) {
char confirm = keypad.getKey();
if (confirm == 'B') {
resetToInitialScreen();
break;
} else if (confirm == 'C') {
showMenu(); // Return to menu if logout is canceled
break;
}
}
}
void resetToInitialScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Logged Out");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Easy Clean Wash");
loadingAnimation();
lcd.clear();
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Masukkan Sandi:");
lcd.blink();
inputPassword = ""; // Reset input password
isAuthenticated = false; // Reset authentication status
inMenu = false; // Ensure menu state is reset
selectingLocker = false;
}
void loadingAnimation() {
lcd.setCursor(0, 1);
for (int i = 0; i < 16; i++) {
lcd.print(".");
delay(50); // Mengurangi delay agar lebih cepat
}
}