debouncing an interrupt trigger

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

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

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.

1 Like