Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- August 17, 2018
- This is an example of one way to de-bounce buttons in code.
- This sketch doesn't really do anything, it only shows how to de-bounce buttons in code.
- */
- int buttonPin2 = 2; // These are defining the pins for the button connections.
- int buttonPin3 = 3;
- int buttonPin4 = 4;
- // All three buttons share the variables below.
- bool buttonPressed = false; // This is the flag variable that must be false for button input to be accepted.
- // When a button is pushed, the above value is set to [true] and a start time [ millis() ] is saved in button_press_start_time.
- unsigned long button_press_start_time = 0; // This stores the system time that the button was pushed.
- unsigned long button_press_current_time = 0; // This stores the current time, to compare to the value above.
- int button_debounce_time = 200; // This is the length of time (in milliseconds) to de-bounce the button inputs.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // Function prototypes:
- // These aren't really needed in this instance (with a one-file sketch in the Arduino IDE) but some compilers do require them.
- void buttonPressDelay();
- void doButton2stuff();
- void doButton3stuff();
- void doButton4stuff();
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void setup() {
- // All that is done here is setting the input pin states.
- pinMode(buttonPin2, INPUT_PULLUP);
- pinMode(buttonPin3, INPUT_PULLUP);
- pinMode(buttonPin4, INPUT_PULLUP);
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void loop() { // begin of main sketch loop
- /*
- Whatever other code you want can go here, outside of the button-handling code.
- */
- if (buttonPressed == false) {
- // The 'if' conditional statement above only allows checking for button presses if the boolean indicator is set to [false].
- // 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.
- // Below is reading the #2 button pin.
- // Since the pins were set to input_pullup, these should be checked for the low state.
- if (digitalRead(buttonPin2) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton2stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #3 button pin.
- if (digitalRead(buttonPin3) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton3stuff(); // Whatever this button press does, is in this function.
- }
- // Below is reading the #4 button pin.
- if (digitalRead(buttonPin4) == LOW) {
- buttonPressDelay(); // The buttonPressDelay() function disables further button input, so that is called first.
- doButton4stuff(); // Whatever this button press does, is in this function.
- }
- } // end of ---> if (buttonPressed == false)
- else { // ---> if (buttonPressed == true)
- // The code below re-sets the buttonPressed variable to false, but only after the amount of milliseconds in {button_delay_time} have passed.
- // Other parts of the sketch can run during this wait time, but no more button inputs will be accepted until this time period expires.
- button_press_current_time = millis();
- if (button_press_current_time >= button_press_start_time) {
- if (button_press_current_time >= (button_press_start_time + button_debounce_time)) {
- buttonPressed = false;
- }
- }
- else { // if (button_press_current_time < button_press_start_time)
- // This test checks for the condition that happens when the system timer has rolled over.
- button_press_start_time = millis();
- }
- }
- /*
- Whatever other code you want can go here, outside of the button-handling code.
- */
- } // This is the end of the main sketch loop.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // The function below is what gets called whenever any button is pushed. The delay time is the same for all buttons.
- void buttonPressDelay() {
- button_press_start_time = millis();
- buttonPressed = true;
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton2stuff() {
- // Whatever button 2 should do, goes in here
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton3stuff() {
- // Whatever button 3 should do, goes in here
- }
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- void doButton4stuff() {
- // Whatever button 4 should do, goes in here
- }
- // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement