Miscalculation of percent

Hello!

I am measuring weight with two load cells and hx711.

I am getting clean numbers of two weights (0,5 kg or 1 kg). I want to calculate how much percent of weight is on every cell, so 50% and 50% if I put 0,5 kg on every cell or 33% and 66% if I put 0,5 kg on one and 1 kg on another cell.

The funny thing is that I never get 100% as result. If I put only one weight on cell I can read load of 1 kg for example but percent is always 99% (should be 100%). Also, if I put 1kg on every cell I don't get 50% and 50% but 49% and 50%. I have no idea why :slight_smile:

I am using byte variable for percent and float for weight. I tried with int also but it didn't help.

I am calculating percent like this:

percent1 = ((weight1 * 100.0) / weightSum);
percent2 = ((weight2 * 100.0) / weightSum);

I just tried to multiply with 101 instead of 100 and now it works as it should, I get 50-50% or 100%. Anyway, I don't understand why.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

So we can see you setup and how you are measuring and calculating your weights.

Thanks.. Tom... :slight_smile:

Yes, that's the nature of using floating point math. You lose accuracy. You should scale up your values and use fixed point math.

Or you can round off the float before truncating it by adding 0.5 before you store it in an int.

johnwasser:
Or you can round off the float before truncating it by adding 0.5 before you store it in an int.

+1

Also, try defining percent1 and percent2 as floats. You should get very close to 100% now.

lg, couka