Advertisement
skizziks_53

button_debouncer_02

Aug 12th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. /*
  2. August 12, 2017
  3. This is an example of one way to debounce buttons in code.
  4. This sketch doesn't produce any functional output, it only shows how to debounce buttons in code.
  5.  
  6. Below is defining two different groups of buttons.
  7. Each group will have its own separate button debounce timer.
  8.  
  9. Buttons on pins #2, #3 and #4 are in group #1.
  10. The button delay (debounce) time for group 1 is set to 1000 milliseconds (the bg1_button_delay_time variable).
  11.  
  12. Buttons on pins #5, #6 and #7 are in group #2.
  13. The button delay (debounce) time for group 2 is set to 5000 milliseconds (the bg2_button_delay_time variable).
  14.  
  15. In this example, neither groups of buttons interferes with the other. The delay/debounce times can overlap each other, and both will still run for the correct total time.
  16.  
  17. If a button in one group is pressed, no more buttons {in that same group} can be pressed until the time delay {for that group} is passed.
  18.  
  19.  
  20. When this program is checked for the Uno board:
  21. Sketch uses 1,174 bytes (3%) of program storage space. Maximum is 32,256 bytes.
  22. Global variables use 27 bytes (1%) of dynamic memory, leaving 2,021 bytes for local variables. Maximum is 2,048 bytes.
  23.  
  24. */
  25. // Button group #1:
  26. int buttonPin2 = 2; // These are defining the physical input pins for the button connections.
  27. int buttonPin3 = 3;
  28. int buttonPin4 = 4;
  29.  
  30. // Button group #2:
  31. int buttonPin5 = 5;
  32. int buttonPin6 = 6;
  33. int buttonPin7 = 7;
  34.  
  35. boolean bg1_buttonPressed = false; // "bg" here means "button group"
  36. unsigned long bg1_button_press_start_time = 0; // This stores the system time that the button was pushed.
  37. unsigned long bg1_button_press_current_time = 0; // This stores the current time, to compare to the value above.
  38. int bg1_button_delay_time = 1000; // This is the length of time (in milliseconds) to delay button inputs for any of the buttons in button group #1.
  39.  
  40. boolean bg2_buttonPressed = false;
  41. unsigned long bg2_button_press_start_time = 0;
  42. unsigned long bg2_button_press_current_time = 0;
  43. int bg2_button_delay_time = 5000; // This is the length of time (in milliseconds) to delay button inputs for any of the buttons in button group #2.
  44.  
  45. // ***************************************************
  46.  
  47. void setup() {
  48. // All that is done here is setting the input pin states.
  49. // This sketch doesn't actually *do* anything with the button inputs, but if it did, you would need these here.
  50. pinMode(buttonPin2, INPUT_PULLUP);
  51. pinMode(buttonPin3, INPUT_PULLUP);
  52. pinMode(buttonPin4, INPUT_PULLUP);
  53. pinMode(buttonPin5, INPUT_PULLUP);
  54. pinMode(buttonPin6, INPUT_PULLUP);
  55. pinMode(buttonPin7, INPUT_PULLUP);
  56. }
  57.  
  58. // ***************************************************
  59.  
  60. void loop() {
  61.  
  62. if (bg1_buttonPressed == false) {
  63. if (digitalRead(buttonPin2) == LOW) {
  64. doButton2stuff();
  65. bg1_buttonPressDelay();
  66. }
  67.  
  68. if (digitalRead(buttonPin3) == LOW) {
  69. doButton3stuff();
  70. bg1_buttonPressDelay();
  71. }
  72.  
  73. if (digitalRead(buttonPin4) == LOW) {
  74. doButton4stuff();
  75. bg1_buttonPressDelay();
  76. }
  77. }
  78.  
  79. if (bg2_buttonPressed == false) {
  80. if (digitalRead(buttonPin5) == LOW) {
  81. doButton5stuff();
  82. bg2_buttonPressDelay();
  83. }
  84.  
  85. if (digitalRead(buttonPin6) == LOW) {
  86. doButton6stuff();
  87. bg2_buttonPressDelay();
  88. }
  89.  
  90. if (digitalRead(buttonPin7) == LOW) {
  91. doButton7stuff();
  92. bg2_buttonPressDelay();
  93. }
  94. }
  95.  
  96.  
  97. // The section below re-enables button group #1 if the timer time span has elapsed since the button was pushed.
  98. if (bg1_buttonPressed == true) {
  99. bg1_button_press_current_time = millis();
  100. if (bg1_button_press_current_time > bg1_button_press_start_time) {
  101. if (bg1_button_press_current_time >= (bg1_button_press_start_time + bg1_button_delay_time)) {
  102. bg1_buttonPressed = false;
  103. }
  104. }
  105. else {
  106. bg1_button_press_start_time = millis();
  107. }
  108. }
  109.  
  110. // The section below re-enables button group #2 if the timer time span has elapsed since the button was pushed.
  111. if (bg2_buttonPressed == true) {
  112. bg2_button_press_current_time = millis();
  113. if (bg2_button_press_current_time > bg2_button_press_start_time) {
  114. if (bg2_button_press_current_time >= (bg2_button_press_start_time + bg2_button_delay_time)) {
  115. bg2_buttonPressed = false;
  116. }
  117. }
  118. else {
  119. bg2_button_press_start_time = millis();
  120. }
  121. }
  122.  
  123. } // This is the end of the main sketch loop.
  124.  
  125. // ***************************************************
  126.  
  127. void bg1_buttonPressDelay() {
  128. // This gets called when any button in group #1 gets pushed.
  129. bg1_button_press_start_time = millis();
  130. bg1_buttonPressed = true;
  131. }
  132.  
  133. // ***************************************************
  134.  
  135. void bg2_buttonPressDelay() {
  136. // This gets called when any button in group #2 gets pushed.
  137. bg2_button_press_start_time = millis();
  138. bg2_buttonPressed = true;
  139. }
  140.  
  141. // ***************************************************
  142.  
  143. /*
  144. The individual functions for what each button does are below.
  145. */
  146.  
  147. void doButton2stuff() {
  148. // Whatever button 2 should do, goes in here
  149. }
  150.  
  151. void doButton3stuff() {
  152. // Whatever button 3 should do, goes in here
  153. }
  154.  
  155. void doButton4stuff() {
  156. // Whatever button 4 should do, goes in here
  157. }
  158.  
  159. void doButton5stuff() {
  160. // Whatever button 5 should do, goes in here
  161. }
  162.  
  163. void doButton6stuff() {
  164. // Whatever button 6 should do, goes in here
  165. }
  166.  
  167. void doButton7stuff() {
  168. // Whatever button 7 should do, goes in here
  169. }
  170.  
  171.  
  172. // ~~~~~~~~ fini ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement