When input is 3.0 V the ADC value is 255 and 2.2 V for 180. Because it's unnecessary to be very accurate so I use simple equation y = ax +b to calculate the percentage of the battery.
100 = 255 a + b ........ (1)
0 = 180 a + b ........ (2)
Use 1 and 2 we can calculate a = 4/3 and b = -240.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint8_t battery_level_percentage = (uint8_t)((battery_level*4)/3.0)-240; |
Ref:
Float divide and integer divide in C.