When comparing the value of millis() numerous times per loop(), are there reasons for/against storing it in a variable once at the top of loop(), v.s. calling millis() each time?
The most basic example code "Blink Without Delay" is written as such:
void loop() {
//...
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// Do stuff
}
}
I don't really she why the example code above bothers storing millis() in a variable "currentMillis" at all. (I realize that in such a simple program, there would be no noticeable difference.)
I imagine that if I use a variable, the longer the duration of loop(), and the further down my use of the "currentMillis" variable, the more deviated the stored value will be.
I'm wondering the other ramifications of either method as well.
Does calling millis() directly hog resources of any sort? If not, I imagine it would be better to not use a variable, so as to get a more accurate time value and save a bit of memory.
if (millis() > interval) do_something; a_function_that_takes_10ms(); if (millis() > interval) do_something_else;
I'd like the do_something_else to be executed always after do_something. If I store the millis value, this is correct, otherwise who knows..