LCD clear last character

Hi,
I made a counter was I showed on LCD 16x2 and the last character( zero) remain when I do decrease the counter
How I clear the last character ?
Thanks in advance

#define CLK 6
#define DT 7
int counter = 0;
int CLKstate;
int CLKLaststate;
#include <LiquidCrystal.h>
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {
   pinMode(CLK,INPUT);
   pinMode(DT,INPUT);
   pinMode(9,OUTPUT);
   lcd.begin(16,2);
   CLKLaststate = digitalRead(CLK);

}

void loop() {
  CLKstate = digitalRead(CLK);
  if(CLKstate != CLKLaststate)
  {
    if(digitalRead(DT) != CLKstate)
    {
    counter++;
    counter = constrain(counter, 0, 255);
  }
  else
  {
    counter--;
    counter = constrain(counter, 0, 255);
  }
   lcd.setCursor(0,1);
   lcd.print("counter= ");
   lcd.print(counter);

}
CLKLaststate= CLKstate;
   analogWrite(9,counter);
   }

You can do an lcd.Clear() before writing.

Write a space after the number to overwrite the previous digit if there is there

More sophisticatedly, write a space when the number has been decremented only if the number of digits has reduced

UKHeliBob:
Write a space after the number to overwrite the previous digit if there is there

More sophisticatedly, write a space when the number has been decremented only if the number of digits has reduced

Please post an exmple

In your code you could just print some extra spaces to overwrite old data, reset the cursor position and write new data.

      lcd.setCursor(0, 1);
      lcd.print("counter =        ");  // extra spaces overwrite old data
      lcd.setCursor(10, 1);
      lcd.print(counter);

write a space when the number has been decremented only if the number of digits has reduced

Although that is the sophisticated way of doing it frankly I would not bother. Just print a trailing space or spaces after the number. If the number is close to something else on the screen and hence there is a danger of overwriting it if a fixed number of spaces is written after a long number then consider writing a variable number of spaces based on the size of the number and forget about checking whether it is strictly necessary

Something like

lcd.print(number);
if (number < 10)
{
  lcd.print("   "); //3 spaces
}
else if (number < 100)
{
  lcd.print("  "); //2 spaces
}
else
{
  lcd.print(" "); //1 space
}

groundFungus:
In your code you could just print some extra spaces to overwrite old data, reset the cursor position and write new data.

      lcd.setCursor(0, 1);

lcd.print("counter =        ");  // extra spaces overwrite old data
     lcd.setCursor(10, 1);
     lcd.print(counter);

Thank You . This solution work

To help avoid flicker don't print the text again and only print the number when it has changed

Here is your code changed to reflect what (I think) UKHeliBob said in reply #7.

#define CLK 6
#define DT 7
int counter = 0;
int lastCount = 0;  // keep track of old count
int CLKstate;
int CLKLaststate;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);



void setup()
{
   pinMode(CLK, INPUT_PULLUP);  // need pullups on my test setup
   pinMode(DT, INPUT_PULLUP);
   pinMode(9, OUTPUT);
   lcd.begin(16, 2);
   CLKLaststate = digitalRead(CLK);
   lcd.setCursor(0,1);  // set to display on second row
   lcd.print("counter = ");  // persistant message
}

void loop()
{
   CLKstate = digitalRead(CLK);
   if (CLKstate != CLKLaststate)
   {
      if (digitalRead(DT) != CLKstate)
      {
         counter++;
         counter = constrain(counter, 0, 255);
      }
      else
      {
         counter--;
         counter = constrain(counter, 0, 255);
      }
      if (counter != lastCount) // only print to lcd if count changes
      {
         lcd.setCursor(9, 1);
         lcd.print("      ");  // overwrite old data
         lcd.setCursor(10, 1);  // reset cursor
         lcd.print(counter);
         lastCount = counter;  // save old count for next time
      }
   }
   CLKLaststate = CLKstate;
   analogWrite(9, counter);
}

That's what I had in mind

Print the fixed text once in setup() then only print data when it changes. If the screen has anything printed on it after the number then you have to be careful about how many blanking spaces you print to avoid overwriting any of it

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.