Advertisement
skizziks_53

3-button debouncing

Aug 17th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. /*
  2.    August 17, 2018
  3.    This is an example of one way to de-bounce buttons in code.
  4.    This sketch doesn't really do anything, it only shows how to de-bounce buttons in code.
  5. */
  6.  
  7. int buttonPin2 = 2; // These are defining the pins for the button connections.
  8. int buttonPin3 = 3;
  9. int buttonPin4 = 4;
  10.  
  11.  
  12. // All three buttons share the variables below.
  13. bool buttonPressed = false; // This is the flag variable that must be false for button input to be accepted.
  14. // When a button is pushed, the above value is set to [true] and a start time [ millis() ] is saved in button_press_start_time.
  15. unsigned long button_press_start_time = 0; // This stores the system time that the button was pushed.
  16. unsigned long button_press_current_time = 0; // This stores the current time, to compare to the value above.
  17.  
  18. int button_debounce_time = 200; // This is the length of time (in milliseconds) to de-bounce the button inputs.
  19.  
  20. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  21. // Function prototypes:
  22. // These aren't really needed in this instance (with a one-file sketch in the Arduino IDE) but some compilers do require them.
  23. void buttonPressDelay();
  24. void doButton2stuff();
  25. void doButton3stuff();
  26. void doButton4stuff();
  27.  
  28. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  29.  
  30. void setup() {
  31.   // All that is done here is setting the input pin states.
  32.   pinMode(buttonPin2, INPUT_PULLUP);
  33.   pinMode(buttonPin3, INPUT_PULLUP);
  34.   pinMode(buttonPin4, INPUT_PULLUP);
  35. }
  36.  
  37. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  38.  
  39. void loop() { // begin of main sketch loop
  40.  
  41.   /*
  42.      Whatever other code you want can go here, outside of the button-handling code.
  43.   */
  44.  
  45.   if (buttonPressed == false) {
  46.     // The 'if' conditional statement above only allows checking for button presses if the boolean indicator is set to [false].
  47.     // The boolean indicator will only be false if there have been no button presses since the button_delay_time has elapsed from the previous button press.
  48.  
  49.     // Below is reading the #2 button pin.
  50.     // Since the pins were set to input_pullup, these should be checked for the low state.
  51.     if (digitalRead(buttonPin2) == LOW) {
  52.       buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  53.       doButton2stuff(); // Whatever this button press does, is in this function.
  54.     }
  55.  
  56.     // Below is reading the #3 button pin.
  57.     if (digitalRead(buttonPin3) == LOW) {
  58.       buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  59.       doButton3stuff(); // Whatever this button press does, is in this function.
  60.     }
  61.  
  62.     // Below is reading the #4 button pin.
  63.     if (digitalRead(buttonPin4) == LOW) {
  64.       buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
  65.       doButton4stuff(); // Whatever this button press does, is in this function.
  66.     }
  67.   } // end of ---> if (buttonPressed == false)
  68.   else { // ---> if (buttonPressed == true)
  69.     // The code below re-sets the buttonPressed variable to false, but only after the amount of milliseconds in {button_delay_time} have passed.
  70.     // Other parts of the sketch can run during this wait time, but no more button inputs will be accepted until this time period expires.
  71.     button_press_current_time = millis();
  72.     if (button_press_current_time >= button_press_start_time) {
  73.       if (button_press_current_time >= (button_press_start_time + button_debounce_time)) {
  74.         buttonPressed = false;
  75.       }
  76.     }
  77.     else { // if (button_press_current_time < button_press_start_time)
  78.       // This test checks for the condition that happens when the system timer has rolled over.
  79.       button_press_start_time = millis();
  80.     }
  81.   }
  82.  
  83.   /*
  84.      Whatever other code you want can go here, outside of the button-handling code.
  85.   */
  86.  
  87. } // This is the end of the main sketch loop.
  88.  
  89. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  90.  
  91. // The function below is what gets called whenever any button is pushed. The delay time is the same for all buttons.
  92. void buttonPressDelay() {
  93.   button_press_start_time = millis();
  94.   buttonPressed = true;
  95. }
  96.  
  97. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  98.  
  99. void doButton2stuff() {
  100.   // Whatever button 2 should do, goes in here
  101. }
  102.  
  103. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  104.  
  105. void doButton3stuff() {
  106.   // Whatever button 3 should do, goes in here
  107. }
  108.  
  109. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  110.  
  111. void doButton4stuff() {
  112.   // Whatever button 4 should do, goes in here
  113. }
  114.  
  115. // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement