Advantages not accessing hardware on every loop?

I have a conceptual question on which I'd appreciate some advice.

I have a sketch which, among other things, writes a pulse width modulation value to a pin (the PWM value controls the brightness of a string of LEDs via a BuckPuck).

When I wrote the sketch I included logic which causes the writing to the pin to not take place on every call to loop(). Instead, I use some logic to only update the pin's value every 5 seconds. I'm not using a call to delay() anywhere, I just monitor millis() and do the update every 5 seconds.

I was thinking that doing this would reduce the Arduino's power consumption. But upon further reflection I think that's silly -- the Arduino is busily running the loop, and if it takes less time in the loop because it's not updating the pin, all that's going to happen is that loop() will get called more frequently.

So now I'm thinking of eliminating that "event" logic, and just updating the pin on every pass through loop().

Is this a reasonable conclusion? Or are there advantages to not accessing external hardware as frequently (via updating the pin) that I'm not considering?

  • Mark

Is this a reasonable conclusion?

Yes.

Or are there advantages to not accessing external hardware as frequently (via updating the pin) that I'm not considering?

None that I'm aware of.

It's a microcontroller ... microprocessor + controller. The point is to interact with the outside world.

It's a microcontroller ... microprocessor + controller. The point is to interact with the outside world.

Good point ;D

All that you would be doing by calling analogWrite on every pass is changing/not changing the duty cycle associated with the pin. The pin is still being turned on and off at the same frequency.

If you are worried for any reason, simply don't call analogWrite unless the value to write is different from the last value written.

If you are worried for any reason, simply don't call analogWrite unless the value to write is different from the last value written.

That's what I've been taught: Don't speak unless you have anything new or interesting to say, don't do I/O unless you want to change something.

I guess most reasons for the I/O are moot these days, but it's a good practice to do anyway - see for example EEPROMS which die a little with every write. As for the speaking, it's still very necessary.

Korman

Or are there advantages to not accessing external hardware as frequently (via updating the pin) that I'm not considering?

Not being busy means time for other things - you could add & control more sensors, leds, etc - all within the same loop().