@fanoush You're right, only hi should be needed. I'm now experimenting with trying to get getBattery to be more accurate since volts/% is not linear. My daughter noted that her watch was claiming 40% remaining battery shortly before it shut down. Based on a discharge graph shown here https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages, I've come up with the following:
E.getBattery = function () {
const hi = 0.48;
var v = analogRead(D3) / hi * 4.2;
if (v >= 4.2) {
return 100;
} else if (v >= 4.1) {
return Math.round(97 + 30 * (v - 4.1));
} else if (v >= 4.0) {
return Math.round(87 + 100 * (v - 4.0));
} else if (v >= 3.9) {
return Math.round(75 + 120 * (v - 3.9));
} else if (v >= 3.8) {
return Math.round(59 + 160 * (v - 3.8));
} else if (v >= 3.7) {
return Math.round(26 + 330 * (v - 3.7));
} else if (v >= 3.6) {
return Math.round(12 + 140 * (v - 3.6));
} else if (v >= 3.5) {
return Math.round(6 + 60 * (v - 3.5));
} else if (v >= 3.4) {
return Math.round(3 + 30 * (v - 3.4));
} else {
return 0;
}
};
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@fanoush You're right, only hi should be needed. I'm now experimenting with trying to get getBattery to be more accurate since volts/% is not linear. My daughter noted that her watch was claiming 40% remaining battery shortly before it shut down. Based on a discharge graph shown here https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages, I've come up with the following: