Advertisement
pleasedontcode

"Menu Navigation" rev_01

Jun 6th, 2024
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Menu Navigation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-07 03:33:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* lcd display i2c  20x4   button pin11 INPUT_PULLUP */
  21.     /* LOW menu up  button pin10 INPUT_PULLUP LOW menu */
  22.     /* enter  button pin12 INPUT_PULLUP   LOW  menu down */
  23.     /* startmenu  test1  test2  submenu test1  lol1  lol2 */
  24.     /* sub menu test2  ha1  ha2 */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h>  //https://github.com/evert-arias/EasyButton
  29. #include <Wire.h>
  30. #include <LiquidCrystal_I2C.h>  // Library for LCD
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void onUpButtonPressed(void);
  36. void onDownButtonPressed(void);
  37. void onEnterButtonPressed(void);
  38. void displayMenu(void);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t UP_BUTTON_PIN = 11;    // Button for menu up
  42. const uint8_t MENU_BUTTON_PIN = 10;  // Button for menu
  43. const uint8_t DOWN_BUTTON_PIN = 12;  // Button for menu down
  44.  
  45. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  46. EasyButton upButton(UP_BUTTON_PIN, 35, true, false);
  47. EasyButton menuButton(MENU_BUTTON_PIN, 35, true, false);
  48. EasyButton downButton(DOWN_BUTTON_PIN, 35, true, false);
  49. LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD address to 0x27 for a 20 chars and 4 line display
  50.  
  51. /****** MENU VARIABLES *****/
  52. int currentMenu = 0;
  53. const char* menuItems[] = {"Start Menu", "Test1", "Test2"};
  54. const char* submenuTest1[] = {"Lol1", "Lol2"};
  55. const char* submenuTest2[] = {"Ha1", "Ha2"};
  56. int currentSubMenu = 0;
  57. bool inSubMenu = false;
  58.  
  59. void setup(void) {
  60.   // Initialize serial communication
  61.   Serial.begin(115200);
  62.  
  63.   // Initialize LCD
  64.   lcd.begin();
  65.   lcd.backlight();
  66.  
  67.   // Initialize buttons
  68.   upButton.begin();
  69.   menuButton.begin();
  70.   downButton.begin();
  71.  
  72.   // Attach button callbacks
  73.   upButton.onPressed(onUpButtonPressed);
  74.   menuButton.onPressed(onEnterButtonPressed);
  75.   downButton.onPressed(onDownButtonPressed);
  76.  
  77.   // Display initial menu
  78.   displayMenu();
  79. }
  80.  
  81. void loop(void) {
  82.   // Read button states
  83.   upButton.read();
  84.   menuButton.read();
  85.   downButton.read();
  86. }
  87.  
  88. void onUpButtonPressed(void) {
  89.   if (inSubMenu) {
  90.     currentSubMenu = (currentSubMenu - 1 + 2) % 2;  // Wrap around submenu items
  91.   } else {
  92.     currentMenu = (currentMenu - 1 + 3) % 3;  // Wrap around main menu items
  93.   }
  94.   displayMenu();
  95. }
  96.  
  97. void onDownButtonPressed(void) {
  98.   if (inSubMenu) {
  99.     currentSubMenu = (currentSubMenu + 1) % 2;  // Wrap around submenu items
  100.   } else {
  101.     currentMenu = (currentMenu + 1) % 3;  // Wrap around main menu items
  102.   }
  103.   displayMenu();
  104. }
  105.  
  106. void onEnterButtonPressed(void) {
  107.   if (inSubMenu) {
  108.     inSubMenu = false;
  109.   } else {
  110.     if (currentMenu == 1 || currentMenu == 2) {
  111.       inSubMenu = true;
  112.       currentSubMenu = 0;
  113.     }
  114.   }
  115.   displayMenu();
  116. }
  117.  
  118. void displayMenu(void) {
  119.   lcd.clear();
  120.   if (inSubMenu) {
  121.     if (currentMenu == 1) {
  122.       lcd.print(submenuTest1[currentSubMenu]);
  123.     } else if (currentMenu == 2) {
  124.       lcd.print(submenuTest2[currentSubMenu]);
  125.     }
  126.   } else {
  127.     lcd.print(menuItems[currentMenu]);
  128.   }
  129.   Serial.print("Current Menu: ");
  130.   Serial.println(menuItems[currentMenu]);
  131.   if (inSubMenu) {
  132.     Serial.print("Current SubMenu: ");
  133.     if (currentMenu == 1) {
  134.       Serial.println(submenuTest1[currentSubMenu]);
  135.     } else if (currentMenu == 2) {
  136.       Serial.println(submenuTest2[currentSubMenu]);
  137.     }
  138.   }
  139. }
  140.  
  141. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement