This is an example of switching the state of something on and off using a button on an interrupt. The button is debounced to prevent noise from causing the switch state to flicker.
Since the millis() function does not update in an interrupt service routine and since calling the interrupt on CHANGE does not indicate the rising or falling state, the logic for debouncing a button is a little different here.
A button is attached to the interrupt 0 pin 2. A LED indicating the state of the button is attached to pin 13 and a LED indicating the state of an on-off condition (such as causing something happen or not happen in the loop() function) is attached to pin 12.
At the start of the sketch, the interrupt is attached to a routine called Rise that is fired when the button state goes from low to high. When this routing is called, we compare the value of millis() to the stored value of the last interrupt time. If the difference is less then debounceDelay, we change the state of the button and attach the interrupt to a service routine called Fall which is fired when the button goes from high to low.
Since the millis() function does not update during the interrupt service routine, the debounceDelay value can be very small since the noise created by the button will cause millis() to return values that are very close or exactly the same regardless of how long we spend in the service routine.
Created on November 17, 2009, 04:09