Advertisement
pleasedontcode

"Menu Navigation" rev_05

Jun 6th, 2024
355
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-07 04:03:28
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* lcd display i2c  20x4  button pin11 UPLOW menu up */
  21.     /* button pin10 UP LOW menu enter  button pin12UP LOW */
  22.     /* menu down  menu  Temperatur->MaxTemp->code */
  23.     /* ->MinTeamp->code  Durchfluss(Flow)->MaxFlow->code */
  24.     /* ->MinTeamp->code */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  30. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void onUpButtonPressed();
  36. void onDownButtonPressed();
  37. void onEnterButtonPressed();
  38. void displayMenu();
  39. void displaySubMenu(const char* title, const char* code);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t UP_BUTTON_PIN = 11;
  43. const uint8_t DOWN_BUTTON_PIN = 12;
  44. const uint8_t ENTER_BUTTON_PIN = 10;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. EasyButton upButton(UP_BUTTON_PIN);
  48. EasyButton downButton(UP_BUTTON_PIN);
  49. EasyButton enterButton(UP_BUTTON_PIN);
  50.  
  51. // Initialize the LCD object with the I2C address (0x27), number of columns (20), and number of rows (4)
  52. LiquidCrystal_I2C lcd(0x27, 20, 4);
  53.  
  54. // Menu variables
  55. int menuIndex = 0;
  56. const char* menuItems[] = {"Temperatur", "Durchfluss(Flow)"};
  57. const char* subMenuItems[][2] = {{"MaxTemp", "MinTemp"}, {"MaxFlow", "MinFlow"}};
  58. const char* subMenuCodes[][2] = {{"code1", "code2"}, {"code3", "code4"}};
  59. int subMenuIndex = 0;
  60. bool inSubMenu = false;
  61.  
  62. void setup(void) {
  63.   // Initialize Serial for debugging purposes
  64.   Serial.begin(115200);
  65.   Serial.println();
  66.   Serial.println(">>> EasyButton multiple buttons example <<<");
  67.  
  68.   // Initialize the buttons
  69.   upButton.begin();
  70.   downButton.begin();
  71.   enterButton.begin();
  72.  
  73.   // Add the callback functions to be called when the buttons are pressed
  74.   upButton.onPressed(onUpButtonPressed);
  75.   downButton.onPressed(onDownButtonPressed);
  76.   enterButton.onPressed(onEnterButtonPressed);
  77.  
  78.   // Initialize the LCD
  79.   lcd.init();
  80.   lcd.backlight();
  81.   displayMenu();
  82. }
  83.  
  84. void loop(void) {
  85.   // Continuously read the status of the buttons
  86.   upButton.read();
  87.   downButton.read();
  88.   enterButton.read();
  89. }
  90.  
  91. // Callback function to be called when the UP button is pressed
  92. void onUpButtonPressed() {
  93.   if (inSubMenu) {
  94.     subMenuIndex = (subMenuIndex - 1 + 2) % 2; // Move up in the sub-menu
  95.     displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  96.   } else {
  97.     menuIndex = (menuIndex - 1 + 2) % 2; // Move up in the main menu
  98.     displayMenu();
  99.   }
  100.   Serial.println("Up button pressed");
  101. }
  102.  
  103. // Callback function to be called when the DOWN button is pressed
  104. void onDownButtonPressed() {
  105.   if (inSubMenu) {
  106.     subMenuIndex = (subMenuIndex + 1) % 2; // Move down in the sub-menu
  107.     displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  108.   } else {
  109.     menuIndex = (menuIndex + 1) % 2; // Move down in the main menu
  110.     displayMenu();
  111.   }
  112.   Serial.println("Down button pressed");
  113. }
  114.  
  115. // Callback function to be called when the ENTER button is pressed
  116. void onEnterButtonPressed() {
  117.   if (inSubMenu) {
  118.     // Perform action based on the selected sub-menu item
  119.     Serial.print("Selected: ");
  120.     Serial.println(subMenuCodes[menuIndex][subMenuIndex]);
  121.   } else {
  122.     inSubMenu = true;
  123.     subMenuIndex = 0;
  124.     displaySubMenu(menuItems[menuIndex], subMenuCodes[menuIndex][subMenuIndex]);
  125.   }
  126.   Serial.println("Enter button pressed");
  127. }
  128.  
  129. // Function to display the main menu
  130. void displayMenu() {
  131.   lcd.clear();
  132.   lcd.setCursor(0, 0);
  133.   lcd.print("Menu:");
  134.   lcd.setCursor(0, 1);
  135.   lcd.print(menuItems[menuIndex]);
  136. }
  137.  
  138. // Function to display the sub-menu
  139. void displaySubMenu(const char* title, const char* code) {
  140.   lcd.clear();
  141.   lcd.setCursor(0, 0);
  142.   lcd.print(title);
  143.   lcd.setCursor(0, 1);
  144.   lcd.print(subMenuItems[menuIndex][subMenuIndex]);
  145.   lcd.setCursor(0, 2);
  146.   lcd.print(code);
  147. }
  148.  
  149. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement