Sub topic 3
Printing at a steady rate without delay
This chapter assumes you know sketch structure (setup and loop), uploading a sketch, variables, if statements, and analogRead and digitalWrite. Serial communication sends bytes between the board and your computer over USB at a set speed, measured in baud.
Printing at a steady rate without delay
Calling delay() pauses the whole sketch. Use millis(), which returns the milliseconds since the board started, to print on a timer instead.
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 runs here without waiting
}Sending commands from the Serial Monitor to the board
Type into the Serial Monitor's input box and press Send. Check Serial.available() before reading, then read one byte with Serial.read().
const int LED_PIN = 13;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '1') {
digitalWrite(LED_PIN, HIGH);
} else if (c == '0') {
digitalWrite(LED_PIN, LOW);
}
}
}Newline characters added by the line-ending setting also arrive as bytes. The code above ignores them because it only acts on '1' and '0'.
Graphing values with the Serial Plotter
Open Tools > Serial Plotter. The plotter draws one line per label. Print label:value pairs separated by commas, ending each line with println, such as A0:512,A1:300.
Turning debug output on and off with macros
A macro is text that the preprocessor replaces before compilation. Wrap debug prints in #if DEBUG so that setting DEBUG to 0 removes them from the compiled sketch.
Serial buffers and the cost of printing
On AVR boards such as the Uno, Serial.print writes into a 64-byte transmit buffer. When the buffer is full, print waits, which blocks the sketch. At 9600 baud, about 960 bytes leave the board each second, so long lines inside a tight loop slow everything down.
Fixing garbled text, missing ports, and silent monitors
- Garbled text: The Serial Monitor baud rate differs from the value in Serial.begin. Set both to the same number.
- Missing port: Another program holds the port, or the cable carries power only. Close other programs and try a data cable.
- Silent monitor: Serial.begin is missing from setup, or the monitor's line ending does not match what the sketch expects.
> Do not rely on this list alone. Check each item in order.
Using other serial terminal programs
Any terminal program that opens the port at the same baud rate works, such as PuTTY, screen, or minicom. Close the IDE Serial Monitor first, because only one program can own the port at a time.
Exercise
Change the timed-printing sketch so it prints every 2 seconds instead of every 500 ms. What single line changes?
Show answer
Change the constant to const unsigned long PRINT_INTERVAL = 2000;. The rest of the sketch stays the same because the comparison uses PRINT_INTERVAL.