Hi all!
As some of you might already have read in my previous post, I'm building a time lapse dolly controlled by an arduino.
So I've come to the next problem(s):
When the number of digits in a number start to grow (I mean 4 has 1 digit, 435 had 3 digits), the number gets larger on the right side. So when the number decreases from 10 to 9, 90 is printed on the LCD.
clearing the lcd every loop makes the numbers not showing up decently (because the time they are erased is longer than the time they are printed). An if value < 10 and then adjusting the cursor location would be a PITA...
Format the number to a string of the desired length, and then display the string. For example:
char buffer[8]; // make it big enough to hold your longest string, plus one byte for the null terminator
int number = 7;
snprintf(buffer, sizeof(buffer), "%4d", number);
// buffer now contains " 7"
guix:
It should replace the previous digits with spaces. Which LCD, and which library are you using ?
I use the arduino LCD library on a 16x2 LCD screen with a HD44780, so the arduino-regular one
And indeed, that should happen, but how? An if x < 10 statement followed by a command which makes a string looks tedious...
Using what PeterH said about sprintf If previous number is 1234 and new number is 56, the %4d thing will replace "1234" by " 56", overwriting "12" with spaces. No need of if statement. Or maybe I don't understand what you mean
PaulS:
If you simply print the number and then three spaces, the old number will be completely gone.
Yeah, but this has to be done for every value of the clock that is running, so I would need to specify an if-statement for when the number becomes larger or smaller.
guix:
Using what PeterH said about sprintf If previous number is 1234 and new number is 56, the %4d thing will replace "1234" by " 56", overwriting "12" with spaces. No need of if statement. Or maybe I don't understand what you mean
I think it might work, but I'm struggling to understand what's done, and so how to integrate it in my program...
Yeah, but this has to be done for every value of the clock that is running, so I would need to specify an if-statement for when the number becomes larger or smaller.
Wrong. You have a print statement now that prints the number. Just add another print statement after it that prints three spaces.
The thread title sounds like you are trying to right justify the number when printed. If so then printing 3 spaces after each number will not work.
Peter's suggestion will though. The snprintf() function formats a char array containing the number, then you can print the formatted buffer just like any other char. Try Peter's example, play with the number being printed and see what it does.
Yeah, but this has to be done for every value of the clock that is running, so I would need to specify an if-statement for when the number becomes larger or smaller.
Wrong. You have a print statement now that prints the number. Just add another print statement after it that prints three spaces.
Aha! I thought you said that I had to print spaces behind every value.
But It's the other way round, no? First 3 spaces, and then the value? otherwise the value will be overwritten by the spaces...
EDIT: I tried the way I thought, which was wrong, and you were indeed right... Why is the value visible for a longer time than the spaces?