5 sec delay..

Status
Not open for further replies.

Wayne

Well-known member
Ok, I have forgotten what to use for delaying 5 secs.. I know delay(5000); but that holds up the program..

What I'm trying to do is open a valve for 5 secs only when I switch to a tank of air to purge.

I think there was a library, but it has been a year since I did it I think..

Pointer please..
 
Yup, elapsedMillis is the easiest way.

Internally, elapsedMillis does the same thing as Nick's example. It's simpler to use, because you can think of it as a writeable variable that automatically increments, so you don't have to worry about those little details like which ways of comparing and subtracting handle the 32 bit rollover and which ones don't.
 
Ok, took a bit, but I did get my delay to work.. the test seems to be needed in the look..

if (purgetime > 3000 )
{
purgetime = 0;
digitalWrite(purgepin, LOW);
}

During setup, i set the digitalWrite(purgepin, HIGH); to enable the purge unit..

the test in the loop turns the LED LOW and stays low until I raise it high.

I needed to do a purge when tanks are switched, so when I do that I set the digitalWrite(purgepin, HIGH); again and it seems to be working..

My mind has a difficult time grasping the difference between delay() and elapsedMillis .. But I think I have it now..

Now for a chirping sound.. has to be annoying for a warning to switch tanks..

Currently I have the display working great thanks to Sumotoy and now tank purge thanks to all here.

I have tried tone, but need to make it chirpy sound..
 
I can recommend metro library. I am using it for a long time and have even ported it to mbed. It leads to a non-preemptive task-like structure in code and it does not load any hidden timer IRQs. Simple, intuitive and save.
 
The difference between using elapsedMillis and delay

delay(5000) stops and waits for 5000 milli seconds.

DO SOMETHING
delay(5000);
KEEP DOING STUFF

This means the processor is doing no useful work for 5 seconds, it will recieve
data using interrups but they can overflow since your progarm is asleep and doesnt handle them


elapsedMillis objects reports the number of milliseconds since it was last reset to 0,
it doesnt stop program execution.

if (myElapsedMillisObject > 5000) {
HANDLE THE STUFF, BUT ONLY WHEN 5000 ms HAS ELAPSED
}

this means only a minute amount of processor power is used to check the time, until 5000 ms has elapsed

If your code only do one task delay can work, but it will interfere with communications.
For many active task and ongoing communications, you can keep one elapsedMillis object to track waiting for each task,
this will allow the processor to handle other tasks while waiting for 5000 millis to go by.

Hope this is an understandable explanation :)

EDIT
Some typos and wrong number of 0's edited
 
Last edited:
Yup it does.. I knew delay had the issue, last year at this time I was using it and worked with other timers stuff.. I just needed a refresh on what was around..

I have a tone now for warnings..

This is a good day so far..
 
Status
Not open for further replies.
Back
Top