fix battery percentage and voltage levels

This commit is contained in:
2025-11-05 17:26:06 +01:00
parent f71563739c
commit 4e9ff1c2b0
2 changed files with 8 additions and 5 deletions

View File

@@ -12,6 +12,7 @@
platform = atmelavr
board = pro8MHzatmega328
framework = arduino
monitor_speed = 115200
lib_deps =
mysensors/MySensors@^2.3.2
thomasfredericks/Bounce2@^2.72
@@ -21,6 +22,7 @@ lib_deps =
platform = atmelavr
board = nanoatmega328new
framework = arduino
monitor_speed = 115200
lib_deps =
mysensors/MySensors@^2.3.2
thomasfredericks/Bounce2@^2.72

View File

@@ -1,4 +1,4 @@
#define PRO_MINI // PRO_MINI o NANO
#define NANO // PRO_MINI o NANO
#define MY_RADIO_RF24
#define MY_DEBUG
@@ -54,7 +54,7 @@
MyMessage ch_comm_dht_temperature(DHT_ID_TEM, V_TEMP);
MyMessage ch_comm_dht_humidity(DHT_ID_HUM, V_HUM);
const float VccMin = 3.7;
const float VccMin = 3.0;
const float VccMax = 4.2;
Vcc vcc;
@@ -127,9 +127,10 @@ void loop()
}
int voltage = Vcc::measure(100, 1100);
int batteryPercent = static_cast<int>(100.0 * (voltage - VccMin) / (VccMax - VccMin));
if (batteryPercent > 100) batteryPercent = 100;
if (batteryPercent < 0) batteryPercent = 0;
int voltageV = voltage / 1000.0;
int batteryPercent = static_cast<int>(100.0 * (voltageV - VccMin) / (VccMax - VccMin));
batteryPercent = constrain(batteryPercent, 0, 100);
sendBatteryLevel(batteryPercent); // función de MySensor que envía el procentaje