Advertisement
pleasedontcode

"Button Navigation" rev_29

Jun 11th, 2024
426
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: "Button Navigation"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-11 08:57:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Lcd 20x4 i2c  Switch case menu with submenu */
  21.     /* Button up  Button down  Button select */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void displayMenu(void);
  32. void navigateMenu(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t UP_BUTTON_PushButton_PIN_D2    = 2;
  36. const uint8_t DOWN_BUTTON_PushButton_PIN_D3  = 3;
  37. const uint8_t ENTER_BUTTON_PushButton_PIN_D4 = 4;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display
  41.  
  42. /****** MENU VARIABLES *****/
  43. int menuIndex = 0;
  44. int subMenuIndex = 0;
  45. bool inSubMenu = false;
  46.  
  47. const char* mainMenu[] = {"Option 1", "Option 2", "Option 3"};
  48. const char* subMenu1[] = {"Submenu 1-1", "Submenu 1-2"};
  49. const char* subMenu2[] = {"Submenu 2-1", "Submenu 2-2"};
  50. const char* subMenu3[] = {"Submenu 3-1", "Submenu 3-2"};
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     pinMode(UP_BUTTON_PushButton_PIN_D2,    INPUT_PULLUP);
  56.     pinMode(DOWN_BUTTON_PushButton_PIN_D3,  INPUT_PULLUP);
  57.     pinMode(ENTER_BUTTON_PushButton_PIN_D4, INPUT_PULLUP);
  58.  
  59.     // Initialize the LCD
  60.     lcd.init();
  61.     lcd.backlight();
  62.     displayMenu();
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // put your main code here, to run repeatedly:
  68.     navigateMenu();
  69. }
  70.  
  71. void displayMenu(void)
  72. {
  73.     lcd.clear();
  74.     if (inSubMenu) {
  75.         lcd.setCursor(0, 0);
  76.         lcd.print(mainMenu[menuIndex]);
  77.         lcd.setCursor(0, 1);
  78.         if (menuIndex == 0) {
  79.             lcd.print(subMenu1[subMenuIndex]);
  80.         } else if (menuIndex == 1) {
  81.             lcd.print(subMenu2[subMenuIndex]);
  82.         } else if (menuIndex == 2) {
  83.             lcd.print(subMenu3[subMenuIndex]);
  84.         }
  85.     } else {
  86.         for (int i = 0; i < 3; i++) {
  87.             lcd.setCursor(0, i);
  88.             if (i == menuIndex) {
  89.                 lcd.print("> ");
  90.             } else {
  91.                 lcd.print("  ");
  92.             }
  93.             lcd.print(mainMenu[i]);
  94.         }
  95.     }
  96. }
  97.  
  98. void navigateMenu(void)
  99. {
  100.     if (digitalRead(UP_BUTTON_PushButton_PIN_D2) == LOW) {
  101.         delay(200); // debounce delay
  102.         if (inSubMenu) {
  103.             subMenuIndex = (subMenuIndex - 1 + 2) % 2; // wrap around submenu
  104.         } else {
  105.             menuIndex = (menuIndex - 1 + 3) % 3; // wrap around main menu
  106.         }
  107.         displayMenu();
  108.     }
  109.  
  110.     if (digitalRead(DOWN_BUTTON_PushButton_PIN_D3) == LOW) {
  111.         delay(200); // debounce delay
  112.         if (inSubMenu) {
  113.             subMenuIndex = (subMenuIndex + 1) % 2; // wrap around submenu
  114.         } else {
  115.             menuIndex = (menuIndex + 1) % 3; // wrap around main menu
  116.         }
  117.         displayMenu();
  118.     }
  119.  
  120.     if (digitalRead(ENTER_BUTTON_PushButton_PIN_D4) == LOW) {
  121.         delay(200); // debounce delay
  122.         if (inSubMenu) {
  123.             inSubMenu = false;
  124.         } else {
  125.             inSubMenu = true;
  126.             subMenuIndex = 0;
  127.         }
  128.         displayMenu();
  129.     }
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement