Updates
This commit is contained in:
@@ -3,11 +3,12 @@
|
||||
void BatterySensor::begin() {
|
||||
pinMode(PIN_BATTERY_ADC, INPUT);
|
||||
pinMode(PIN_LBO, INPUT);
|
||||
pinMode(PIN_CHG, INPUT_PULLUP); // CHG is LOW when charging
|
||||
|
||||
analogReadResolution(12); // 12-bit ADC (0-4095)
|
||||
|
||||
Serial.println("[Battery] Initialized");
|
||||
Serial.println("[Battery] ADC on GPIO 2, LBO on GPIO 5");
|
||||
Serial.println("[Battery] ADC on GPIO 2, LBO on GPIO 5, CHG on GPIO 4");
|
||||
|
||||
// Initial reading
|
||||
_voltage = readBatteryVoltage();
|
||||
@@ -29,30 +30,47 @@ void BatterySensor::loop() {
|
||||
float prevVoltage = _voltage;
|
||||
int prevPercent = _percent;
|
||||
bool wasLow = _isLow;
|
||||
bool wasCharging = _isCharging;
|
||||
|
||||
// Read battery voltage via ADC
|
||||
_voltage = readBatteryVoltage();
|
||||
_percent = voltageToPercent(_voltage);
|
||||
|
||||
// Check CHG pin (LOW when charging)
|
||||
bool chgLow = !digitalRead(PIN_CHG);
|
||||
_isCharging = chgLow;
|
||||
|
||||
// Check LBO pin as backup confirmation
|
||||
bool lboHigh = digitalRead(PIN_LBO);
|
||||
bool lboIndicatesLow = !lboHigh;
|
||||
|
||||
// Battery is low if either voltage is low OR LBO pin indicates low
|
||||
_isLow = (_voltage < VOLTAGE_LOW) || lboIndicatesLow;
|
||||
// Battery is low if either voltage is low OR LBO pin indicates low (and not charging)
|
||||
_isLow = ((_voltage < VOLTAGE_LOW) || lboIndicatesLow) && !_isCharging;
|
||||
|
||||
// Log significant changes
|
||||
if (abs(_percent - prevPercent) >= 5 || _isLow != wasLow) {
|
||||
if (abs(_percent - prevPercent) >= 5 || _isLow != wasLow || _isCharging != wasCharging) {
|
||||
Serial.print("[Battery] Voltage: ");
|
||||
Serial.print(_voltage, 2);
|
||||
Serial.print("V (");
|
||||
Serial.print(_percent);
|
||||
Serial.print("%) LBO: ");
|
||||
Serial.print("%) ");
|
||||
|
||||
if (_isCharging) {
|
||||
Serial.print("CHARGING ");
|
||||
}
|
||||
|
||||
Serial.print("LBO: ");
|
||||
Serial.println(lboHigh ? "OK" : "LOW");
|
||||
|
||||
if (_isLow && !wasLow) {
|
||||
if (_isLow && !wasLow && !_isCharging) {
|
||||
Serial.println("[Battery] ⚠️ LOW BATTERY WARNING - Please recharge!");
|
||||
}
|
||||
|
||||
if (_isCharging && !wasCharging) {
|
||||
Serial.println("[Battery] 🔌 Charging started");
|
||||
} else if (!_isCharging && wasCharging) {
|
||||
Serial.println("[Battery] ✓ Charging complete or disconnected");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user