On off intervals, overflow safe

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

Why not just use Unix time stamps; the Time Class.

If you are using unsigned datatypes (e.g. uint32_t instead of int) at all instances then it is overflow safe.
As soon you start mixing signed and unsigned, you are bound to run into troubles since order of execution and implicit type casting will make things hard to predict.

That’s the reason why the compiler usually throws warnings when you start mixing :wink:

2 Likes

In the first iteration of this code i did use the Time.minute() to control how ofter to trigger the pin but i wanted a more precise control of the time. I couldn’t figure out how to use the time class to my advantage in that aspect

Thanks for the insight! Can you elaborate on how the signed vs unsigned effects the overflow. Now i know that its not good practice, but doesn’t the unsigned take president, e.g. if i declare a variable as int then set it equal to the timer, doesn’t that make it unsigned?

Thanks again.

Nope, if you try to assign a value that can't be represented with the type, you will get wrong results. You can't cram values 2,147,483,648 .. 4,294,967,295 into a type that can only hold values in the range -2,147,483,648 .. +2,147,483,647.

Assignments never can change the declared type of a variable.

BTW, Time.now() provides you with an int value that represents the number of seconds since 1.1.1970. So using that would give you at least seconds precision. If you don't need to be any more precise, it might be an option still.
With that you won't need to think about overflowing until January 2038 :wink:

1 Like