Sorry @Dave, I was busy typing this up.
edit, making the code section look better
I noticed the problems seemed to be happening around the 15 minute mark of every hour, which corresponded to an event in my code. I am using the core to turn on my lights at 6:15 every day (dawn simulation) and am using an exponential equation to slowly ramp up the lights pwm from the 15 min to 30 min mark.
after commenting pow(1.44,rtc.minuteString(currentTime).toInt()-15);
I haven’t had an issue in ~4 hours. So maybe I am doing something wrong with math, or some kind of math overflow.
Here is my code (or at least the relevant part, there is some more stuff with a PIR sensor). I am using the real time clock library used here: https://community.spark.io/t/real-time-clock-library-for-spark/2925
#include "SparkTime.h"
#include <math.h>
//Helios V5 Adding Real Time clock
//Lights variables
int lightout = 0;
int lightControl(int amount){
//sets each of the light strips to specified amount.
lightout = amount;
if(amount == 000){
digitalWrite(A1,LOW);
digitalWrite(A4,LOW);
digitalWrite(A6,LOW);
// take control of the LED
RGB.control(true);
// red, green, blue, 0-255
RGB.color(0, 0, 0);
}else if(amount == 255){
digitalWrite(A1,HIGH);
digitalWrite(A4,HIGH);
digitalWrite(A6,HIGH);
// resume normal LED operation
RGB.control(false);
}else {
analogWrite(A1, amount);
analogWrite(A4, amount);
analogWrite(A6, amount);
}
return lightout;
}
//dummy readable variable
int photovoltage = 0;
int nowtime;
int val;
//testing fuction for the pir sensor
void blink(void);
//sparktime stuff
UDP UDPClient;
SparkTime rtc;
unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr;
void setup() {
//Register Spark function
Spark.function("FadeLightsTo", FadeLightsTo);
Spark.function("Goodnight",Goodnight);
pinMode(A1, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A6, OUTPUT);
Spark.variable("photovoltage", &photovoltage, INT);
Spark.variable("Lights", &lightout, INT);
Spark.variable("Time", &nowtime, INT);
Spark.variable("Triggered", ×ince, INT);
pinMode(D0, INPUT);
pinMode(D1, OUTPUT);
rtc.begin(&UDPClient, "north-america.pool.ntp.org");
rtc.setTimeZone(-5); // gmt offset
}
// This routine loops forever
void loop()
{
if (rtc.hour(currentTime)==6 && rtc.minute(currentTime) >15 && rtc.minute(currentTime)<30 && rtc.dayOfWeek(currentTime)<6){
double temmp = 0;
temmp = pow(1.44,rtc.minuteString(currentTime).toInt()-15);
lightControl(temmp);
}
////Naughty Line//////////////////////////////////////////////
//photovoltage = pow(1.44,rtc.minuteString(currentTime).toInt()-15);
//////////////////////////////////////////////////////////////////
currentTime = rtc.now();
if (currentTime != lastTime) {
timeStr = "";
timeStr += rtc.hourString(currentTime);
timeStr += ":";
timeStr += rtc.minuteString(currentTime);
timeStr += ":";
timeStr += rtc.secondString(currentTime);
timeStr += " ";
timeStr += rtc.AMPMString(currentTime);
timeStr = "";
timeStr += rtc.hourString(currentTime);
timeStr += rtc.minuteString(currentTime);
timeStr += rtc.secondString(currentTime);
//I haven't figured out how to read String variables from the core, so I toint a string.
nowtime = timeStr.toInt();
lastTime = currentTime;
}
}
int FadeLightsTo(String command){
int previous = lightout;
int newtemp = command.substring(0,3).toInt();
if (previous > newtemp){
for(int j = previous;j>=newtemp;j--){
lightControl(j);
delay(10);
}
}else if (newtemp >previous){
for(int j = previous;j<=newtemp;j++){
lightControl(j);
delay(10);
}
}
return lightout;
}
int Goodnight(String command){
for(int j = lightout;j>=0;j--){
lightControl(j);
delay(90);
}
}