0

I'm having some trouble with software debouncing on Arduino UNO. There is a condition in my code where an unwanted debounce registers as a button push.

I'm using an interrupt for my button press, since my main loop has occasional long delays if certain conditions are met. I've attached pin 2 to my interrupt like so:

#define BTN 2
pinMode(BTN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BTN),buttonClick, FALLING);

And my interrupt function:

volatile unsigned long _lastClick = 0;    
void buttonClick()
{
  unsigned long _clickTimer = millis();
  if (_clickTimer - _lastClick > DEBOUNCE) 
  {
    _timer = 0;
    _count++;
    _lcd.setCursor(0, 1);
    _lcd.print(_count);
    _softSerial.println("stuff");
  }
  _lastClick = _clickTimer;
}

This works just fine when pressing the button. However, this doesnt account for the bounce when the button is released. So occasionally, if I hold the button down for long enough and then let go, the bouncing from depressing the key also registers as a falling edge.

Everything works just fine if my button press is brief enough to fall within the limit set by DEBOUNCE. What would be the best way to ensure the interrupt isn't called when depressing the button? Without adding passives to my circuit like capacitors?

1 Answer 1

1

Untested idea: acknowledge a button press only on a falling edge that follows a long HIGH period. For this you will have to:

  • attach the interrupt handler to the CHANGE events

  • update _lastClick on every such event

  • acknowledge a press if the DEBOUNCE time has elapsed and the input line reads LOW.

Side note: it is bad practice to do lengthy stuff (like printing to SoftSerial or an LCD) in interrupt context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.