From 383eff80119212f1030af7ab34de92b7bb6d0cb0 Mon Sep 17 00:00:00 2001 From: Sjef Verhoeven PE5PVB Date: Thu, 30 May 2024 11:40:34 +0200 Subject: [PATCH] Make battery readout more accurate (closer to reality) --- TEF6686_ESP32.ino | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/TEF6686_ESP32.ino b/TEF6686_ESP32.ino index c0e1e2a..d6fd0c0 100644 --- a/TEF6686_ESP32.ino +++ b/TEF6686_ESP32.ino @@ -416,6 +416,8 @@ void setup() { gpio_set_drive_capability((gpio_num_t) 21, GPIO_DRIVE_CAP_0); gpio_set_drive_capability((gpio_num_t) 22, GPIO_DRIVE_CAP_0); gpio_set_drive_capability((gpio_num_t) 23, GPIO_DRIVE_CAP_0); + analogReadResolution(12); + setupmode = true; EEPROM.begin(EE_TOTAL_CNT); if (EEPROM.readByte(EE_BYTE_CHECKBYTE) != EE_CHECKBYTE_VALUE) DefaultSettings(hardwaremodel); @@ -3667,14 +3669,22 @@ void ShowRSSI() { } void ShowBattery() { + static uint32_t batupdatetimer = 0; + if (millis() >= batupdatetimer + TIMER_BAT_TIMER) { batupdatetimer = millis(); } else { return; } - uint16_t v = 0; - if (!wifi) v = analogRead(BATTERY_PIN); + uint32_t adcSum = 0; + for (int i = 0; i < 16; i++) { + adcSum += analogRead(BATTERY_PIN); + delay(1); + } + + int v = adcSum / 16; + battery = map(constrain(v, BAT_LEVEL_EMPTY, BAT_LEVEL_FULL), BAT_LEVEL_EMPTY, BAT_LEVEL_FULL, 0, BAT_LEVEL_STAGE); byte batteryprobe = map(constrain(v, BAT_LEVEL_EMPTY, BAT_LEVEL_FULL), BAT_LEVEL_EMPTY, BAT_LEVEL_FULL, 0, 20); @@ -3688,7 +3698,7 @@ void ShowBattery() { tft.fillRoundRect(313, 13, 4, 6, 2, ActiveColor); } if (batteryoptions != BATTERY_VALUE && batteryoptions != BATTERY_PERCENT && battery != 0) { - tft.fillRoundRect(279, 8, (battery * 8) , 16, 2, BarInsignificantColor); + tft.fillRoundRect(279, 8, (battery * 8), 16, 2, BarInsignificantColor); } else { tft.fillRoundRect(279, 8, 33, 16, 2, BackgroundColor); } @@ -3701,9 +3711,8 @@ void ShowBattery() { batteryVold = 0; vPerold = 0; - if (!wifi && batterydetect) { - float batteryV = constrain((((float)v / 4095.0) * 3.3 * (1100 / 1000.0) * 2.0), 0.0, 5.0); + float batteryV = constrain(v / 4095.0 * 3.3 * 2.0, 0.0, 5.0); float vPer = constrain((batteryV - BATTERY_LOW_VALUE) / (BATTERY_FULL_VALUE - BATTERY_LOW_VALUE), 0.0, 0.99) * 100; if (abs(batteryV - batteryVold) > 0.05 && batteryoptions == BATTERY_VALUE) {