I'm trying to implement a simple interrupt and am having trouble. I have a pushbutton causing an interrupt on pin 2. In the interrupt routine, I check the state of pin 2 with a digitalRead command to implement some debounce. When I remove the digitalRead command the interrupt works but with no debounce.
I am presuming that if you use digitalRead on the same pin that's being used for interrupt that the interrupt is cancelled. Can anyone shed some light on this?
Thanks
Hi leharker,
I am presuming that if you use digitalRead on the same pin that's being used for interrupt that the interrupt is cancelled.
what do you mean by cancelled?
if you posted the code it would be clearer what the problem is.
I'm guessing that you are using millis() or delay() in your interrupt function.
From the reference page:
Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
So, instead of doing the work inside the interrupt function, just set a flag variable to true and trap for that outside the interrupt function.
Something like this (fragment):
int state = false;
void loop()
{
if(state)
{
//Do stuff
state = false;
}
}
void intFunc()
{
state = true;
}
dmesser, you may be right, but it would be ok to read the millis value in the interrupt handler to see if the switch de-bounce time had passed. Millis will be correct when the interrupt handler is called. But as you say, if the code is waiting in the handler for millis to increase it will fail.
Yep. Seeing the interrupt function would help.
Happy new year, btw.
Thanks for the help folks.
I see that the problem is not reading the pin that messed me up it was using the delay command. I missed the note about using delay in interrupt routines.