Question on attachInterrupt function?

Hiya,

OK so I am using the attachInterrupt function on the Arduino for the first time today and it is mostly going smoothly. However I have a quick question, if I have a push button attached to interrupt pin 0 (digital pin 2) which when pressed runs a function, if I push that button attached to interrupt pin 0 again during the function being ran by the interrupt, will it interrupt and restart the function?

Sorry if that is not clearly explained! Another way to put it, can you use the button your using to interrupt during the function the interrupt runs?

Haha, thanks in advanced! :slight_smile:

Only one interrupt runs at a time. One interrupt of each type can be queued while an ISR (interrupt service routine) is running.

So if you push the button again while the interrupt is running, it will be queued and run when complete.

However, no ISR should run so long that it would be possible to push a button twice. ISRs should do something quick and simple like set a flag which is picked up by the main program loop.

if I push that button attached to interrupt pin 0 again during the function being ran by the interrupt, will it interrupt and restart the function?

No.
Interrupts are automatically disabled when an ISR is run. In any case an ISR should be as short as possible so reading pins is not the sort of thing that you want to do. More likely you will set a flag or change a variable to indicate that the ISR has been triggered and respond to the flag/change in the main routine.

Hey,

Thanks for the quick replies, this has answered my question perfectly. However out of interest, why not stay in an interrupt for ages, does it do damage at all or just not good coding? it is the only way that I could think of at the time to get the Arduino to do what I wanted but with the flag idea I may have just thought of another way.

Thanks!

why not stay in an interrupt for ages, does it do damage at all or just not good coding?

It does no damage but it's a special state and all the other interrupts are disabled meanwhile. This results in millis(), delay() and other stuff is not working, you cannot print to the serial interface, you cannot use the I2C or SPI interface, you're more or less caught there.

Interrupt handlers should be very small and fast. Everything that takes more than a few microseconds should not be done in the interrupt handler but moved to the loop and activated by a flag variable.

SamuelCB:
not stay in an interrupt for ages

Your orginial question demonstrates why. You don't want to be sitting in an Interrupt missing other events.