Go Down

Topic: debouncing an interrupt trigger (Read 12 times) previous topic - next topic


Rafael Quines


I was having very much the same problem with my button bouncing and producing very strange results as the interrrupt was being called over and over on the button bounces.  I tries Big Mike's idea with the code idea from mikalhart and it works great!  Thanks alot guys I'm glad I found this thread!!!



 What do you propose with the timer interrupt library?



Hello! Let's go back to 2008...

BigMike's idea is a good one, but not perfect...

Code: [Select]
void my_interrupt_handler()
 static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = millis();
 // If interrupts come faster than 200ms, assume it's a bounce and ignore
 if (interrupt_time - last_interrupt_time > 200)
   ... do your thing
 last_interrupt_time = interrupt_time;

Let's imagine that an interrupt occurs every 150ms constantly for 15 seconds. In such situation the interrupt routine wouldn't do anything, am I right? This is because the line which updates the "last interrupt time" variable doesn't depend on the result of "if". If we put the "last_interrupt_time = interrupt_time;" under the "if", so that it updates the "last interrupt time" only when the interrupt is succesfull, the hypothetical button will be debounced for 200 ms - button's second state change after 300 ms will be succesfull.

Sorry for poor English.

Go Up