/* long_lp_timer starts a timer for 2hr 35min 15:025sec can time up to 245,000 years in hrs (c)2018 Forward Computing and Control Pty. Ltd. This example code is in the public domain. */ // comment out the next define to remove Serial support and save 1mA or so in supply current // is use Serial.prints short out the current shut resistors #define DEBUG #include lp_timer hrs_timer; // counts hrs lp_timer mins_secs_ms_delay; // times the last mins, sec, ms int hrsCounter; uint32_t minsSecms_timeout; void startHrMinSecTimer(unsigned int hrs, unsigned int mins, unsigned int secs, unsigned int ms) { // do checks here for valid input const uint32_t ms_1hr = 1000 * 60 * 60; #ifdef DEBUG Serial.println(); Serial.println(" -- startHrMinSecTimer -- "); Serial.print(" hrs:"); Serial.print( hrs); Serial.print(" mins:"); Serial.print(mins); Serial.print(" secs:"); Serial.print( secs); Serial.print(" ms:"); Serial.print(ms); Serial.println(); #endif uint64_t ms_timeout = ms + 1000 * (secs + 60 * mins); // move excess hrs to hrsCounter uint64_t excessHrs = 0; if (ms_timeout > ms_1hr) { excessHrs = ms_timeout / ms_1hr; ms_timeout = ms_timeout % ms_1hr; } minsSecms_timeout = (uint32_t) ms_timeout; hrsCounter = hrs + excessHrs; #ifdef DEBUG Serial.print(" hrsCounter:"); Serial.print( hrsCounter); Serial.print(" minsSecms_timeout:"); Serial.print(minsSecms_timeout); Serial.println(); #endif if (hrsCounter > 0) { hrs_timer.startTimer(ms_1hr, hrs_handler); // start counting hrs #ifdef DEBUG Serial.print(" start hrs_timer for "); Serial.print(hrsCounter); Serial.print(" x 1hr (1hr == "); Serial.print(ms_1hr); Serial.print("ms)"); Serial.println(); #endif } else { // just do mins sec mins_secs_ms_delay.startDelay( minsSecms_timeout, ms_handler); #ifdef DEBUG Serial.print(" start mins_secs_ms_delay for "); Serial.print(minsSecms_timeout); Serial.print("ms"); Serial.println(); #endif } } void hrs_handler() { #ifdef DEBUG Serial.println(); Serial.print(" hrs_handler at "); Serial.print(millis()); Serial.print("ms"); Serial.println(); #endif hrsCounter = hrsCounter - 1; #ifdef DEBUG Serial.print(" updated hrsCounter:"); Serial.print( hrsCounter ); Serial.println(); #endif if (hrsCounter == 0) { hrs_timer.stop(); mins_secs_ms_delay.startDelay( minsSecms_timeout, ms_handler); #ifdef DEBUG Serial.print(" start mins_secs_ms_delay for "); Serial.print(minsSecms_timeout); Serial.print("ms"); Serial.println(); #endif } // else } void ms_handler() { #ifdef DEBUG Serial.println(); Serial.print(" -- mins_secs_ms_delay timed out -- at "); Serial.print(millis()); Serial.print("ms"); Serial.println(); #endif // do stuff here on timeout // restart here if you need to repeat } void setup() { #ifdef DEBUG Serial.begin(115200); Serial.println(); #endif startHrMinSecTimer(2, 35, 15, 25); #ifdef DEBUG Serial.print("startup() finished at "); Serial.print(millis()); Serial.println("ms"); #endif } void loop() { sleep(); #ifdef DEBUG Serial.print("loop() triggered at "); Serial.print(millis()); Serial.print("ms"); Serial.println(); #endif }