Friends,
I have written code that is intended to turn something on for a specified amount of time and then off after it has run for said time. I am using the EllapsedMillis Library but am worried about it overflowing, since the code is designed to run for long periods of time.
I am eventually looking to make other variables control influence this interval so i am looking for the safest, simplest, and most robust way to go about it.
here is my code so far;
// This #include statement was automatically added by the Particle IDE.
#include <elapsedMillis.h>
#define SprayPin D2
/****************************INPUTS*****************************************/
int OnTime = 10000; //Time in milliseconds, how long you want to spray for
int OffTime = 500000; //Time in milliseconds, how long to be off in between cycles
/***************************************************************************/
elapsedMillis timer0; // a running timer to keep track of ON time
bool Spraying = false; // a flag used to tell us if the pump is on
int mainOnTime = -1;
int mainOffTime = -1;
int resetTime = 40*24*3600*1000; // how long to let timer go before we reset it to zero
void setup() {
pinMode(SprayPin, OUTPUT); // Telling the controller this pin will be used as a Digital output
digitalWrite(SprayPin, LOW); // On start up keep output off
}
void loop() {
if(!Spraying){
if(timer0 - mainOffTime >= OffTime){
mainOnTime = timer0;
digitalWrite(SprayPin, HIGH);
}
}
if(Spraying){
if(timer0 - mainOnTime >= OnTime){
mainOffTime = timer0;
digitalWrite(SprayPin, LOW);
}
}
if(timer0 >= resetTime && Spraying == false){
timer0=0;
}
}
Is this script overflow safe? Do i even need to include the reset portion?
any insight is greatly appreciated