Go Down

Topic: How to add deboucing to push button on my code  (Read 111 times) previous topic - next topic

ryurkovich

Nov 09, 2018, 03:23 pm Last Edit: Nov 09, 2018, 03:26 pm by ryurkovich
Hello,

I have this interactive that uses push buttons to control a power point show.
I'm also using a Teensy 2.0.

However I've been having issues with the  Keyboard.press(KEY_LEFT_SHIFT); Keyboard.press(KEY_TAB); Keyboard.press(KEY_ENTER); pressing multiple times when the button is pressed. From some research I have found that maybe 'debouncing' it could help but I can't figure out what code to add where to what I currently have. Thanks in advance for your help.

This is the code I currently have:

_________________________________
Code: [Select]

#include <Bounce.h>

// Define our two buttons. We are using the
// 3rd party "bounce" library to more reliably
// read the button clicks.
//
// Read more:
// https://www.pjrc.com/teensy/td_libs_Bounce.html
Bounce buttonTrue = Bounce(PIN_D4, 10);
Bounce buttonFalse = Bounce(PIN_B1, 10);

// Setup the two buttons with the pins we
// have connected to and make sure to
// make use of the internal pull-up circuitry
void setup() {
  pinMode(PIN_D4, INPUT_PULLUP); // True button
  pinMode(PIN_B1, INPUT_PULLUP); // False button
  pinMode(PIN_D6, OUTPUT); // LED
}

void loop() {
  buttonTrue.update();
  buttonFalse.update();
[b]
if (buttonTrue.fallingEdge()) {
 Keyboard.press(KEY_LEFT_SHIFT);
   Keyboard.press(KEY_TAB);
      Keyboard.press(KEY_ENTER);
}

if (buttonTrue.risingEdge()) {
  Keyboard.release(KEY_LEFT_SHIFT);
      Keyboard.release(KEY_TAB);
        Keyboard.release(KEY_ENTER);[/b]
}


if (buttonFalse.fallingEdge()) {
 Keyboard.press(KEY_DOWN_ARROW);
   Keyboard.release(KEY_DOWN_ARROW);
   delay(100);
   Keyboard.press(KEY_DOWN_ARROW);
   Keyboard.release(KEY_DOWN_ARROW);
}

if (buttonFalse.risingEdge()) {
    Keyboard.release(KEY_DOWN_ARROW);
}

}

miancule

It seems that de-bouncing is already performed in the Bounce class - but there's an issue with your code: you should check the result of the update() call.

Try something like this:

Code: [Select]

void loop() {
 
if (buttonTrue.update()) {
  if (buttonTrue.fallingEdge()) {
   Keyboard.press(KEY_LEFT_SHIFT);
   Keyboard.press(KEY_TAB);
   Keyboard.press(KEY_ENTER);
  }
  if (buttonTrue.risingEdge()) {
    Keyboard.release(KEY_LEFT_SHIFT);
    Keyboard.release(KEY_TAB);
    Keyboard.release(KEY_ENTER);
  }
}

if (buttonFalse.update()) {
  if (buttonFalse.fallingEdge()) {
    Keyboard.press(KEY_DOWN_ARROW);
    Keyboard.release(KEY_DOWN_ARROW);
    Keyboard.press(KEY_DOWN_ARROW);
    Keyboard.release(KEY_DOWN_ARROW);
  }

  if (buttonFalse.risingEdge()) {
    Keyboard.release(KEY_DOWN_ARROW);
  }
}

}

ryurkovich

Thanks, that seemed to help. The button still presses multiple times, but not as much.
Is there more of a delay I can add/where would I add it in the code?

cattledog

Quote
Is there more of a delay I can add/where would I add it in the code?
The second parameter of the constructor is the debounce period in milliseconds

Code: [Select]
Bounce buttonTrue = Bounce(PIN_D4, 10);
Bounce buttonFalse = Bounce(PIN_B1, 10);


Go Up