#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27, 20 columns, 4 rows
int buttonUp = 6; // Digital pin for up button
int buttonDown = 7; // Digital pin for down button
int buttonSelect = 8; // Digital pin for select button
int buttonBack = 9; // Digital pin for back button
int menuIndex = 0;
int maxMenuIndex = 3; // Total number of menu items - 1
bool inSubMenu = false;
void setup() {
Wire.begin();
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
pinMode(buttonSelect, INPUT_PULLUP);
pinMode(buttonBack, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Menu:");
}
void loop() {
if (!inSubMenu) {
for (int i = 0; i < 4; i++) {
lcd.setCursor(0, i);
if (menuIndex == i) {
lcd.print("~ ");
} else {
lcd.print(" ");
}
switch (i) {
case 0:
lcd.print("Solar Setting ");
break;
case 1:
lcd.print("Battery Setting ");
break;
case 2:
lcd.print("Output Setting ");
break;
case 3:
lcd.print("Freq Setting ");
break;
}
}
} else {
// Display voltage, current, and power readings for option 1 sub-menu
float voltage = 12.3; // Replace with actual voltage reading
float current = 1.5; // Replace with actual current reading
float power = voltage * current;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage);
lcd.print("V");
lcd.setCursor(0, 1);
lcd.print("Current: ");
lcd.print(current);
lcd.print("A");
lcd.setCursor(0, 2);
lcd.print("Power: ");
lcd.print(power);
lcd.print("W");
lcd.setCursor(0, 3);
lcd.print("Press SELECT to return");
}
if (digitalRead(buttonUp) == LOW) {
menuIndex = (menuIndex - 1 + maxMenuIndex + 1) % (maxMenuIndex + 1);
delay(200); // Button debounce
}
if (digitalRead(buttonDown) == LOW) {
menuIndex = (menuIndex + 1) % (maxMenuIndex + 1);
delay(200); // Button debounce
}
if (digitalRead(buttonSelect) == LOW) {
if (!inSubMenu && menuIndex == 0) {
// Enter option 1 sub-menu
inSubMenu = true;
} else if (inSubMenu) {
// Exit option 1 sub-menu
inSubMenu = false;
} else {
// Perform action based on selected menu item
// For simplicity, just print the selected menu index
lcd.clear();
lcd.print("Selected: ");
lcd.print(menuIndex + 1);
delay(1000); // Display for 1 second
lcd.clear();
}
delay(200); // Button debounce
}
if (digitalRead(buttonBack) == LOW) {
// Return to main menu
menuIndex = 0;
inSubMenu = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Menu:");
delay(200); // Button debounce
}
}