Time Calculation

I have written a simple script, based on a different script I found online, to publish to the Particle console every 5 minutes but I don’t understand exactly how it works. The entire code is below but the specific part is:

 if (now - LastCheckIn > WAIT_SECONDS) {
        LastCheckIn = now;

How is the last check in time being stored? When LastCheckIn = now, wouldn’t that mean that the formula “now - LastCheckIn” would always equal 0? Since it’s working I know this is not the case but do not understand why.

#define WAIT_SECONDS 300
#define LED D7

int LastCheckIn = 0; //Last time the Photon published the time

void setup() {
    pinMode(LED, OUTPUT);
    Particle.publish("Battery Timer Online", PRIVATE);
}

void loop() {
    int now = Time.now();
    if (now - LastCheckIn > WAIT_SECONDS) {
        LastCheckIn = now;
        Particle.publish("Check In", PRIVATE);
        digitalWrite(LED, HIGH);
    } else {
        digitalWrite(LED, LOW);
    }
}

At the beginning of each loop, you request the current time into the int now variable. The if statement if (now-LastChekIn > WAIT_SECONDS) basically checks to see if the current time has passed a specified interval of 300 seconds (WAIT_SECONDS). If the 300 seconds has passed, then the very next line LastCheckIn = now; updates the LastCheckIn variable. The if statement is only true every 300 seconds.

To find any specified time interval, you need to know 2 times; the current time and the start time of the interval. To calculate how much of the interval has elapsed, you do: Current Time - Start Time = Interval Seconds. Each time you get to the end of the interval, in this case 300 seconds, you have to update the start time so that a new interval period starts. The start time of the interval is recorded in the LastCheckIn variable. The line LastCheckIn = now; resets the interval by recording a new start time.

If it helps, you could accomplish the same task with the variables in different positions such as:

if ( now > LastCheckIn + WAIT_SECONDS)

So @ninjatill if I understand correctly LastCheckIn gets evaluated at a different time than when the formula (now-LastCheckIn > WAIT_SECONDS) is run?

I wouldn't use those exact words. The LastCheckIn gets "evaluated" in the if statement. The if statement if (now - LastCheckIn > WAIT_SECONDS) is run every loop and calculates the number of seconds that has elapsed since LastCheckIn. However the if statement only evaluates true every 300 seconds. The LastCheckIn time is updated during that true period once every 300 seconds.

@ninjatill Your explanations have been very helpful. You said that “the if statement…is run every loop and calculates the number of seconds has elapsed since LastCheckIn.”

How does it know what the previous value of LastCheckIn was? It’s my understanding that the script I have basically works in the following order:
1- Read defined terms
2- Run setup to interpret pin and publish startup notifcation
3- Run loop in the following order:
a- now = current time
b- evaluate if statement
4- Repeat loop

My hangup is that I do not see how the value for LastCheckIn is getting stored between loops.

Thanks for the patience and help.

LastCheckIn is a global variable, so it retains whatever value you set it to for the life of the program (until you change it, that is, which only happens every 300 seconds in your example).

As @Ric states, the LastCheckIn is a global variable. When you declare a variable at the top of your code outside of, and before, setup() and loop(), that is a “global” variable. A global variable retains its value outside of your functions and is accessible by just about any function (there are caveats to accessibility and retention which are outside the scope of what you are asking).

@ninjatill and @Ric

Thank you for the explanations. It makes sense now.

1 Like