Better adc reading and USB recognition

This commit is contained in:
macvenez 2024-09-05 11:37:39 +02:00
parent 93c365dc48
commit 7a6855d90e

18
Power.h
View File

@ -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;