improve battery tracking

This commit is contained in:
2023-02-21 11:49:25 +01:00
parent ba91da7ed3
commit 2f0027b68b
3 changed files with 26 additions and 3 deletions
+6
View File
@@ -33,6 +33,12 @@ double Battery::getBatteryVoltage() {
return (int)(res*100+0.5)/100.0;
}
bool Battery::isBatteryLow(uint8_t percent) {
if (this->getBatteryPercent() <= percent && this->batteryVoltage > this->absurdLowVoltage)
return true;
return false;
}
double Battery::readInputVoltage() {
// Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
double reading = analogRead(this->pin);
+15
View File
@@ -73,13 +73,28 @@ class Battery {
*/
uint8_t getBatteryPercent() {return this->batteryPercent;}
/**
* @brief Checks if the battery is low.
*
* The function will also return false if the battery voltage is
* absurd low. This is for the case that the uController is powered
* by usb and no battery is connected.
*
* @param percent the limit the battery have to
* @return true if the battery is low
* @return false if the battery is high
*/
bool isBatteryLow(uint8_t percent);
private:
double readInputVoltage();
void calculateBatteryVoltage();
void calculateBatteryPercent();
uint8_t absurdLowVoltage = 5;
uint8_t pin;
uint8_t batteryPercent;
uint8_t batteryLowPercent = 10;
uint16_t delay = 20000;
uint32_t r1;
uint32_t r2;