Advertisement
pleasedontcode

Button Increment rev_05

Dec 15th, 2024
46
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 Increment
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-16 02:45:24
  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. #### Feedback 2 ####
  38. - Add that when the button is pressed it is displayed on the seria
  39. l monitor, along with the count and the settings for the mux.
  40. Al
  41. so flash the internal LED for 1 second every time the button is
  42. pressed
  43. ********* User code review feedback **********/
  44.  
  45. /* START CODE */
  46.  
  47. /****** DEFINITION OF LIBRARIES *****/
  48. #include <EasyButton.h>
  49.  
  50. /****** FUNCTION PROTOTYPES *****/
  51. void setup(void);
  52. void loop(void);
  53.  
  54. // Pin definitions
  55. const int muxPins[] = {13, 12, 14, 27}; // MUX pins (Updated: Removed 'D' prefix)
  56. const int buttonPin = 26; // Button pin (Updated: Removed 'D' prefix)
  57. EasyButton button(buttonPin); // Create button object
  58.  
  59. // MUX value
  60. int muxValue = 0;
  61.  
  62. void setup(void)
  63. {
  64.     // Set pins 5, 18, 19, 23 to HIGH
  65.     pinMode(5, OUTPUT);
  66.     pinMode(18, OUTPUT);
  67.     pinMode(19, OUTPUT);
  68.     pinMode(23, OUTPUT);
  69.    
  70.     digitalWrite(5, HIGH);
  71.     digitalWrite(18, HIGH);
  72.     digitalWrite(19, HIGH);
  73.     digitalWrite(23, HIGH);
  74.  
  75.     // Set MUX pins as OUTPUT
  76.     for (int i = 0; i < 4; i++) {
  77.         pinMode(muxPins[i], OUTPUT);
  78.     }
  79.  
  80.     // Initialize button
  81.     button.begin();
  82.  
  83.     // Initialize Serial communication
  84.     Serial.begin(9600);
  85. }
  86.  
  87. void loop(void)
  88. {
  89.     // Update button state
  90.     button.update();
  91.  
  92.     // Check if the button is pressed
  93.     if (button.isPressed()) {
  94.         // Increment the mux value
  95.         muxValue = (muxValue + 1) % 16; // Assuming a 4-bit MUX (0-15)
  96.  
  97.         // Set MUX pins according to the muxValue
  98.         for (int i = 0; i < 4; i++) {
  99.             digitalWrite(muxPins[i], (muxValue >> i) & 0x01);
  100.         }
  101.  
  102.         // Display the button press count and mux settings on Serial Monitor
  103.         Serial.print("Button pressed! Count: ");
  104.         Serial.print(muxValue);
  105.         Serial.print(" MUX settings: ");
  106.         for (int i = 0; i < 4; i++) {
  107.             Serial.print((muxValue >> i) & 0x01);
  108.             if (i < 3) Serial.print(", ");
  109.         }
  110.         Serial.println();
  111.  
  112.         // Flash the internal LED for 1 second
  113.         digitalWrite(LED_BUILTIN, HIGH);
  114.         delay(1000);
  115.         digitalWrite(LED_BUILTIN, LOW);
  116.     }
  117.  
  118.     // Add a small delay to debounce the button
  119.     delay(50);
  120. }
  121.  
  122. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement