I have voltage measuring on my LiPo battery that is defined full at 4.2V and empty at 3.2V. This reading is very accurate but I want to convert this to percentage numbers. I know that the percentage of energy left in the LiPo is not linear to the voltage levels, but I would be fine with defining a linear approximation of the percentage left. Something like (4.2V-3.2V)/100 would give me 1% increments.
The issue I'm facing is how would I program this to show the percentage? It seems like ridiculously bad coding to have a switch statement with 100 cases, each for every percentage like the pseudocode under:
Switch(BattVoltage)
case: BattVoltage > 4.19 "print 100%"
case: BattVoltage < 4.19 && BattVoltage > 4.18 "print 99%"
.
.
...
How would one do this in a smart manner?