Skip to content

Commit

Permalink
Merge pull request #3964 from adamsthws/0_15_battery_usermod_init_imp…
Browse files Browse the repository at this point in the history
…rovement

Update Usermod: Battery
  • Loading branch information
blazoncek committed May 11, 2024
2 parents c016ded + bc5aadf commit 522e5e7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions usermods/Battery/battery_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
#endif
#endif

// The initial delay before the first battery voltage reading after power-on.
// This allows the voltage to stabilize before readings are taken, improving accuracy of initial reading.
#ifndef USERMOD_BATTERY_INITIAL_DELAY
#define USERMOD_BATTERY_INITIAL_DELAY 10000 // (milliseconds)
#endif

// the frequency to check the battery, 30 sec
#ifndef USERMOD_BATTERY_MEASUREMENT_INTERVAL
#define USERMOD_BATTERY_MEASUREMENT_INTERVAL 30000
Expand Down
5 changes: 5 additions & 0 deletions usermods/Battery/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define `USERMOD_BATTERY` in `wled00/my_config.h`
| ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- |
| `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp |
| `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO35 on ESP32 |
| `USERMOD_BATTERY_INITIAL_DELAY` | ms | delay before initial reading. defaults to 10 seconds to allow voltage stabilization
| `USERMOD_BATTERY_MEASUREMENT_INTERVAL` | ms | battery check interval. defaults to 30 seconds |
| `USERMOD_BATTERY_{TYPE}_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) |
| `USERMOD_BATTERY_{TYPE}_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) |
Expand Down Expand Up @@ -88,6 +89,10 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3.

2024-04-30

- improved initial reading accuracy by delaying initial measurement to allow voltage to stabilize at power-on

2024-04-30

- integrate factory pattern to make it easier to add other / custom battery types
- update readme

Expand Down
28 changes: 25 additions & 3 deletions usermods/Battery/usermod_v2_Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class UsermodBattery : public Usermod
UMBattery* bat = new UnkownUMBattery();
batteryConfig cfg;

// Initial delay before first reading to allow voltage stabilization
unsigned long initialDelay = USERMOD_BATTERY_INITIAL_DELAY;
bool initialDelayComplete = false;
bool isFirstVoltageReading = true;
// how often to read the battery voltage
unsigned long readingInterval = USERMOD_BATTERY_MEASUREMENT_INTERVAL;
unsigned long nextReadTime = 0;
Expand Down Expand Up @@ -137,7 +141,6 @@ class UsermodBattery : public Usermod
if (pinManager.allocatePin(batteryPin, false, PinOwner::UM_Battery)) {
DEBUG_PRINTLN(F("Battery pin allocation succeeded."));
success = true;
bat->setVoltage(readVoltage());
}

if (!success) {
Expand All @@ -148,10 +151,10 @@ class UsermodBattery : public Usermod
}
#else //ESP8266 boards have only one analog input pin A0
pinMode(batteryPin, INPUT);
bat->setVoltage(readVoltage());
#endif

nextReadTime = millis() + readingInterval;
// First voltage reading is delayed to allow voltage stabilization after powering up
nextReadTime = millis() + initialDelay;
lastReadTime = millis();

initDone = true;
Expand All @@ -178,6 +181,25 @@ class UsermodBattery : public Usermod

lowPowerIndicator();

// Handling the initial delay
if (!initialDelayComplete && millis() < nextReadTime)
return; // Continue to return until the initial delay is over

// Once the initial delay is over, set it as complete
if (!initialDelayComplete)
{
initialDelayComplete = true;
// Set the regular interval after initial delay
nextReadTime = millis() + readingInterval;
}

// Make the first voltage reading after the initial delay has elapsed
if (isFirstVoltageReading)
{
bat->setVoltage(readVoltage());
isFirstVoltageReading = false;
}

// check the battery level every USERMOD_BATTERY_MEASUREMENT_INTERVAL (ms)
if (millis() < nextReadTime) return;

Expand Down

0 comments on commit 522e5e7

Please sign in to comment.