LCD issue: 3 digit number --> 2 digit number

I'm having some issues displaying a sensor value on a hitatchi LCD screen using the lcd.print command.

For example, when displaying '53' it shows up correctly. Then the sensor value changes to '142' and again it shows up correctly. But when the sensor value drops back to a double digit number there is still remnants of the 3 digit number, so instead of seeing '53' the LCD shows '532'.

By clearing the screen with the lcd.clear() function before using lcd.print the LCD works as expected. However this clears the entire display, containing other information that should remain unchanged.

Is there a way to just clear for example the bottom half of my 16x2 display?

#include <LiquidCrystal.h>
//LiquidCrystal(RS, Enable, DB4, DB5, DB6, DB7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorPin = 0;    // pin the pot is connected to
int sensorValue = 0;    // value of pot

void setup() {
  lcd.begin(16, 2);
  lcd.noCursor();
  lcd.print("Sensor value: ");
}

void loop() {
  sensorValue = analogRead(sensorPin);    // read analog input to sensorValue
  lcd.setCursor(0, 1);
  //lcd.clear();
  lcd.print(sensorValue);
  delay(100);  // make display more readable           
}

Just print a few spaces in the position before printing the value.

void loop() {
  sensorValue = analogRead(sensorPin);    // read analog input to sensorValue
  lcd.setCursor(0, 1);   //<<<<<<<<
  lcd.print("   ");          //<<<<<<<<
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  delay(100);  // make display more readable
}

Gordon

I believe this will also work, just appending the spaces onto your value.

void loop() {
  sensorValue = analogRead(sensorPin);    // read analog input to sensorValue
  lcd.setCursor(0, 1);
  lcd.print(sensorValue);
  lcd.print("   ");          //<<<<<<<<
  delay(100);  // make display more readable
}

You could always do an lcd.clear() before setting the cursor and printing the new value.

digimike:

You could always do an lcd.clear() before setting the cursor and printing the new value.

Perhaps you should reread his original post.

Don

:-[ good point.

Thank you gordon and crook! Both solutions work excellently!

I'm more of a hardware guy, this software is all new to me so sometimes I miss even the obvious solutions.