Advertisement
pleasedontcode

"Button Navigation" rev_04

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