Add averaging to reading battery levels
All checks were successful
Compile / Compile (push) Successful in 1m21s

This commit is contained in:
Lewis Jackson 2023-06-01 16:17:40 +03:00
parent 32cd26fccf
commit 5993fda85e
2 changed files with 15 additions and 2 deletions

View file

@ -2,6 +2,9 @@
#include "Battery.h"
#include <Wire.h>
#include <Arduino.h>
#include <limits>
float WatchFeatures::Battery::m_previousVoltage = std::numeric_limits<float>::infinity();
WatchFeatures::Battery::Battery()
{
@ -9,13 +12,20 @@ WatchFeatures::Battery::Battery()
float WatchFeatures::Battery::GetVoltage()
{
return analogReadMilliVolts(BATT_ADC_PIN) / 1000.0f * 2.0f;
float voltage = analogReadMilliVolts(BATT_ADC_PIN) / 1000.0f * 2.0f;
if (m_previousVoltage == std::numeric_limits<float>::infinity()) {
m_previousVoltage = voltage;
}
float averageVoltage = (m_previousVoltage + voltage) / 2.0f;
m_previousVoltage = voltage;
return averageVoltage;
}
uint8_t WatchFeatures::Battery::GetPercentage()
{
float voltage = GetVoltage();
Serial.println(voltage);
float level = (GetVoltage() - 3.6f) / 0.6f;
if (level < 0.0f) {

View file

@ -13,4 +13,7 @@ public:
Battery();
float GetVoltage();
uint8_t GetPercentage();
private:
static float m_previousVoltage;
};