We have a really simple question when looking at millis. Everything I can find online seems to suggest millis will start counting from when the program starts running.
Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
The code below returns zero, where I would have expected it to be closer to 50. Thoughts?
Starting Program
0
unsigned long time = millis();
void setup() {
Serial.begin(9600);
Serial.println("Starting Program");
delay(50);
Serial.println(time);
}
void loop() {
//
}
The variable Time gets initialized to the value of millis() at the very start of the code (before setup() begins execution). This value does not update automatically, so it will remain at 0 until you assign a new value to Time.
Isn't it the 3rd step? I first run the print line, then a delay of 50ms, then read the time.
Hence, at start up time, it is very reasonable millis( ) will return 0 (zero).
I even added a 50ms delay, so it should show up? Even with a 5 second delay, I don't get a value.
it seems like I can't assign mills to a variable while initializing the global, or during setup. But if I assigned the variable in the loop, it seems to work fine.