Advertisement
pleasedontcode

"Button Navigation" rev_09

Jun 7th, 2024
280
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 10:28:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* lcd display i2c  20x4  button pin11 UP LOW menu up */
  21.     /* button pin10 UP LOW menu enter  button pin12UP LOW */
  22.     /* menu down  Temperatur  submenu   MaxTemp */
  23.     /* ->max_temp -= 0.1;  MinTeamp->min_temp -= 0.1; */
  24.     /* Durchfluss(Flow)  submenu   MaxFlow ->  MinFlow -> */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  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 displayMenu(void);
  35. void handleButtonPress(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t UP_BUTTON_PushButton_PIN_D11        = 11;
  39. const uint8_t DOWN_BUTTON_PushButton_PIN_D12      = 12;
  40. const uint8_t ENTER_BUTTON_PushButton_PIN_D10     = 10;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display
  44.  
  45. /****** MENU VARIABLES *****/
  46. enum MenuState { MAIN_MENU, TEMP_SUBMENU, FLOW_SUBMENU };
  47. MenuState currentMenu = MAIN_MENU;
  48. int menuIndex = 0;
  49. float maxTemp = 100.0;
  50. float minTemp = 0.0;
  51. float maxFlow = 10.0;
  52. float minFlow = 0.0;
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.  
  58.     pinMode(UP_BUTTON_PushButton_PIN_D11,    INPUT_PULLUP);
  59.     pinMode(DOWN_BUTTON_PushButton_PIN_D12,  INPUT_PULLUP);
  60.     pinMode(ENTER_BUTTON_PushButton_PIN_D10, INPUT_PULLUP);
  61.  
  62.     // Initialize the LCD
  63.     lcd.init();
  64.     lcd.backlight();
  65.  
  66.     // Display the initial menu
  67.     displayMenu();
  68. }
  69.  
  70. void loop(void)
  71. {
  72.     // put your main code here, to run repeatedly:
  73.     handleButtonPress();
  74. }
  75.  
  76. void displayMenu(void)
  77. {
  78.     lcd.clear();
  79.     switch (currentMenu)
  80.     {
  81.         case MAIN_MENU:
  82.             lcd.setCursor(0, 0);
  83.             lcd.print("1. Temperatur");
  84.             lcd.setCursor(0, 1);
  85.             lcd.print("2. Durchfluss");
  86.             break;
  87.         case TEMP_SUBMENU:
  88.             lcd.setCursor(0, 0);
  89.             lcd.print("MaxTemp: ");
  90.             lcd.print(maxTemp, 1);
  91.             lcd.setCursor(0, 1);
  92.             lcd.print("MinTemp: ");
  93.             lcd.print(minTemp, 1);
  94.             break;
  95.         case FLOW_SUBMENU:
  96.             lcd.setCursor(0, 0);
  97.             lcd.print("MaxFlow: ");
  98.             lcd.print(maxFlow, 1);
  99.             lcd.setCursor(0, 1);
  100.             lcd.print("MinFlow: ");
  101.             lcd.print(minFlow, 1);
  102.             break;
  103.     }
  104. }
  105.  
  106. void handleButtonPress(void)
  107. {
  108.     static uint32_t lastDebounceTime = 0;
  109.     const uint32_t debounceDelay = 50;
  110.  
  111.     if (millis() - lastDebounceTime > debounceDelay)
  112.     {
  113.         if (digitalRead(UP_BUTTON_PushButton_PIN_D11) == LOW)
  114.         {
  115.             lastDebounceTime = millis();
  116.             if (currentMenu == MAIN_MENU)
  117.             {
  118.                 menuIndex = (menuIndex == 0) ? 1 : 0;
  119.             }
  120.             else if (currentMenu == TEMP_SUBMENU)
  121.             {
  122.                 maxTemp -= 0.1;
  123.                 minTemp -= 0.1;
  124.             }
  125.             else if (currentMenu == FLOW_SUBMENU)
  126.             {
  127.                 maxFlow -= 0.1;
  128.                 minFlow -= 0.1;
  129.             }
  130.             displayMenu();
  131.         }
  132.         else if (digitalRead(DOWN_BUTTON_PushButton_PIN_D12) == LOW)
  133.         {
  134.             lastDebounceTime = millis();
  135.             if (currentMenu == MAIN_MENU)
  136.             {
  137.                 menuIndex = (menuIndex == 1) ? 0 : 1;
  138.             }
  139.             else if (currentMenu == TEMP_SUBMENU)
  140.             {
  141.                 maxTemp += 0.1;
  142.                 minTemp += 0.1;
  143.             }
  144.             else if (currentMenu == FLOW_SUBMENU)
  145.             {
  146.                 maxFlow += 0.1;
  147.                 minFlow += 0.1;
  148.             }
  149.             displayMenu();
  150.         }
  151.         else if (digitalRead(ENTER_BUTTON_PushButton_PIN_D10) == LOW)
  152.         {
  153.             lastDebounceTime = millis();
  154.             if (currentMenu == MAIN_MENU)
  155.             {
  156.                 if (menuIndex == 0)
  157.                 {
  158.                     currentMenu = TEMP_SUBMENU;
  159.                 }
  160.                 else if (menuIndex == 1)
  161.                 {
  162.                     currentMenu = FLOW_SUBMENU;
  163.                 }
  164.             }
  165.             else
  166.             {
  167.                 currentMenu = MAIN_MENU;
  168.             }
  169.             displayMenu();
  170.         }
  171.     }
  172. }
  173.  
  174. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement