The millis() Clock
Part of the Timing Without Delay section of Coddy's Arduino journey. Lesson 1 of 18.
The board has a clock inside it. millis() returns how many milliseconds have passed since the board started: 0 at power-on, 1000 after one second, 60000 after a minute.
You never start or reset this clock. It counts on its own, even while your sketch is inside delay(), and you can read it at any moment:
Serial.println(millis()); // prints the time, in msUntil now, delay() was the only way to time something, and it stops the whole sketch while it waits. Reading the clock stops nothing: the sketch keeps running and checks how much time has passed.
Challenge
EasyPrint the board's clock once a second:
- in
loop(), printmillis()withSerial.println() - keep the
delay(1000)after it
Press Run and watch the numbers in the Serial monitor, then Check.
Try it yourself
void setup() {
Serial.begin(9600);
}
void loop() {
// print the time since the board started here
delay(1000);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Timing Without Delay
1millis()
The millis() ClockTimes in unsigned longBlink Without DelayTwo LEDs, Two SpeedsBlink While a Button TogglesA Light on a TimerPractice on your own: Arduino playground