# Calculating LiPo balance

I want to calculate LiPo balance by measuring all cells with voltage dividers. It works so far, but I have one problem...

The balance is calculated from the lowest and the highest cell of the pack. So I have..

L = lowest cell H = highest cell B = balance

I now use this code...

B = ((L) / ((H) / 100));

When for example L = 405 and H= 409, with a normal calculator I get 99,02% balance, thats correct. (405 / 4.09) But the arduino does not seem to calculate decimals, so it calculates 405 / 4 = 101% balance, thats incorrect. Is there a way to make a code that outputs a number like 9902? Or just 99? A number like that I can easily print on a lcd screen like 99.02% or 99% I can't find a way.

I assume that B, H & L are int. Declare them as float instead & replace 100 with 100.0

Thanks, it worked!
Learned something today!

mwhens: I now use this code...

B = ((L) / ((H) / 100));

Another way to rework that formula is:

``````B = ( 100 * L ) / H;
``````

Which swaps a multiply for a divide, a real time savings with Arduino's floating point.

By the way, if speed is an issue, you can do this math in integers - just assume the final result is *100, and use this formula:

``````unsigned long L, H, B100;
B100 = ( 10000 * L ) / H;
``````

Using your original numbers, you'll get 9902 instead of 99.02 - but it's a whole lot faster than floating point, and you can insert your own decimal when printing it out easily enough.