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.
So, what am I missing here?
code:
// This #include statement was automatically added by the Spark IDE.
#include "SparkTime/SparkTime.h"
UDP UDPClient;
SparkTime rtc;
int secaccum;
int correction = D0;
int status = D1;
void setup()
{
pinMode(correction, OUTPUT);
pinMode(status, OUTPUT);
rtc.begin(&UDPClient, "north-america.pool.ntp.org");
rtc.setTimeZone(-7); // gmt offset
}
void loop()
{
while(rtc.minute(rtc.now())==57 || rtc.minute(rtc.now())==58)
{
if(secaccum<rtc.second(rtc.now())) secaccum++;
while(secaccum>=57 && secaccum<=63 && (rtc.hour(rtc.now())!=6 || rtc.hour(rtc.now())!=18))
{
digitalWrite(correction, HIGH);
if(secaccum<rtc.second(rtc.now())) secaccum++;
if(secaccum>=64) break;
}
while(secaccum>=57 && secaccum<=67 && (rtc.hour(rtc.now())==6 || rtc.hour(rtc.now())==18))
{
digitalWrite(correction, HIGH);
if(secaccum<rtc.second(rtc.now())) secaccum++;
if(secaccum>=68) break;
}
digitalWrite(correction, LOW);
}
digitalWrite(correction, LOW);
secaccum=0;
}
Thanks for your help!