I'm using a UNO with an HX711 to read from a load cell. It works as should and I get the correct readings in the serial monitor. The issue happens when it's displayed on the LCD.
My scale is reading in pounds at 1 unit increments. When testing the scale with anything under 10 pounds, it will weigh, display the correct weight and return to "0" on the LCD. If you go to 10 pounds or above - 2 digits, when the weight is removed the LCD will read like "02" where the "2" is tenths of a pound. If you go into the 100 pound range, when the weight is removed, the LCD will then have 3 digits on the screen, like "002" where the 2 is again 2/10 of a pound.
I would like the LCD to display only the whole number weight, all the time.
It's like once a digit gets "activated" on the LCD, it never turns off, just turns into a leading zero.
Read the forum guidelines to see how to properly post code and some information on how to get the most from the forum. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
The reason you are seeing incorrect numbers on the display is that printing a number with fewer digits starts at the same cursor position, and only overwrites the number of digits needed by the current number, leaving any additions digits already on the display as-is. As an example, printing 123 will put 3 digits onto the LCD display, then printing 4 will result in 423, because only the first digit is overwritten.
Do not use lcd.init() more than once - that is for initializing the display, not clearing it. If you need to clear the entire display, that is what lcd.clear() is for.
Adding 0.05 to a float will round to the nearest tenth, not the nearest whole number.
If you want to print only the integer part of a float, with rounding, you can use lcd.print(scale.get_units(), 0); , where the 0 specifies no decimal places.
Thanks david_2018. I understand what you're saying about the LCD position.
I used the lcd.clear() as you recommended and used the lcd.print(scale.get_units(), 0); as you also recommended. It's working close to what I want now. Only issue now is that "zero weight" is displayed as "-0" and from 1-9 going up is displayed as "10,20,30,40,50,60,70,80,90" with a trailing zero. The trailing zero would be fine down in that low range but would need a decimal as to not look like "tens of pounds" when it's actually just 1 through 9 pounds.
Look at reply #6, the code shows how to print spaces over the previous number to blank out the previous digits.
The -0 is likely the result of rounding a number slightly below zero. You will get negative weights when you have a tare weight set and have nothing on a scale.
I think I have it working as I need it to now. I was a little over concerned with the negative numbers and was going to write a trap for negatives but then it occurred to me that this is an "S type" load cell that will be used for compression and tension... So... I'm good.
But of course, you generally do not want to clear the entire display for two reasons, one being that then you have to re-draw everything and the other that doing so causes annoying flicker.
This is the first time I’ve ever worked with an Arduino. I’ve had quite a bit of experience writing different languages of code in the financial industry over the years but this is the first time interfacing this type of hardware with it. So far it’s been enjoyable and a good learning experience.
Thank everyone for their quick responses to my post.