Proper formatting for Multiple "||" statements in an "if" test?

Hello,

I am trying to setup a schedule based task using an "if" test and multiple "||" such as:

    if ((timeSecs == 25200) || (timeSecs == 28800) || (timeSecs == 32400)){

timeSecs is a "long" variable based on time elapsed in seconds and added to a NPT time update function that updates once daily. I have verified that timeSecs is correct, so I suspect my formatting in my if statement is incorrect. Does it look right? If not, what am I doing wrong?

Thanks,
Travis

As long as you want the test to return true if any of the 3 values equals the value of timeSecs then the test looks OK but bear in mind this is not a range based test but an exact match test

Have you tried printing the value of timeSecs just before the test ?
Does it work if you set timeSecs explicitly to one of the values just before the test ?

32,400 seconds is 9 hours. How can the elapsed time ever match that if it's only updated once a day?

Thanks UKHeliBob,

That is correct, I want the test to be "true" if any one of the timeSecs is true. Thanks for confirming that it is correct.

In regards to:

Have you tried printing the value of timeSecs just before the test ?

no, I tested timeSecs in the loop by adding:

Serial.print(timeSecs);
delay(1000);

I just tested it right before the "if" test as you asked and got a running string that did not directly correspond to the timeSecs variable (at least as far as I could break it down).

Does it work if you set timeSecs explicitly to one of the values just before the test ?

Yes, it does. Thanks for this. I am going to create a function outside of all other functions to set the schedule and call that function from the primary "if" test instead of trying to set it directly inside the schedule function - I think this will solve the problem.

Thanks for your help, I will reply back once done and if that solved the problem.

Thanks for replying aarg,

32,400 seconds is 9 hours. How can the elapsed time ever match that if it's only updated once a day?

timeSecs is elapsed time in seconds added to whatever time it was during the last NPT time sync. So timeSecs should range from 0 to 86399 - unless the network goes down. I don't know why this would not work?

NTP returns epoch time, when I checked recently, it was 1583775580245UL. You say, "timeSecs is elapsed time in seconds added to whatever time it was during the last NPT time sync". So 9 hours from now, NTP + timeSecs will be 1,583,775,612,645‬UL. How will your test react to that?

Questions like this are why we usually ask for a complete sketch.

Thanks aarg, But I did not fully explain how my ntp sync works. It does the math and extrapolates seconds from the server sync, then it disconnects from the sever until called to resync 24 hrs later. It does work, I have printed timeSecs to the serial monitor and it is correct.

I just tested it right before the "if" test as you asked and got a running string that did not directly correspond to the timeSecs variable (at least as far as I could break it down).

If timeSecs has rubbish in it, possibly due to being overwritten by writing to an array beyond its bounds, then the comparison is never going to work.

Please post your complete program so that the problem can be seen in context

mainewoodsandpond:
It does the math and extrapolates seconds from the server sync

Seconds from when, to when? This is why we need the source code.

Are you confident that the line is being executed more than once a second? If not, you might miss an equality, as has been hinted at in reply #1.

The formatting is really a personal preference, and yours isn't confusing, so it's fine.

However, I would suggest explicitly marking your constants as 'long', to remind you in the future in case your constants get bigger.

Integer constants default to integer size. Yours now are smaller than the max value an integer can hold, but marking them as long (using 'l' or 'L' at the end) will help remind you in the future that you want 'long' constants, not 'int'.