From 7a6855d90edd97464792e9b6d069bfc889c4e475 Mon Sep 17 00:00:00 2001 From: macvenez Date: Thu, 5 Sep 2024 11:37:39 +0200 Subject: [PATCH] Better adc reading and USB recognition --- Power.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Power.h b/Power.h index 1e64991..fec7506 100644 --- a/Power.h +++ b/Power.h @@ -65,6 +65,7 @@ int bat_charged_samples = 0; bool bat_voltage_dropping = false; float bat_delay_v = 0; #elif BOARD_MODEL == BOARD_HELTEC32_V3 +#define BAT_V_MIN 3.4 #define PIN_VBAT 1 #define VBAT_READ_CNTRL_PIN 37 #endif @@ -138,7 +139,6 @@ void measure_battery() { } #elif BOARD_MODEL == BOARD_HELTEC32_V3 - battery_installed = true; battery_indeterminate = false; battery_state = BATTERY_STATE_DISCHARGING; battery_ready = true; @@ -148,15 +148,21 @@ void measure_battery() { delay(50); analogRead(PIN_VBAT); // to clear out potential wrong reads on first read int analogValue = 0; - for (int i = 0; i < 5; i++) + for (int i = 0; i < 8; i++) { - analogValue += analogRead(PIN_VBAT); // reading value to make an average over 5 values + analogValue += analogReadMilliVolts(PIN_VBAT); // reading value to make an average over 5 values delay(10); } - analogValue /= 5; // calculate average value digitalWrite(VBAT_READ_CNTRL_PIN, HIGH); - battery_voltage = (float(analogValue) / 4095.0) * 1.25 * (4.9); // at 2.5db attenuation range is 0-1250mV, 4.9 is voltage divider ratio - battery_percent = (battery_voltage - 3.4) * 125.0; + analogValue /= 8; // calculate average value + battery_voltage = (float(analogValue)/1000.0)* (4.92); // at 2.5db attenuation range is 0-1250mV, 4.9 is voltage divider ratio + battery_percent = (battery_voltage - BAT_V_MIN) * 125.0; + if(battery_voltage > 4.4){ + battery_installed = false; + }else{ + battery_installed = true; + } + if (battery_percent > 100.0) { battery_percent = 100.0;