LCD: Getting rid of old numbers when a value drops

Hi there. I'm new to the Arduino and electronics in general. When I loop a variable to be printed on my LCD, it works until the value decreases in numbers. Instead of goin from 100 to 99, it goes to 990. My question is: What's the most efficient way to fix this? Also, how do I make it expand to the left instead?

Thank you for your time :) -EB

Simplest way is to print 3 spaces after it!

lcd.print(number) ; lcd.print(" ") ;

You could use sprintf to print the value to a string (with a fixed width), and then print the string, instead.

char buffer[10];
sprintf(buffer, "%4d", intVal);

The default is right justification, so for intVal = 1, 10, 100, and 1000, the string in buffer would look like: ___1 __10 _100 1000 (Where the _ represents a space)

PaulS, You really are a mind full of useful info. I will try to remember that. :)


You really are a mind full of useful info.

Useless, too. But I keep that to myself. ;D

PaulS. That's interesting, but doesn't work for me. I did replace intVal with my own int variable, but I only get a blank on the display. Have you tried this yourself? Also, do you think you could explain the sprintf function a little? Searching didn't help me. I know modulo, but what's 4d? Thanks!

I would have printed blanks over the old number first and then printed the new numeric value, but PaulS's solution looks good.

I would have printed blanks over the old number first and then printed the new numeric value, but PaulS's solution looks good.

Well I'd normally work out how big the value is and print corresponding blanks.

If you just print over the whole number the the max refresh of the number slows down.


but what's 4d

Print as 4 decimal digits.

% is not modulo in this case, it's a meta character that flags the following format string.

You could use google to find information about sprintf. Basically, it is part of the printf family. In the case of sprintf, it "prints" formatted data to a string. There are 2 or more arguments. The first is the string to print to (a character array). The 2nd argument defines how to print to the file (the format). If there are additional arguments, they are the values to print.

The format specifier starts with a % sign, and is followed by some optional and some mandatory fields. In the case of %4d, the d is an integer output specifier. That is, the value to be printed will be an integer. The optional 4 defines the minimum number of characters in the resulting string. Since there are no other values present, the field will be right-justified, space-filled to a minimum of 4 characters.

If the sprintf command you used did not provide the expected output, there are two possibilities. One is that you used it wrong. The other is that the string to be written to was not large enough. We'd need to see some code to help you determine which it is.

After some re-reading, thinking, and experimenting I kind of understand what the code does, and realized I simply forgot to lcd.print(buffer); afterwards ::) It's just very hard to interpret the lingo of something you're a complete beginner at, and in a different language at the same time. Google didn't give me much, until I realized I had been searching on only the site, doh!

Thank you for your input everyone. Much appreciated.

I had a similar issue with the LCD not clearing. I added 5 blank spaces as outlined above. Does what I need.

Thanks Squiggler.

-- M.S.

Don’t want to double-post. Check this post out:

Using sprintf is the best way to organize output but also the most resource-consuming, lcd.print(number) is the next, only without control over how many digits dot how many digits.

I’m working on a code with less memory footprint to do integer output. Code is on that post.