How to calculate percentage for battery level indicator?

123 Views Asked by At

I'm doing a hobby project which i want to display battery voltage in a OLED display. I have a battery indicator with 30 pixels width.

i want to display when battery is 8.4V full 30 pixels width and when battery is 6v, 0 pixels.

currently I'm calculating how many pixels i want to display as below

indicatorWidth = (((voltage / 8.4) * 100) * 30) / 100;

When battery is at 6v this shows 21 pixels.

how do i fix this to show 0 width when at 6v and 30 width at 8.4v?

Regards

1

There are 1 best solutions below

0
On BEST ANSWER

You subtract $6$ from $v$. Now the range is only $2.4$, so your width would be $\frac {v-6}{2.4} \cdot 30$ The problem with this is that battery capacity is not linear in voltage, but it answers the question you asked.