Hi all, I’m totally puzzled as to why my output turns on at the right time in this code, but once it’s on, it never turns off. My understanding is that the code enters the outer loop when the current minute is 57, then enters one of the internal loops depending on whether or not the hour is 6 or 18. Each time the internal loop increments, the “secaccum” seconds counter increments, and once it reaches the given upper boundary for the loop, we should be exiting that loop. At that point, we’re still looping inside the outer loop, but each time we increment, we also turn the output off. Once we’re outside of the 58th minute of the hour, the output is always off and the accumulator is always zero.

Hmm…when I pasted the code, it seems to have omitted the “>” character. Basically, anywhere you see me saying secaccum=57, it should be secaccum>=57. Weird! Also seems to have omitted several other lines of my code…

Let me try again, with an edit to properly initialize secaccum;

EDIT: Apparently this forum software has some weird bugs when you try to post code, of all things. Lines keep disappearing when I paste! I’ll have to find a different way to paste, code is still not working after I initialize secaccum using int secaccum = 0. I promise I’m updating secaccum, at least I think I am…using “if(rtc.second(rtc.now())) secaccum++;” both in the outer and inner WHILE loops (meaning, the loop that happens only between the 57th and 58th minute, and the loops that occur within that loop, which actually turn on D0.

So I think we need to take a step back. Your library will poll an NTP server for the current time once when first started, then once every hour by default it seems… then keep that time updated based on the millis() counter on the Spark Core.

Every time you call rtc.now() it’s going to return a new current time in Unix time format, or maybe that’s Unix Epoch time… I dunno… the code is not commented at all really. It’s one of those times in milliseconds though… I think.

You have a lot of tests in there, checking the current time now() against some constant. One big problem is that every time the code sequences through and calls now() it’s different, so rtc.second(rtc.now())>=57 might be false but then when it gets to the next one the time has advanced just enough to now make that statement true. When you are AND’ing and OR’ing a bunch of things together you need to be comparing one reading to a bunch of constants, not changing readings to constants.

To fix this you need to save the now() reading to make it fixed, and then enter your multi-expression sequence. Like so:

Now let’s see what’s going on with this while statement. This bit of code is redundant:rtc.second(unix)>=57 && rtc.minute(unix)==57

It will be true when the second is equal to 57, but false any other time. Which is the same as just this statement alone rtc.minute(unix)==57

The rest looks like these statements:

while (
1. second equals 57 OR
2. (minute equals 58 AND second less than 4) AND
(hour is not 6 OR hour is not 18)
)

The second OR is going to be a problem. I’m guessing you meant AND because if one of them evaluates to true, i.e. let’s say it’s 6pm (18o’clock), the first part will be true, the hour is not 6, so then the whole thing is true because of the OR statement. But it’s 6pm! Rats. So I’m guessing you want (hour is not 6 AND hour is not 18)

So then you can just AND them all together, with one OR statement…

while (
1. second equals 57 OR
2. (minute equals 58 AND
second less than 4 AND
hour is not 6 AND
hour is not 18)
)

I’m not entirely sure what that is all doing for you, but I think I’m helping here

My problem turned out to be my use of “while” loops. I failed to realize that this program is one big loop already, so I didn’t need all the loops within loops. I think it was actually causing the Spark to hang up, because every time the program was supposed to start doing something, if I wanted to flash it again afterward I’d have to remove power and restart it. Now, I don’t have that problem!