Advertisement
pleasedontcode

**Button Control** rev_01

Dec 15th, 2024
47
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 Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-16 02:12:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* set pins d5,d18,d19,d23 to high.  set pins D13, */
  21.     /* D12, D14, D27 as mux pins s0, s1 ,s2 ,s3.  add a */
  22.     /* button to pin D26, when the input goes low, */
  23.     /* increment the number to the mux. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // Pin definitions
  36. const int muxPins[] = {D13, D12, D14, D27}; // MUX pins
  37. const int buttonPin = D26; // Button pin
  38. EasyButton button(buttonPin); // Create button object
  39.  
  40. // MUX value
  41. int muxValue = 0;
  42.  
  43. void setup(void)
  44. {
  45.     // Set pins D5, D18, D19, D23 to HIGH
  46.     pinMode(D5, OUTPUT);
  47.     pinMode(D18, OUTPUT);
  48.     pinMode(D19, OUTPUT);
  49.     pinMode(D23, OUTPUT);
  50.    
  51.     digitalWrite(D5, HIGH);
  52.     digitalWrite(D18, HIGH);
  53.     digitalWrite(D19, HIGH);
  54.     digitalWrite(D23, HIGH);
  55.  
  56.     // Set MUX pins as OUTPUT
  57.     for (int i = 0; i < 4; i++) {
  58.         pinMode(muxPins[i], OUTPUT);
  59.     }
  60.  
  61.     // Initialize button
  62.     button.begin();
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // Update button state
  68.     button.update();
  69.  
  70.     // Check if the button is pressed
  71.     if (button.isPressed()) {
  72.         // Increment the mux value
  73.         muxValue = (muxValue + 1) % 16; // Assuming a 4-bit MUX (0-15)
  74.  
  75.         // Set MUX pins according to the muxValue
  76.         for (int i = 0; i < 4; i++) {
  77.             digitalWrite(muxPins[i], (muxValue >> i) & 0x01);
  78.         }
  79.     }
  80.  
  81.     // Add a small delay to debounce the button
  82.     delay(50);
  83. }
  84.  
  85. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement