Advertisement
skizziks_53

Another button debouncer v1000000.0

Apr 6th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. /*
  2.    April 6, 2019
  3.    Button demonstration:
  4.    1. The button sends messages only when it changes state, holding it down does nothing.
  5.    2. Pin #13 LED lights up to reflect the button state.
  6.    3. The button uses non-blocking button debouncing on both the press and the release events.
  7.    Board = any
  8.  
  9.    Turn on the serial monitor to use this sketch. After the "Exiting setup()" message, the button sends a serial message every time it is pressed down, and every time it is released.
  10.  
  11. */
  12.  
  13.  
  14. int button_1_pin = 7; // This is where the button is connected.
  15. // The above pin is using INPUT_PULLUP, so it should connect to a ground pin when pressed.
  16.  
  17. int button_debounce_time = 100; // This is the time in milliseconds to wait, to prevent the buttons from bouncing.
  18.  
  19. // The variables above you can change the values of.
  20. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  21. // The variables below do not change the values of.
  22.  
  23. int button_1_previous_state = 0;
  24. int button_1_current_state = 0;
  25.  
  26. bool buttons_enabled = true;
  27. unsigned long button_press_begin_time = 0;
  28. unsigned long button_press_current_time = 0;
  29.  
  30.  
  31.  
  32. void setup() {
  33.   Serial.begin(9600);
  34.   pinMode(button_1_pin, INPUT_PULLUP);
  35.   Serial.println("Exiting setup()");
  36. }
  37.  
  38.  
  39.  
  40. void loop() {
  41.  
  42.   if (buttons_enabled == true) {
  43.     button_1_current_state = 0;
  44.     if (digitalRead(button_1_pin) == LOW) {
  45.       button_1_current_state = 1;
  46.     }
  47.     // The if() condditions below make sure that you only get the "button pressed" message when the button state changes from "off" to "on".
  48.     if (button_1_previous_state == 0) { // If the button was previously not pressed...
  49.       if (button_1_current_state == 1) { // ...but it is pressed now...
  50.         Serial.println("Button pressed.");
  51.         digitalWrite(13, HIGH);
  52.         button_pressed();
  53.       }
  54.     }
  55.     // The two lines below place a de-bounce delay when the button is released, to make sure that it does not trigger a positive pulse at that time.
  56.     // This part can be placed inside the "if (buttons_enabled == true)" condition because the only time that digitalRead() is called on the button pin is read is when buttons_enabled == true.
  57.     if (button_1_previous_state == 1) { // If the button was previously being pressed...
  58.       if (button_1_current_state == 0) { // ...but it is not pressed now...
  59.         Serial.println("Button released.");
  60.         digitalWrite(13, LOW);
  61.         button_pressed();
  62.       }
  63.     }
  64.     button_1_previous_state = button_1_current_state;
  65.   }
  66.  
  67.   else { // If the buttons are not enabled, then that means they are de-bouncing and the elapsed time since the press must be checked.
  68.     button_press_current_time = millis();
  69.     if (button_press_current_time > button_press_begin_time) {
  70.       if (button_press_current_time >= (button_press_begin_time + button_debounce_time)) {
  71.         buttons_enabled = true;
  72.       }
  73.     }
  74.     else {
  75.       button_press_begin_time = millis();
  76.     }
  77.   }
  78.  
  79. } // end of main loop()
  80.  
  81.  
  82. void button_pressed() {
  83.   buttons_enabled = false;
  84.   button_press_begin_time = millis();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement