/********* 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: "Button Navigation"
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-06-11 08:57:20
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Lcd 20x4 i2c Switch case menu with submenu */
/* Button up Button down Button select */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include //https://github.com/marcoschwartz/LiquidCrystal_I2C
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void displayMenu(void);
void navigateMenu(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t UP_BUTTON_PushButton_PIN_D2 = 2;
const uint8_t DOWN_BUTTON_PushButton_PIN_D3 = 3;
const uint8_t ENTER_BUTTON_PushButton_PIN_D4 = 4;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
/****** MENU VARIABLES *****/
int menuIndex = 0;
int subMenuIndex = 0;
bool inSubMenu = false;
const char* mainMenu[] = {"Option 1", "Option 2", "Option 3"};
const char* subMenu1[] = {"Submenu 1-1", "Submenu 1-2"};
const char* subMenu2[] = {"Submenu 2-1", "Submenu 2-2"};
const char* subMenu3[] = {"Submenu 3-1", "Submenu 3-2"};
void setup(void)
{
// put your setup code here, to run once:
pinMode(UP_BUTTON_PushButton_PIN_D2, INPUT_PULLUP);
pinMode(DOWN_BUTTON_PushButton_PIN_D3, INPUT_PULLUP);
pinMode(ENTER_BUTTON_PushButton_PIN_D4, INPUT_PULLUP);
// Initialize the LCD
lcd.init();
lcd.backlight();
displayMenu();
}
void loop(void)
{
// put your main code here, to run repeatedly:
navigateMenu();
}
void displayMenu(void)
{
lcd.clear();
if (inSubMenu) {
lcd.setCursor(0, 0);
lcd.print(mainMenu[menuIndex]);
lcd.setCursor(0, 1);
if (menuIndex == 0) {
lcd.print(subMenu1[subMenuIndex]);
} else if (menuIndex == 1) {
lcd.print(subMenu2[subMenuIndex]);
} else if (menuIndex == 2) {
lcd.print(subMenu3[subMenuIndex]);
}
} else {
for (int i = 0; i < 3; i++) {
lcd.setCursor(0, i);
if (i == menuIndex) {
lcd.print("> ");
} else {
lcd.print(" ");
}
lcd.print(mainMenu[i]);
}
}
}
void navigateMenu(void)
{
if (digitalRead(UP_BUTTON_PushButton_PIN_D2) == LOW) {
delay(200); // debounce delay
if (inSubMenu) {
subMenuIndex = (subMenuIndex - 1 + 2) % 2; // wrap around submenu
} else {
menuIndex = (menuIndex - 1 + 3) % 3; // wrap around main menu
}
displayMenu();
}
if (digitalRead(DOWN_BUTTON_PushButton_PIN_D3) == LOW) {
delay(200); // debounce delay
if (inSubMenu) {
subMenuIndex = (subMenuIndex + 1) % 2; // wrap around submenu
} else {
menuIndex = (menuIndex + 1) % 3; // wrap around main menu
}
displayMenu();
}
if (digitalRead(ENTER_BUTTON_PushButton_PIN_D4) == LOW) {
delay(200); // debounce delay
if (inSubMenu) {
inSubMenu = false;
} else {
inSubMenu = true;
subMenuIndex = 0;
}
displayMenu();
}
}
/* END CODE */