Advertisement
pleasedontcode

"Button MUX" rev_02

Dec 15th, 2024
36
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 MUX"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-16 02:21:02
  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.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - C:\Users\Craig\AppData\Local\Temp\.arduinoIDE-unsaved20241116-22
  30. 928-iq50t9.lf7t\sketch_dec16a\sketch_dec16a.ino:37:24: error: 'D
  31. 13' was not declared in this scope; did you mean 'A13'?
  32.    37 |
  33. const int muxPins[] = {D13, D12, D14, D27}; // MUX pins
  34.       |
  35.                        ^~~
  36.       |          
  37. ********* User code review feedback **********/
  38.  
  39. /* START CODE */
  40.  
  41. /****** DEFINITION OF LIBRARIES *****/
  42. #include <EasyButton.h>
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47.  
  48. // Pin definitions
  49. const int muxPins[] = {13, 12, 14, 27}; // MUX pins (Updated: Removed 'D' prefix)
  50. const int buttonPin = 26; // Button pin (Updated: Removed 'D' prefix)
  51. EasyButton button(buttonPin); // Create button object
  52.  
  53. // MUX value
  54. int muxValue = 0;
  55.  
  56. void setup(void)
  57. {
  58.     // Set pins 5, 18, 19, 23 to HIGH
  59.     pinMode(5, OUTPUT);
  60.     pinMode(18, OUTPUT);
  61.     pinMode(19, OUTPUT);
  62.     pinMode(23, OUTPUT);
  63.    
  64.     digitalWrite(5, HIGH);
  65.     digitalWrite(18, HIGH);
  66.     digitalWrite(19, HIGH);
  67.     digitalWrite(23, HIGH);
  68.  
  69.     // Set MUX pins as OUTPUT
  70.     for (int i = 0; i < 4; i++) {
  71.         pinMode(muxPins[i], OUTPUT);
  72.     }
  73.  
  74.     // Initialize button
  75.     button.begin();
  76. }
  77.  
  78. void loop(void)
  79. {
  80.     // Update button state
  81.     button.update();
  82.  
  83.     // Check if the button is pressed
  84.     if (button.isPressed()) {
  85.         // Increment the mux value
  86.         muxValue = (muxValue + 1) % 16; // Assuming a 4-bit MUX (0-15)
  87.  
  88.         // Set MUX pins according to the muxValue
  89.         for (int i = 0; i < 4; i++) {
  90.             digitalWrite(muxPins[i], (muxValue >> i) & 0x01);
  91.         }
  92.     }
  93.  
  94.     // Add a small delay to debounce the button
  95.     delay(50);
  96. }
  97.  
  98. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement