Go Down

Topic: How to clear "extra" characters on Liquid Crystal ? (Read 202 times) previous topic - next topic

timberrock

I am hoping someone has solved this problem previously, or can identify what I am doing wrong. I want to display a data field and a text field on one row of a 1602 lcd. I can do that, BUT...for displaying a motor rpm, that will vary the number of digits from 3 to 4 digits...when the data increases from 3 to 4 digits, the text shifts over 1 column, then when the data reduces from 4 to 3 digits, I've got a leftover text character...so, remove the text...then when the data reduces from 4 to 3 digits, I've got a leftover data digit.  Photo panel 1 shows the display with 4 data digits and 'rpm' text. Photo panel 2 shows 3 data digits and 'rpmm' text. How do I clear that. I've looked through the lcd library without success. And obviously lcd.clear doesn't work in void loop as you will never see anything on the lcd. Thanks to respondents.

Here is the pertinent part of the sketch:
void setup() {
  lcd.begin(16,2); // set the lcd display to 16 columns and 2 rows
  pinMode (pulsePin,OUTPUT);
  pinMode (pulseResistor,INPUT); 
   
  lcd.clear();
  delay(2500); // set a delay before the 'ignition' loop starts
}

void loop() {
  lcd.setCursor(0,0);
 
  float pulseFactor = analogRead(pulseResistor); // needs to be 'float' or won't work correctly
  pulse(); // call subroutine
 
  pulseVary=15+75*(pulseFactor/1023);
  // because pulseTimeLo=100ms-10ms(HI pulse)-15ms(LOW pulse) 
 
  delay(pulseVary);
  int rpm=2400-1800*(pulseFactor/1023);
 
  lcd.print(rpm);
  lcd.print(" rpm");
 
}

timberrock

OK...photo now attached...I hope

bperrybap

If you don't care about the text jumping around and moving left & right just print additional spaces after the m in "rpm"

Code: [Select]

lcd.print(rpm);
lcd.print(" rpm    ");



but if you want the text "rpm" to stay in a fixed position and right adjust the number then you will have to either print extra spaces or zeros when the number is smaller before you print "rpm".

i.e.
Code: [Select]

if(rpm < 1000)
        lcd.print("0");
if(rpm < 100)
        lcd.print("0");
if(rpm < 10)
        lcd.print("0");
lcd.print(rpm);
lcd.print(" rpm");


Alternatively you could use sprintf and let it do all the formatting for the entire line:
Code: [Select]
char buf[16];
sprintf(buf, "%04d rpm", rpm)
lcd.print(buf);



--- bill

timberrock

Bill,
Thanks very much for the tips. Yeah, not too much concerned if the text jumps around as long as it's what it should be. A few ideas to play with...again, thanks. John

Go Up