Interrupts and Bouncy buttons. How to Solve??

I don't think you need to disable the interrupt, just recognize if you are waiting for a previous buttons bounce time. I think the following will work.

#define BOUNCE_DURATION 20 // define an appropriate bounce time in ms for your switches

volatile unsigned long bounceTime=0; // variable to hold ms count to debounce a pressed switch

void intHandler(){
// this is the interrupt handler for button presses
// it ignores presses that occur in intervals less then the bounce time
if(millis() > bounceTime)
{
// Your code here to handle new button press ?
bounceTime = millis() + BOUNCE_DURATION; // set whatever bounce time in ms is appropriate
}
}

Note, the attachInterrupt documentation says that millis() wont work in an interrupt handler. Looking at the millis() function in wiring .c, it appears it will return the most recent millis reading, but it will not be incremented while in the handler, which is ok for this solution

1 Like