Duration of an interrupt service routine

Hi! Is is it somehow possible to determine how long it takes for Arduino to execute ISR every time that interrupt occurs?

I'm pretty beginner myself but what about recording the millis() function at the beginning and end of your ISR?

Use the method above for any code within the ISR, but you might also want to include the ISR setup/exit times. Nick Gammon has run some tests and found these to total 5.125us on a 16MHz board - http://www.gammon.com.au/forum/?id=11488

Alternatively, if you can control the rate at which the ISR is called -

  1. increment a counter variable within the ISR, e.g. count++
    2.in the main loop, wait a known amount of time e.g. delay(t*1000) , and then inspect the counter variable.
    3.increase the interrupt rate until you start getting underruns i.e. counter variable doesn’t increase - at that point you have execution speed - t/count.
  2. subtract from t/count the time taken to execute count++ as you’ll be removing this line from the ISR.

Use the method above for any code within the ISR, but you might also want to include the ISR setup/exit times. Nick Gammon has run some tests and found these to total 5.125us on a 16MHz board

That means that millis() will never show different values when used in an ISR. For two reasons - one is the the timer that millis() shows data for doesn't update during an ISR and the other is that the time scale that millis() would measure is too large.

The values returned by micros() do change in an ISR, and are 1/1000th the scale of those returned by millis().

PaulS: That means that millis() will never show different values when used in an ISR. For two reasons - one is the the timer that millis() shows data for doesn't update during an ISR and the other is that the time scale that millis() would measure is too large.

The values returned by micros() do change in an ISR, and are 1/1000th the scale of those returned by millis().

Thank you PaulS. micros() did the job. One more question. Is it possible that an ISR is changing some volatile variable so fast that when that variable is printed from void loop () {} , some values that I know this variable has, won't be printed?

s it possible that an ISR is changing some volatile variable so fast that when that variable is printed from void loop () {} , some values that I know this variable has, won't be printed?

Yes, it is.