Can I save power using interrupts to run code every 1ms instead of busy waiting?

Here is some excerpt code from a glowstick. It is part of the loop(), and if the user is not currently pressing any buttons, it’s pretty much all the glowstick does:

    for (int i = 0; i < NUM_LIGHT_MODES; ++i)
    {
        if (lightModes[i].enabled &&
            millis() >= lightModes[i].nextOccurrence)
        {
            lightModes[i].apply();
            lightModes[i].nextOccurrence = millis() + lightModes[i].interval;
        }
    }

lightModes[ i].apply(); is a function pointer, and runs some code, but I’m fairly sure even if all the light modes were enabled, this code wouldn’t amount to more than 1ms execution time.
So the rest of the time, it’s just spinning, checking if millis() has increased enough to run any of the light modes again.

I have seen timer interrupts used to run a function at regular intervals. If I just ran this for(;:wink: loop once a milisecond, and left the loop() function empty, does anyone know if this would save any power (as it’s battery-operated, and I’d like to maximise battery life, while still using the arduino code I’ve written)?

Essentially, does spinning in a do nothing loop use less power than repeatedly checking an array of longs against the return value of a function?

I’m not necessarily expecting anyone to know this for certain, but I’d be keen to hear from anyone who has a vague idea.

Thank you in advance!

Rob

Essentially, does spinning in a do nothing loop use less power than repeatedly checking an array of longs against the return value of a function?

No. If the CPU is executing instructions, it’s using power.

What you need to do is use an interrupt to initiate your function and the rest of the time do a power down sleep. The processor uses less power when asleep.

Pete

So, if I move all the code from loop() to a function which is run by an interrupt, and then put the processor to sleep at the end of this function, might that save power? it's worth mentioning that I'm using the analogWrite() function to pwm some LEDs, so not sure if sleep is an option here. Does pwm require the main processor to be executing, or just the timers (which I believe continue running in some sleep modes)?

Rob

The PWMs just use hardware timer modules so dont need the CPU. You can set the processor to IDLE mode which disables the CPU clock, but leaves timers, USART, SPI etc. still running. You could then have a timer which triggers the processor to wake from sleep mode every millisecond. In the interrupt do your checks, and then put it back into IDLE mode at the end of the ISR. In IDLE mode, the chip will use approx. 25% of the power it consumed during non-sleep.

Usually for sleep you run the code in the main loop. At the start of (or end of, or specific point in) the loop you send the processor to sleep.

A timer then triggers an interrupt which causes the processor to wake up. The processing continues with the next instruction after the sleep instruction.

You don't actually have to do anything within the interrupt routine at all.

Thanks for the replies guys - very helpful :-) [SOLVED]