Debounce (delay button press)

How do I debounce my button presses without lagging my app. I want to use a button as a toggle, but I don;t want to actually slow down the app. Example:

////////////////////////////////////////////////
//check button
if (arduboy.pressed(B_BUTTON))
  {
   delay(100);
   
   if (b==60)
    {
      b=10;
    }
   else { b=60;
    }
  }
////////////////////////////////////////////////

Thanks,
Brad

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

1 Like

He can call his sketches all he wants :wink:

you could for example toggle a boolean and count.

boolean myToggle;
byte b;
int myCounter;

if (arduboy.pressed(B_BUTTON))
{
myToggle = !myToggle;
}

if ((myCounter > 100) && myToggle)
{
b =10;
myCounter = 0;
}
if ((myCounter >  100) && !myToggle)
{
b = 60;
myCounter = 0;
}

OR the other option is to use pressed and notPressed to toggle a boolean pressing …

boolean myToggle;
boolean pressing;
byte b;

if (arduboy.pressed(B_BUTTON) && !pressing)
{
myToggle = !myToggle;
pressing = true;
}
if (arduboy.notPressed(B_BUTTON) pressing = false;

if (myToggle) b = 10;

if (!myToggle) b = 60;

or a combination of your code with these examples.

1 Like

A) I am writing an app, and thank you for making sure I now call everything I write an app, even if it’s a game.
B) I don’t know what else to call it , so I’ll call it ‘bounce behavior’ and I want to ‘de-bounce’ it. I know what debouncing is, and you knew what I meant anyways, jeesh!
C) Thanks, I will need to research this.

1 Like

Thank you, this is very helpful.

1 Like

If you’re looking to do something when a button is just pressed, rather than every time the button is held down (which is what arduboy.pressed does), one way to do it would be like this:

// Stores the button state
uint8_t previousButtonState = 0;
uint8_t currentButtonState = 0;

// Needs to be called at the start of the loop function
void updateButtonState(Arduboy &ab)
{
  previousButtonState = currentButtonState;
  currentButtonState = ab.buttonsState();
}

// Returns if a button was just pressed
bool buttonJustPressed(uint8_t button)
{
  if (!(previousButtonState & button) && (currentButtonState & button))
  {
    return true;
  }
  return false;
}

An example of how to use the above code:

#include <Arduboy.h>

// Stores the button state
uint8_t previousButtonState = 0;
uint8_t currentButtonState = 0;

// Needs to be called at the start of the loop function
void updateButtonState(Arduboy &ab)
{
  previousButtonState = currentButtonState;
  currentButtonState = ab.buttonsState();
}

// Returns if a button was just pressed
bool buttonJustPressed(uint8_t button)
{
  if (!(previousButtonState & button) && (currentButtonState & button))
  {
    return true;
  }
  return false;
}




Arduboy arduboy;

void setup()
{
  arduboy.begin();
  
  // Your setup code goes here
}

void loop()
{
  if (!arduboy.nextFrame()) return;
  
  // Update the button state first
  updateButtonState(arduboy);
  
  arduboy.clear();
  
  // Check for a button just being pressed
  if (buttonJustPressed(B_BUTTON))
  {
    // Do logic
  }
  
  // Your loop code goes here

  arduboy.display();
}

There are probably better ways to do it, but this works.

2 Likes

@Pharap I fixed it.

@Mortician while the original Arduboy code doesn’t have debounce, we use our own library which has “justPressed” created by @Dreamer3 in an extra library called Arduboy Extra: https://github.com/yyyc514/ArduboyExtra

Have a look at simple_buttons.cpp and simple_buttons.h

While it might be to complicated for you to extract what you need, I think it’s still interesting to try to understand the code :slight_smile:

have fun

I’ll have to see if I can dig up my favorite implementation of this, but I much rather prefer to grab the buttons with a pin change interrupt routine:

http://playground.arduino.cc/Main/PinChangeInterrupt

Depending on your application you can either put the debounce in the interrupt handler or in the input handler (because you don’t want any actual functions handled while in interrupt).

I can’t think of why any frame based game would want to do interrupt based button stuff vs just polling the buttons all at once for each loop. (other than wake from sleep and other rare things)

1 Like