Hi - I'm liking interrupts more and more and am trying to use an interrupt with a switch. It seems though that the switch I am using is a little noisy and I am getting 2 interrupts for what seems 1 switch event. Since I can't use delay() in an interrupt is there another way to debounce a switch?
I guess I can set a flag and then check it in loop() but that's no different than simply polling a pin state - so why bother with the interrupt.
Yes, that can be a problem when using mechanical switch contacts with a interrupt input. Of course using external components to eliminate contact bounce is one solution. Sometimes it's as simple as hanging the right size cap from the input to ground. There are also active components, cross coupled gates, direct set/reset flip flops, etc.
you can use a global variable and store the actual time (from millis()) there. If the actual vale of millis() is > stored value + 50ms (for example) this is a new keypress, otherwide store the new value and return without any action.
Good idea, Mike. And remember to define the global variable as volatile to tell the compiler's optimizer not to make any assumptions about it.
I guess I can set a flag and then check it in loop() but that's no different than simply polling a pin state - so why bother with the interrupt.
Using an interrupt all but guarantees that your button will be recognized, even if you wait to handle it a little later in loop(). Any flag set in the interrupt routine is not "un-set" when the button is released.
Polling a pin state in loop(), on the other hand, depends on the button still being held down when the loop() logic gets around to checking it. Depending on how complex your logic is, and whether it contains any delays, the button could be released by the time your logic gets to the part of the code that checks the pin state.
Interrupts are a better deal, and as long as you already have one, you should look for ways to continue to use it.