CCCPaste

"Menu Navigation" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Menu Navigation"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-07 03:33:07

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd display i2c  20x4   button pin11 INPUT_PULLUP */
	/* LOW menu up  button pin10 INPUT_PULLUP LOW menu */
	/* enter  button pin12 INPUT_PULLUP   LOW  menu down */
	/* startmenu  test1  test2  submenu test1  lol1  lol2 */
	/* sub menu test2  ha1  ha2 */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   //https://github.com/evert-arias/EasyButton
#include 
#include   // Library for LCD

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onUpButtonPressed(void);
void onDownButtonPressed(void);
void onEnterButtonPressed(void);
void displayMenu(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t UP_BUTTON_PIN = 11;    // Button for menu up
const uint8_t MENU_BUTTON_PIN = 10;  // Button for menu
const uint8_t DOWN_BUTTON_PIN = 12;  // Button for menu down

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton upButton(UP_BUTTON_PIN, 35, true, false);
EasyButton menuButton(MENU_BUTTON_PIN, 35, true, false);
EasyButton downButton(DOWN_BUTTON_PIN, 35, true, false);
LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD address to 0x27 for a 20 chars and 4 line display

/****** MENU VARIABLES *****/
int currentMenu = 0;
const char* menuItems[] = {"Start Menu", "Test1", "Test2"};
const char* submenuTest1[] = {"Lol1", "Lol2"};
const char* submenuTest2[] = {"Ha1", "Ha2"};
int currentSubMenu = 0;
bool inSubMenu = false;

void setup(void) {
  // Initialize serial communication
  Serial.begin(115200);

  // Initialize LCD
  lcd.begin();
  lcd.backlight();

  // Initialize buttons
  upButton.begin();
  menuButton.begin();
  downButton.begin();

  // Attach button callbacks
  upButton.onPressed(onUpButtonPressed);
  menuButton.onPressed(onEnterButtonPressed);
  downButton.onPressed(onDownButtonPressed);

  // Display initial menu
  displayMenu();
}

void loop(void) {
  // Read button states
  upButton.read();
  menuButton.read();
  downButton.read();
}

void onUpButtonPressed(void) {
  if (inSubMenu) {
    currentSubMenu = (currentSubMenu - 1 + 2) % 2;  // Wrap around submenu items
  } else {
    currentMenu = (currentMenu - 1 + 3) % 3;  // Wrap around main menu items
  }
  displayMenu();
}

void onDownButtonPressed(void) {
  if (inSubMenu) {
    currentSubMenu = (currentSubMenu + 1) % 2;  // Wrap around submenu items
  } else {
    currentMenu = (currentMenu + 1) % 3;  // Wrap around main menu items
  }
  displayMenu();
}

void onEnterButtonPressed(void) {
  if (inSubMenu) {
    inSubMenu = false;
  } else {
    if (currentMenu == 1 || currentMenu == 2) {
      inSubMenu = true;
      currentSubMenu = 0;
    }
  }
  displayMenu();
}

void displayMenu(void) {
  lcd.clear();
  if (inSubMenu) {
    if (currentMenu == 1) {
      lcd.print(submenuTest1[currentSubMenu]);
    } else if (currentMenu == 2) {
      lcd.print(submenuTest2[currentSubMenu]);
    }
  } else {
    lcd.print(menuItems[currentMenu]);
  }
  Serial.print("Current Menu: ");
  Serial.println(menuItems[currentMenu]);
  if (inSubMenu) {
    Serial.print("Current SubMenu: ");
    if (currentMenu == 1) {
      Serial.println(submenuTest1[currentSubMenu]);
    } else if (currentMenu == 2) {
      Serial.println(submenuTest2[currentSubMenu]);
    }
  }
}

/* END CODE */