Sub topic 9
Printing at a Steady Rate Without `delay()`
Assume you know setup(), loop(), variables, if statements, analogRead(), and basic Serial Monitor use. This chapter shows how to print sensor data at regular intervals while allowing the rest of the sketch to keep running.
Why `delay()` Can Be a Problem
delay() pauses the processor for the specified number of milliseconds:
Serial.println(analogRead(A0));
delay(500);This prints twice per second, but the Arduino cannot perform other work during the delay. It cannot respond quickly to a button, update an output, or read another sensor.
A non-blocking approach checks the current time and prints only when enough time has passed. The sketch can continue through loop() between prints.
Comparing `millis()` with the Last Print Time
millis() returns the number of milliseconds since the board started running the sketch. Store the time of the previous print, then compare it with the current time.
unsigned long lastPrint = 0;
const unsigned long PRINT_INTERVAL = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
if (now - lastPrint >= PRINT_INTERVAL) {
lastPrint = now;
Serial.println(analogRead(A0));
}
// Other work can run here on every pass through loop().
}PRINT_INTERVAL is the desired time between readings. In this example, the interval is 500 milliseconds.
The subtraction form is important. Arduino’s millis() value eventually reaches its maximum value and wraps back to zero. Using unsigned values and now - lastPrint continues to work across that wraparound.
| Expression | Meaning |
|---|---|
| `millis()` | Current elapsed time in milliseconds |
| `now - lastPrint` | Time since the previous print |
| `>= PRINT_INTERVAL` | Whether it is time to print |
Choosing the Time Update
The example sets lastPrint = now after a print. This is simple and usually suitable for sensor display.
If the work inside the timed section can take a long time, repeated intervals may drift slightly. For a task that should follow a fixed schedule, use:
lastPrint += PRINT_INTERVAL;This preserves the planned timing, but it can cause several immediate runs if the sketch falls far behind. Choose the update style based on the task:
- Use
lastPrint = nowwhen each interval should begin after the previous operation. - Use
lastPrint += PRINT_INTERVALwhen maintaining a regular schedule matters more.
Running Several Timed Tasks
Each independent task needs its own timestamp and interval.
unsigned long lastSensorRead = 0;
unsigned long lastStatusPrint = 0;
const unsigned long SENSOR_INTERVAL = 100;
const unsigned long STATUS_INTERVAL = 1000;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
if (now - lastSensorRead >= SENSOR_INTERVAL) {
lastSensorRead = now;
int reading = analogRead(A0);
Serial.print(F("sensor="));
Serial.println(reading);
}
if (now - lastStatusPrint >= STATUS_INTERVAL) {
lastStatusPrint = now;
Serial.println(F("running"));
}
// Button checks and output updates can run here too.
}Avoid using one timestamp for unrelated tasks. A single timestamp can make one task prevent another task from running at its intended rate.
Exercise
Change the first example so that it prints the value of A0 every 100 milliseconds and prints the text "heartbeat" every 1000 milliseconds without using delay().
Show answer
Use two timestamps and two interval constants. Check each interval separately inside loop(), updating its timestamp when that interval has elapsed.