Skip to content
Permalink
Browse files

Merge branch 'underflow_fix' of https://github.com/betarho/emontx3 in…

…to betarho-underflow_fix
  • Loading branch information...
glynhudson committed Oct 3, 2019
2 parents eaf3322 + 521393a commit 66802bf0ddf809e38855758667a03b546f71ec6d
Showing with 11 additions and 2 deletions.
  1. +11 −2 firmware/src/src.ino
@@ -497,17 +497,26 @@ void loop()
send_rf_data(); // *SEND RF DATA* - see emontx_lib
}

unsigned long sleeptime = 0;
unsigned long max_runtime = (TIME_BETWEEN_READINGS*1000) - 100;
unsigned long runtime = millis() - start;
unsigned long sleeptime = (TIME_BETWEEN_READINGS*1000) - runtime - 100;

if (runtime < max_runtime) {
sleeptime = max_runtime - runtime;
}

if (ACAC) { // If powered by AC-AC adaper (mains power) then delay instead of sleep
for (int i=0; i<5; i++){
delay(sleeptime/5);
wdt_reset(); //this line must be called faster than 8s, otherwise ATmega watchfog will kick in a resret the unit in event of a crash
}
} else { // if powered by battery then sleep rather than delay and disable LED to reduce energy consumption
word time_to_loose = 0;
if (sleeptime > 500) {
time_to_loose = sleeptime-500;
}
// lose an additional 500ms here (measured timing)
Sleepy::loseSomeTime(sleeptime-500); // sleep or delay in milliseconds
Sleepy::loseSomeTime(time_to_loose); // sleep or delay in milliseconds
}
} // end loop
//-------------------------------------------------------------------------------------------------------------------------------------------

0 comments on commit 66802bf

Please sign in to comment.
You can’t perform that action at this time.