I currently measure elapsed time in my sketch with a code to measure elapsed time like the one seen here. The code needs to be accurate to about 1 second, but I would like to program it to be as robust as possible.
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{foo;}
I'm wondering what the best way to add a pause functionality so that if bool pause == true
either previousMillis
or interval
adjusts to make sure that foo
fires after pause==false
for interval
Right now I'm thinking something along the lines of:
//global setup
bool pause=false;
unsigned long previousMillis = 0;
unsigned long interval = 1000;
_________
unsigned long currentMillis = millis();
RClock();
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{
interval=1000;
foo;
}
void RClock()
{
if(pause)
{
unsigned long runtime = millis()-previousMillis;
unsigned long timeleft= interval-runtime;
previousMillis=millis();
interval=timeleft;
}
}
Is there a better way to do this? I feel like it should be a more simple operation.
Edit
To clear things up, my goal is for foo
to trigger when pause==true
for a total of interval
so if interval=1000
(ms) and, during that time, pause==true
for 500(ms) foo
would occour after 1500 ms
Edit 2
To clarify further, the time something runs before pause==true
needs to be taken into acount lets say that:
interval = 1000
(ms) when the program starts.The program runs for 500(ms)
After 500(ms)
pause == true
After 3hr
Pause == false
foo
will be triggered after 500(ms) more ofPause == false
pause==true
? – fnocettifoo
to trigger whenpause==true
for a total ofinterval
– ATE-ENGEpause=true
? What I understood from question is you want to execute foo for 500ms for every 1000ms, correct? – svtag