Live Numbers on the LCD
Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 8 of 24.
lcd.print() prints numbers too, and a variable or a calculation works as well as text:
lcd.print(millis() / 1000); // seconds since the board startedTo keep a number up to date, print it again in loop(). But every print moves the cursor on, so a second print lands after the first: 0, then 00, then 000. Move the cursor back with lcd.setCursor() before each print, and the new number is written over the old one.
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
delay(200);
}The short delay() keeps the screen from being redrawn thousands of times a second for a number that changes once a second.
Challenge
EasyThe top row says Seconds:. Under it, show how many seconds the board has run:
- in
loop(), move the cursor to the start of the bottom row - print
millis() / 1000 - wait 200 ms
Press Run and watch it count, then Check.
Try it yourself
#include <LiquidCrystal.h>
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Seconds:");
}
void loop() {
// show the seconds since the board started on the bottom row
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Displays and Sensors
1Seven-Segment Displays
Seven-Segment DisplayDigit Patterns in BytesCount on the DisplayCountdown to ZeroDice Roller2Character LCD
Hello on the LCDPlace Text with setCursorLive Numbers on the LCDClear Leftover DigitsDraw Custom CharactersScrolling TextPractice on your own: Arduino playground