Advertisement
pleasedontcode

**Menu Display** rev_01

Dec 29th, 2024
186
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: **Menu Display**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-29 16:34:32
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 1 have 2 buttons 1 ssd1306 display i want an */
  21.     /* intervace to choose from 4 options */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SSD1306.h>
  28. #include <EasyButton.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void displayOptions(int selectedOption);
  34. void handleButtonPress(void);
  35.  
  36. /****** GLOBAL VARIABLES *****/
  37. SSD1306 display(0x3C, 4, 5); // I2C address, SDA, SCL pins
  38. EasyButton button1(2); // Button 1 on pin 2
  39. EasyButton button2(3); // Button 2 on pin 3
  40. int selectedOption = 0; // Variable to track the selected option
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize the display
  45.     display.init();
  46.     display.clear();
  47.     display.display();
  48.  
  49.     // Initialize buttons
  50.     button1.begin();
  51.     button2.begin();
  52.  
  53.     // Set button press handlers
  54.     button1.onPressed([]() {
  55.         selectedOption = (selectedOption + 1) % 4; // Cycle through options
  56.         displayOptions(selectedOption);
  57.     });
  58.  
  59.     button2.onPressed([]() {
  60.         selectedOption = (selectedOption - 1 + 4) % 4; // Cycle backwards through options
  61.         displayOptions(selectedOption);
  62.     });
  63.  
  64.     // Display the initial options
  65.     displayOptions(selectedOption);
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Update button states
  71.     button1.update();
  72.     button2.update();
  73. }
  74.  
  75. void displayOptions(int selectedOption)
  76. {
  77.     display.clear();
  78.     display.setTextSize(1);
  79.     display.setTextColor(WHITE);
  80.     display.setCursor(0, 0);
  81.     display.print("Select an option:");
  82.  
  83.     // Display options
  84.     for (int i = 0; i < 4; i++) {
  85.         if (i == selectedOption) {
  86.             display.setTextColor(BLACK, WHITE); // Highlight selected option
  87.         } else {
  88.             display.setTextColor(WHITE);
  89.         }
  90.         display.setCursor(0, 10 + i * 10);
  91.         display.print("Option ");
  92.         display.print(i + 1);
  93.     }
  94.     display.display();
  95. }
  96.  
  97. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement