Advertisement
pleasedontcode

"Button Navigation" rev_10

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