Hi, I am using a fake clock for some nodes on my IoT stuff.
Every hour the base sends out a time check of Seconds Since Midnight and in those nodes using the fake time to keep a rough track of events, I have this.
void pumpSSM(uint32_t &secSM) {
if (millis() % 1000 == 0) {
if ((secSM > 86400) || (secSM < 1)) { // Is this line correct?
secSM = 0; // Oops EDIT forgot this line
++secSM;
}
}
}
But, I am unsure if that line should be "86399" or "86400 >=" and the "< 1" or what?
I know it is not critical, but just wondering.
Thanks
I'm confused! Sorry!
86400 seconds is the number of seconds in 1 day, yes?
So, surely, secSM (seconds since midnight, I assume) should increment every second, with something that resets it to zero at midnight, which requires an external clock of some kind so you know it's midnight. Without that I don't see how you can achieve that you are trying to do.
I also don't understand why are you are incrmenting secSM when it is greater than 86400 or 0. You will increment it once with that code, to 1, after which it will never increment again.
Or it could be that my confusion is because I can't see the rest of your code.
I would also be careful of the statement
if (millis() % 1000 == 0)
because that requires the if statement to execute exactly when the last 3 digits of millis() are 0, something that can be hard to guarantee, and easy to miss.
A better approach would be to check for when the value of (millis() / 1000) changes. Create a variable to hold the previous value of millis() / 1000, then compare it to the current calculations, such as:
if ( (millis() / 1000) != previous_seconds ) {
previous_seconds = millis() / 1000;
//your code to be executed each second
}
PerryBebbington:
I'm confused! Sorry!
Me too. Sorry. I have corrected the code. I was copying it from screen on a 7" tablet and missed a line.
david_2018:
I would also be careful of the statement
if (millis() % 1000 == 0)
because that requires the if statement to execute exactly when the last 3 digits of millis() are 0, something that can be hard to guarantee, and easy to miss.
I normally use the approach you suggested, but was reading online about the Mod approach being preferred by some, it seemed cleaner so I tried it. On testing it with a "Serial.print(" in there I was getting secSM to be the same for more than 7-repeats. So, the 1000 was being seen at least 7-times. Plus, as mentioned it is not critical plus-minus 90-seconds would make no difference in reporting the temp or light levels. 
The nodes I use it on only call home once every ten minutes so not much else happening. The fake SSM seems to be always within a second of the Base time which has an RTC and sends correcting SSM regularly as mentioned.
I only put the 86400 etc check in there in case the Base drops out or the node stops getting the SSM correction for some reason.