Button triggered interrupt needs to run for x seconds

I've got a break to make button that triggers an ISR, read an Analog Pin Value, and outputs that to serial. It does that just fine.

My problem is how do I get that interrupt to keep reading that value for x amount of seconds? millis() is disabled in ISR's so I can't use it to exit a loop of readings. Everything I search for brings up interrupt timers which is how to trigger a interrupt on a timer, but I want the opposite. I want to be able to run an interrupt for a set amount of time once triggered.

It's the wrong approach. You should never use interrupts this way. Create a volatile global variable and use it as a flag. Set the flag in the interrupt routine (and do nothing else), and in your main code, test the flag until it is true. Then enter your loop with timed readings. When you're finished with that, reset (clear) the flag, and continue with tasks in your main loop.

By the way, you can call millis() from inside an interrupt handler. But I suspect it will not continue to increment while the interrupt is running. I use this in one of my programs, but never needed to read more than once because normally you want to complete the routine as quickly as possible.

aarg is right, interrupts are not meant to be used for delays, but just for the FYI, you could always use delayMicroseconds(), although it is only accurate up to 16383 uS...

Again, don't use this in your ISR because it is bad practice, but it is useful just for testing purposes...

, you could always use delayMicroseconds(), although it is only accurate up to 16383 uS...


If the button is being pressed by a human and you are timing things for periods measured in seconds you probably don't need to use an interrupt at all. Checking the buttonPin in loop() with digitalRead() should be quite adequate. Interrupts are normally for things that need to be detected within microseconds.

If you must use an interrupt take @aarg's advice and just use it to set a flag.


AWOL: :o

Don't make me feel bad about trying to educate the guy, its a tool to keep in the back pocket...