Menu
CoddyTech

Clear Leftover Digits

Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 9 of 24.

Writing over a number only replaces as many characters as the new number has. Print 10, go back, print 9, and the screen shows 90: the 9 covered the 1, and the 0 was never touched.

There are two fixes. The quick one: print a few spaces after the number, so they cover whatever a longer number left:

lcd.setCursor(0, 1);
lcd.print(reading);
lcd.print("    ");   // covers up to four old digits

The other is lcd.clear(), which wipes the whole screen and puts the cursor at the top left. It also wipes everything else you printed, so you have to print it all again, and a screen cleared many times a second flickers. For one changing number, the spaces are better.

challenge icon

Challenge

Easy

The bottom row shows the knob reading, but turn the knob down from 1023 and old digits stay behind. Fix it:

  • after printing the reading, print spaces to cover leftover digits
  • keep Knob: on the top row

Press Run, turn the knob up and down, 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("Knob:");
}

void loop() {
  int reading = analogRead(A0);
  lcd.setCursor(0, 1);
  lcd.print(reading);
  // a shorter number leaves old digits behind: fix it here
  delay(100);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Displays and Sensors

Practice on your own: Arduino playground