Advertisement
pleasedontcode

"Button Navigation" rev_03

Jun 6th, 2024
336
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:57:12
  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 <Wire.h>
  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.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t UP_BUTTON_PushButton_PIN_D11 = 11;
  42. const uint8_t DOWN_BUTTON_PushButton_PIN_D12 = 12;
  43. const uint8_t ENTER_BUTTON_PushButton_PIN_D10 = 10;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t lcd_LCD2004I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 based on examples
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. EasyButton upButton(UP_BUTTON_PushButton_PIN_D11);  // Initialize EasyButton for UP button
  50. EasyButton downButton(UP_BUTTON_PushButton_PIN_D12);  // Initialize EasyButton for DOWN button
  51. EasyButton enterButton(ENTER_BUTTON_PushButton_PIN_D10);  // Initialize EasyButton for ENTER button
  52. LiquidCrystal_I2C lcd(lcd_LCD2004I2C_I2C_SLAVE_ADDRESS, 20, 4);  // Initialize LiquidCrystal_I2C for a 20x4 display
  53.  
  54. /****** MENU VARIABLES *****/
  55. enum MenuOption { TEMPERATURE, MAX_TEMP, MIN_TEMP, FLOW, MAX_FLOW, MIN_FLOW };
  56. MenuOption currentMenu = TEMPERATURE;
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     Serial.begin(115200);
  62.  
  63.     pinMode(UP_BUTTON_PushButton_PIN_D11, INPUT_PULLUP);
  64.     pinMode(DOWN_BUTTON_PushButton_PIN_D12, INPUT_PULLUP);
  65.     pinMode(ENTER_BUTTON_PushButton_PIN_D10, INPUT_PULLUP);
  66.  
  67.     // Initialize EasyButton instances
  68.     upButton.begin();
  69.     downButton.begin();
  70.     enterButton.begin();
  71.  
  72.     // Attach callback functions to button press events
  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. {
  85.     // put your main code here, to run repeatedly:
  86.     upButton.read();
  87.     downButton.read();
  88.     enterButton.read();
  89. }
  90.  
  91. // Callback functions for button presses
  92. void onUpButtonPressed() {
  93.     Serial.println("UP button pressed");
  94.     if (currentMenu == TEMPERATURE) {
  95.         currentMenu = MIN_FLOW;
  96.     } else {
  97.         currentMenu = static_cast<MenuOption>(currentMenu - 1);
  98.     }
  99.     displayMenu();
  100. }
  101.  
  102. void onDownButtonPressed() {
  103.     Serial.println("DOWN button pressed");
  104.     if (currentMenu == MIN_FLOW) {
  105.         currentMenu = TEMPERATURE;
  106.     } else {
  107.         currentMenu = static_cast<MenuOption>(currentMenu + 1);
  108.     }
  109.     displayMenu();
  110. }
  111.  
  112. void onEnterButtonPressed() {
  113.     Serial.println("ENTER button pressed");
  114.     // Implement code to handle the enter button press based on the current menu
  115. }
  116.  
  117. void displayMenu() {
  118.     lcd.clear();
  119.     switch (currentMenu) {
  120.         case TEMPERATURE:
  121.             lcd.setCursor(0, 0);
  122.             lcd.print("Menu: Temperature");
  123.             break;
  124.         case MAX_TEMP:
  125.             lcd.setCursor(0, 0);
  126.             lcd.print("Menu: MaxTemp");
  127.             break;
  128.         case MIN_TEMP:
  129.             lcd.setCursor(0, 0);
  130.             lcd.print("Menu: MinTemp");
  131.             break;
  132.         case FLOW:
  133.             lcd.setCursor(0, 0);
  134.             lcd.print("Menu: Flow");
  135.             break;
  136.         case MAX_FLOW:
  137.             lcd.setCursor(0, 0);
  138.             lcd.print("Menu: MaxFlow");
  139.             break;
  140.         case MIN_FLOW:
  141.             lcd.setCursor(0, 0);
  142.             lcd.print("Menu: MinFlow");
  143.             break;
  144.     }
  145. }
  146.  
  147. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement