diff --git a/timeRequest.ino b/timeRequest.ino index c10241c..39c317b 100644 --- a/timeRequest.ino +++ b/timeRequest.ino @@ -20,7 +20,7 @@ #endif #define MY_RF24_CHANNEL 83 #define MY_BAUD_RATE 57600 -#define MY_DEBUG +//#define MY_DEBUG #define SKETCH_NAME "timeRequest" #define SKETCH_VERSION "20171203_01" @@ -41,38 +41,20 @@ } // presentation -void printDigits(int digits){ - // utility function for digital clock display: prints preceding colon and leading 0 - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} // printDigits - - void receiveTime(unsigned long ts) { const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2017 +#ifdef MY_DEBUG Serial.print('['); Serial.print(millis() / 1000); Serial.print(F("]: ts: ")); Serial.println(ts); +#endif if (ts > DEFAULT_TIME) { setTime(ts); - - printDigits(hour()); - Serial.write(':'); - printDigits(minute()); - Serial.write(':'); - printDigits(second()); - Serial.write(' '); - printDigits(day()); - Serial.write('.'); - printDigits(month()); - Serial.write('.'); - Serial.println(year()); } } // receiveTime @@ -98,6 +80,33 @@ #endif +void printDigits(int digits) +{ + // utility function for digital clock display: prints preceding colon and leading 0 + if(digits < 10) + { + Serial.print('0'); + } + Serial.print(digits); +} // printDigits + + +void printTime() +{ + printDigits(hour()); + Serial.write(':'); + printDigits(minute()); + Serial.write(':'); + printDigits(second()); + Serial.write(' '); + printDigits(day()); + Serial.write('.'); + printDigits(month()); + Serial.write('.'); + Serial.println(year()); +} // printTime + + void setup() { Serial.begin(MY_BAUD_RATE); @@ -106,11 +115,15 @@ Serial.print(F("Version: ")); Serial.println(F(SKETCH_VERSION)); Serial.flush(); + + // request time at first + requestTime(); } // setup void loop() { +#ifdef MY_DEBUG static unsigned long lastRequest = 0; unsigned long currentTime = millis(); if (currentTime - lastRequest >= 10000UL) @@ -118,4 +131,18 @@ lastRequest = currentTime; requestTime(); } +#else + static unsigned int lastHour = 0; + // request time every our + if (hour() != lastHour) + { + requestTime(); + lastHour = hour(); + } + + printTime(); + + sleep(10000UL); +#endif } // loop +