#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change the I2C address if needed
const int BUTTON_UP = 15; // Pin for the "Up" button
const int BUTTON_DOWN = 18; // Pin for the "Down" button
const int BUTTON_SELECT = 19; // Pin for the "Select" button
enum Screen {
START,
MAIN_MENU,
SUB_MENU,
};
Screen currentScreen = START;
int selectedOption = 0;
bool submenuActive = false;
void setup() {
lcd.init();
lcd.backlight();
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("START");
delay(2000);
lcd.clear();
}
void loop() {
handleButtons();
switch (currentScreen) {
case START:
displayStartScreen();
break;
case MAIN_MENU:
displayMainMenu();
break;
case SUB_MENU:
displaySubMenu();
break;
}
}
void handleButtons() {
static unsigned long lastDebounceTime = 0;
static unsigned long debounceDelay = 50;
int readingUp = digitalRead(BUTTON_UP);
int readingDown = digitalRead(BUTTON_DOWN);
int readingSelect = digitalRead(BUTTON_SELECT);
if (millis() - lastDebounceTime > debounceDelay) {
if (readingUp == LOW) {
if (currentScreen == MAIN_MENU || (currentScreen == SUB_MENU && submenuActive)) {
selectedOption = (selectedOption + 1) % (submenuActive ? 6 : 5);
}
lastDebounceTime = millis();
} else if (readingDown == LOW) {
if (currentScreen == MAIN_MENU || (currentScreen == SUB_MENU && submenuActive)) {
selectedOption = (selectedOption + (submenuActive ? 5 : 4)) % (submenuActive ? 6 : 5);
}
lastDebounceTime = millis();
} else if (readingSelect == LOW) {
if (currentScreen == START) {
currentScreen = MAIN_MENU;
} else if (currentScreen == MAIN_MENU) {
if (selectedOption == 4) {
currentScreen = START;
} else {
if (selectedOption == 3) {
submenuActive = true;
selectedOption = 0;
currentScreen = SUB_MENU;
} else {
// Handle actions for other options in the main menu
}
}
} else if (currentScreen == SUB_MENU) {
// Handle actions for options in the sub-menu
}
lastDebounceTime = millis();
}
}
}
void displayStartScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("START");
}
void displayMainMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Option 1");
lcd.setCursor(0, 1);
lcd.print("Option 2");
lcd.setCursor(0, 2);
lcd.print("Option 3");
lcd.setCursor(0, 3);
lcd.print("Option 4");
lcd.setCursor(0, 4);
lcd.print("Return");
}
void displaySubMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sub-option 1");
lcd.setCursor(0, 1);
lcd.print("Sub-option 2");
lcd.setCursor(0, 2);
lcd.print("Sub-option 3");
lcd.setCursor(0, 3);
lcd.print("Sub-option 4");
lcd.setCursor(0, 4);
lcd.print("Sub-option 5");
lcd.setCursor(0, 5);
lcd.print("Sub-option 6");
}