I'm trying to calculate the "drained" capacity from the battery each second so I can get the nearest possible capacity used.
This is how I'm getting this.
- dischargingCurrentSum - Is the sum of the "current" which I get every second, milliamps-second
For example
- 1s = 300mA
- 2s = 380mA
- 3s = 540mA
When I sum this it's 1220mAs
dischargingUpdateCount - Is the count of entries, to be precise each second is one entry, in this case, it will be 3, it helps me to determine the arithmetic mean.
currentTimeUnix - This is the current time, it the exact same time of calculation in milliseconds (each second)
dischargingStartTime - This is the time when discharging started also in milliseconds
/1000 - is to convert milliseconds to seconds
/3600 - is to get the mAh
Formula:
drainedMah = dischargingCurrentSum / dischargingUpdateCount * (currentTimeUnix - dischargingStartTime) / 1000 / 3600
I'm not sure what I have missed here, at the first time it counts normally, but after a while, it became way more than the actual battery capacity is.
dischargingCurrentSum
already the mAh value? In which case, you should not be summing it. \$\endgroup\$