Advertisement
pleasedontcode

"Button Navigation" rev_08

Jun 6th, 2024
417
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 04:13:09
  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 <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.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t UP_BUTTON_PushButton_PIN_D11 = 11;
  37. const uint8_t DOWN_BUTTON_PushButton_PIN_D12 = 12;
  38. const uint8_t ENTER_BUTTON_PushButton_PIN_D10 = 10;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. EasyButton buttonUp(UP_BUTTON_PushButton_PIN_D11);
  42. EasyButton buttonDown(DOWN_BUTTON_PushButton_PIN_D12);
  43. EasyButton buttonEnter(ENTER_BUTTON_PushButton_PIN_D10);
  44.  
  45. // Initialize the LCD with the I2C address 0x27 and a 20x4 display
  46. LiquidCrystal_I2C lcd(0x27, 20, 4);
  47.  
  48. /****** VARIABLES FOR MENU *****/
  49. float max_temp = 100.0;
  50. float min_temp = 0.0;
  51. float max_flow = 10.0;
  52. float min_flow = 0.0;
  53. int menu_index = 0;
  54. int submenu_index = 0;
  55. bool in_submenu = false;
  56.  
  57. /****** CALLBACK FUNCTIONS *****/
  58. void onButtonUpPressed() {
  59.   if (in_submenu) {
  60.     if (submenu_index == 0) {
  61.       max_temp -= 0.1;
  62.     } else if (submenu_index == 1) {
  63.       min_temp -= 0.1;
  64.     } else if (submenu_index == 2) {
  65.       max_flow -= 0.1;
  66.     } else if (submenu_index == 3) {
  67.       min_flow -= 0.1;
  68.     }
  69.   } else {
  70.     menu_index = (menu_index - 1 + 4) % 4; // Wrap around menu index
  71.   }
  72.   updateLCD();
  73. }
  74.  
  75. void onButtonDownPressed() {
  76.   if (in_submenu) {
  77.     if (submenu_index == 0) {
  78.       max_temp += 0.1;
  79.     } else if (submenu_index == 1) {
  80.       min_temp += 0.1;
  81.     } else if (submenu_index == 2) {
  82.       max_flow += 0.1;
  83.     } else if (submenu_index == 3) {
  84.       min_flow += 0.1;
  85.     }
  86.   } else {
  87.     menu_index = (menu_index + 1) % 4; // Wrap around menu index
  88.   }
  89.   updateLCD();
  90. }
  91.  
  92. void onButtonEnterPressed() {
  93.   if (!in_submenu) {
  94.     in_submenu = true;
  95.     submenu_index = menu_index;
  96.   } else {
  97.     in_submenu = false;
  98.   }
  99.   updateLCD();
  100. }
  101.  
  102. void updateLCD() {
  103.   lcd.clear();
  104.   if (!in_submenu) {
  105.     lcd.setCursor(0, 0);
  106.     lcd.print("Menu:");
  107.     lcd.setCursor(0, 1);
  108.     lcd.print(menu_index == 0 ? "> Max Temp" : "  Max Temp");
  109.     lcd.setCursor(0, 2);
  110.     lcd.print(menu_index == 1 ? "> Min Temp" : "  Min Temp");
  111.     lcd.setCursor(0, 3);
  112.     lcd.print(menu_index == 2 ? "> Max Flow" : "  Max Flow");
  113.     lcd.setCursor(0, 4);
  114.     lcd.print(menu_index == 3 ? "> Min Flow" : "  Min Flow");
  115.   } else {
  116.     lcd.setCursor(0, 0);
  117.     if (submenu_index == 0) {
  118.       lcd.print("Max Temp: ");
  119.       lcd.print(max_temp);
  120.     } else if (submenu_index == 1) {
  121.       lcd.print("Min Temp: ");
  122.       lcd.print(min_temp);
  123.     } else if (submenu_index == 2) {
  124.       lcd.print("Max Flow: ");
  125.       lcd.print(max_flow);
  126.     } else if (submenu_index == 3) {
  127.       lcd.print("Min Flow: ");
  128.       lcd.print(min_flow);
  129.     }
  130.   }
  131. }
  132.  
  133. void setup(void) {
  134.   // Initialize serial communication
  135.   Serial.begin(115200);
  136.  
  137.   // Initialize buttons
  138.   buttonUp.begin();
  139.   buttonDown.begin();
  140.   buttonEnter.begin();
  141.  
  142.   // Attach callback functions to buttons
  143.   buttonUp.onPressed(onButtonUpPressed);
  144.   buttonDown.onPressed(onButtonDownPressed);
  145.   buttonEnter.onPressed(onButtonEnterPressed);
  146.  
  147.   // Initialize the LCD
  148.   lcd.init();
  149.   lcd.backlight();
  150.   updateLCD();
  151.  
  152.   // Set pin modes for buttons
  153.   pinMode(UP_BUTTON_PushButton_PIN_D11, INPUT_PULLUP);
  154.   pinMode(DOWN_BUTTON_PushButton_PIN_D12, INPUT_PULLUP);
  155.   pinMode(ENTER_BUTTON_PushButton_PIN_D10, INPUT_PULLUP);
  156. }
  157.  
  158. void loop(void) {
  159.   // Read button states
  160.   buttonUp.read();
  161.   buttonDown.read();
  162.   buttonEnter.read();
  163.  
  164.   // Your main code here, to run repeatedly:
  165. }
  166.  
  167. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement