millis() and interrupts

Hi,

I'm writing a code using millis() in de void loop.

I also want to use interrupts to read out an encoder signal. In the text bellow (copied from this site) there is something about millis() using interrupts and not working while an interrupt is running. I don't understand it. Does it mean I cannot use millis() in the code when I want to use interrupts and vice versa?

"Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be ignored (turned off) until the current one is finished. as delay() and millis() both rely on interrupts, they will not work while an ISR is running. delayMicroseconds(), which does not rely on interrupts, will work as expected."

Thanks,

Leo

Does it mean I cannot use millis() in the code when I want to use interrupts and vice versa?

No, it doesn't say that. It says that you can't use millis() to create a delay IN AN ISR. And, you can't use delay() IN AN ISR.

mechatron: I also want to use interrupts to read out an encoder signal. In the text bellow (copied from this site) there is something about millis() using interrupts and not working while an interrupt is running. I don't understand it. Does it mean I cannot use millis() in the code when I want to use interrupts and vice versa?

You can use millis() where you want to, either in your normal code or in an interrupt handling routine.

BUT: millis() is not counting up during an interrupt handling routine, the millis() counter is blocked during your interrupt code is running. If your interrupt handling should run (beware!) even longer than about one millisecond, the millisecond counting on the Arduino will become incorrect: Your Arduino will not count every millisecond, but milliseconds get lost forever, if interrupt handling takes too long.

Ok, thanks.

How can I determine the time it takes to handle a certain interrupt? Is it x microseconds a line or something like that?

mechatron: How can I determine the time it takes to handle a certain interrupt? Is it x microseconds a line or something like that?

Depends on the code you write into an interrupt handling.

The overhead for calling and ending an interrupt is about 4µs.

A simple "digitalRead()" will take also about 4µs.

An "analogRead()" will take about 115µs.

And small calculations like adding two "int" variables is less than 1µs.