How should I blink and LED using an interrupt?

I am using an Arduino on an I2C connected as a slave to a Pi. When the Arduino receives a command, I want it to blink x number of times. The problem I am concerned about is that the interrupt routine should be as short as possible and avoid delays. How should I execute a series of blinks that will occur over >15 seconds?

What is the rule of thumb for interrupt routines? Is there a max number of cycles I should stay within?

This is not a problem that requires an interrupt.

Load up the Blink_Without_Delay example sketch. Study it. Try to add a counter so that it only blinks 5 times. Add some serial or I2C code to reset the counter.

The problem is that I have to trigger the function from within the request interrupt from the I2C.

pekasus: The problem is that I have to trigger the function from within the request interrupt from the I2C.

Set a flag inside your interrupt routine. Then read the flag and respond in the regular loop() function.

@ChrisTenone thanks. That makes sense. What is the rule of thumb for the longest that an interrupt should run? 1 second? Shorter?

pekasus: @ChrisTenone thanks. That makes sense. What is the rule of thumb for the longest that an interrupt should run? 1 second? Shorter?

Usually way shorter. It depends on the rate and randomness of whatever your interrupt is doing. For example, I use an interrupt to read the state of an 'atomic clock' radio. While the signal pulse happens once a second, the length of the pulse is important, and can be as short as 0.2s. Additionally, I want to detect noise, which can be a couple orders of magnitude shorter than that.

So my ISR consists of a single line of code: radioInputState = digitalRead(radioInput); This takes a few microseconds to run, so it fits within the shortest pulse I care about.

Ok. Thanks for the explanation.

Also consider what is going to happen if you receive another interrupt whilst you are still servicing the first one. Not in the ISR but in the main loop; that is, you are still flashing the LED and another interrupt occurs.

What are you going to do in the main loop if that occurs? That's an issue for long running processes (and your LED flashing routine could be considered a long-running process in this context).

pekasus:
What is the rule of thumb for the longest that an interrupt should run? 1 second? Shorter?

100 microseconds would be long.

…R

I'd go as long as a millisecond if that was literally the most important thing running on the Arduino and every other part of the program could be written to cope with that huge block of time randomly disappearing.

For most normal interrupts, keep it down to a few microseconds. That means only a few tens of machine instructions. Save a millis() value, update a flag variable and get out of there.