So earlier this morning I decided to try and use an LCD display to output the measurements by an ultrasonic sensor. However I've been having issues with displaying the measurements.
Initially the sensor would change from 9cm to 10cm, but when going back to 9 it would display 90. Or if there wasn't something in the way before reset it would display 357, then change to 557 rather than just 5 when something is 5cm in front of it.
I found some solutions on this forum already, however none of them have worked. Here's a video with the problems with the solutions I've tried:
Any ideas on what I should do? Something I'm not understanding?
Edit: I want it to display 001 when something is 1cm away
Also here's the code from my best attempt (Fix 1 in the video):
Based on when it actually works I'm fine with it, 1 or 001 doesn't really matter to me as long as it's accurate. However, as shown in the video, it doesn't always work.
Start by posting your best attempt at solving the problem. Please use code tags when you post your code
I assume that you know what causes the problem and, if so, the solution should be obvious, ie print a space where the previously printed value was. Do this either before printing the new value or print print spaces at the end of the new value to overwrite the end of the old value
I note that the title of this topic is about leading zeroes. Is it that you actually have a problem with or old values not being removed when you print new, smaller values ?
I added some things I should have clarified and the debatably best version of my code.
I assume that you know what causes the problem
I do not
print a space where the previously printed value was. Do this either before printing the new value or print print spaces at the end of the new value to overwrite the end of the old value
I also don't know how to do that.
I note that the title of this topic is about leading zeroes. Is it that you actually have a problem with or old values not being removed when you print new, smaller values ?
Yes and no. Yes, old values aren't being removed. I was going to use leading 0s to fix the issue.
You have been given a solution but unless you understand the problem and the solution you will have learned nothing
The problem arises because when you print say 123 on the LCD at a particular position and later print say 99 at the same position then the final 3 of 123 is left in place so that you see 993 instead of 99
You can fix this in a number of ways including
print a couple of spaces after the number that you print so that all characters of the original number or overwritten
as above but only print the spaces if the new number has fewer digits than the previous number printed
always print some spaces at the position where you will be printing a new number so that the previous number is erased before printing the new one
format the number that you print so that it has either spaces or zeroes as its leading characters and is always the same length so that all characters of the previous value are overwritten. This is the approach that @david_2018 used in his solution