#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C bisa berbeda, sesuaikan dengan modul Anda
// Inisialisasi 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}; // Pin baris keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Pin kolom keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variabel global
int menuIndex = 0; // Indeks menu saat ini
String inputBuffer = ""; // Buffer untuk input data
String menu[] = {"Menu 1", "Menu 2", "Input", "About"};
int totalMenus = sizeof(menu) / sizeof(menu[0]);
void setup() {
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Welcome!");
delay(2000);
showMenu();
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case 'A':
// Back
if (menuIndex > 0) {
menuIndex--;
}
showMenu();
break;
case 'B':
// Enter
if (menu[menuIndex] == "Input") {
handleInput();
} else {
lcd.clear();
lcd.print("Selected: ");
lcd.setCursor(0, 1);
lcd.print(menu[menuIndex]);
delay(2000);
showMenu();
}
break;
case 'C':
// Up
menuIndex = (menuIndex > 0) ? menuIndex - 1 : totalMenus - 1;
showMenu();
break;
case 'D':
// Down
menuIndex = (menuIndex < totalMenus - 1) ? menuIndex + 1 : 0;
showMenu();
break;
case '#':
// Titik
if (menu[menuIndex] == "Input") {
inputBuffer += ".";
lcd.setCursor(0, 1);
lcd.print(inputBuffer);
}
break;
case '*':
// Reset / Clear input
if (menu[menuIndex] == "Input") {
inputBuffer = "";
lcd.setCursor(0, 1);
lcd.print(" "); // Clear line
lcd.setCursor(0, 1);
lcd.print(inputBuffer);
}
break;
default:
// Handle angka atau karakter lain
if (menu[menuIndex] == "Input" && isDigit(key)) {
inputBuffer += key;
lcd.setCursor(0, 1);
lcd.print(inputBuffer);
}
break;
}
}
}
void showMenu() {
lcd.clear();
for (int i = 0; i < totalMenus; i++) {
lcd.setCursor(0, i == menuIndex ? 1 : 0);
if (i == menuIndex) {
lcd.print(">"); // Indikator pilihan
lcd.setCursor(1, 1);
lcd.print(menu[i]);
} else {
lcd.setCursor(1, 0);
lcd.print(" ");
lcd.print(menu[i]);
}
}
}
void handleInput() {
lcd.clear();
lcd.print("Enter value:");
inputBuffer = "";
lcd.setCursor(0, 1);
}