Weird problem with 20x4 LCD

Im having problems with an LCD not showing the correct numbers, it shows 640% instead of 64% , so there's an extra 0

ok.. This is the regular LCD screen

Blue:100% White:100% ( 3 digit number and no empty spaces)

But just after the "X" function starts, the "%" character stays at the same place, like this:

Blue:99 % White:99 % (2 digit number and one empty space)

So the main problem is that im getting 990% instead of 99%

Is there a way to write a code to do something like this:

So the idea would be that if a 3 digit number changes into a two digit number , something between 1 and 99 then, print the LCD with both numbers and add the "%" character just next to the two digit number, like this:

Blue:99% White:99%

Blue:10% White:20%

So in that case I wouldnt have an extra 0, because it would be covered by the "%" character.

I think the problem is due to an extra space between the characters..

Blue:99space% White:99space%

Your problem is in your code, line 34 I think, but I could be wrong.

Im sorry... I think It could be hard to debug the whole code and I don't want to bother people, that's why I asked that.. but what do I need to post to get an advice?
thanx in advance
:slight_smile:

A friend of mine suggested this:

if(ledVal = 100){

lcd.print(ledVal);}

else
{
lcd.print(" ");

lcd.print("ledVal");

}

What Im trying to do is to print the lcd from 0 to 100%, but I need to get the "%" character next to whatever number i get on the LCD, with no spaces.

like this :

1%
20%
100%

because i have it like this:

1 %
20 %
100%

I hope you guys can help me with that..

badstraw360:
Im sorry... I think It could be hard to debug the whole code and I don't want to bother people, that's why I asked that.. but what do I need to post to get an advice?
thanx in advance
:slight_smile:

Read the how to use this forum sticky post.
We need enough code to reproduce your problem then we can see where you are going wrong. This is not a problem people normally have.

Create a small program that shows the problem. You should be able to do this in few lines of code, most of which you can copy from your original.

What cursor positioning commands do you have when printing, do you clear the screen before printing, do you clear the line before printing, do you clear the area to be used before printing, are you printing a variable or text, what type is the variable ? We can see none of this at the moment.

Im using a 20x4 LCD..

ledval should be a value between 0-100 defined by a map function.

Im trying this, but the code freezes... that's within the loop

lcd.setCursor(0, 0);

  lcd.print("Blue:");
  if (ledVal = 100){

  lcd.setCursor(5, 0);

  lcd.print(map(ledVal, 0, 255, 0, 100));  // this would print a number between 0-100

  lcd.setCursor(8, 0);

  lcd.print("%"); 

  }

  if (ledVal <= 99){

  lcd.setCursor(6, 0);

  lcd.print(" ");
 
  lcd.setCursor(5, 0);

  lcd.print(map(ledVal, 0, 255, 0, 100)); 

  lcd.setCursor(8, 0);

  lcd.print("%"); 

  }

The code freezes.. what am i doing wrong?

It would be better to see the whole code .......

Why do you position the cursor to column 8 no matter how many digits there are in the number before printing the % ? Why not just print the number, however big it is, then print the % ?

Never use a single = in an if statement is is always ==

Print the number first and then print a % sign. What is wrong with that

Print the number first and then print a % sign. What is wrong with that

If you print 100%, then 99%, the display shows 99%%.

There are several ways to solve the problem. Using sprintf() for formulate the string with a fixed number of characters is the simplest. Others require slightly more lines of code but result a smaller overall sketch.

Whatever you do, keep in mind that you'll need to deal with the value dropping below 10, too.

Could you show me a code example please?

What Im trying to do is to print this on the LCD..

100%
99%
1%

instead of this:

99 %
1 %

Could you place a code example please?

if(ledVal < 100)
   lcd.print("  ");
if(ledVal < 10)
   lcd.print(" ");
lcd.print(ledVal);
lcd.print("%");

or

char buf[10];
sprintf(buf, "%02d%", lcdVal);
lcd.print(buf);

Not quite as correct but an old and simple hack from green screen days

lcd.setCursor(5, 0);
lcd.print(map(ledVal, 0, 255, 0, 100)); 

//Just print 2 extra spaces after the % sign
lcd.print("%  ");

That is all. You do not need set the cursor any differently for 1% or 100%

The problem is that when you print to the LCD it does not clear to the end of line.
You know your number is between 1 and 3 digits, so printing 2 spaces after the % will over write any characters left from the last run through.

Thank you all guys!!

You're giving me great ideas!!

well the thing is that no matter what code I use Im getting an extra character on the lcd just when the numbers are < 100

Here's an example:

Blue:100% // that's ok, there's no problem

But when the value changes into a 2 digit number i get this:

Blue:80%% You see? Im getting an extra "%"

So to get rid of the extra character im thinking that maybe i could add this code:

   lcd.setCursor(5, 0);
  
  if(ledVal == 100){
   lcd.print(ledVal); 
   lcd.print("%");
  }

   else if (ledVal < 99){
   lcd.print(ledVal);
   lcd.print("%");
   lcd.setCursor(7,0);
   lcd.print(" ");
   
   }

What do you think?

Edit... na it doesn't show anything on the screen..

Im testing your hack MattS-UK , thanks !! I'll let you know if it works..

Great I think its working!!!!! thank you!