Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: 5 sec delay..

  1. #1
    Senior Member
    Join Date
    Oct 2014
    Posts
    167

    5 sec delay..

    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..

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,234
    Perhaps elapsedMillis ?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,633
    Saw this yesterday

    http://www.gammon.com.au/blink

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,354
    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.

  5. #5
    Senior Member
    Join Date
    Oct 2014
    Posts
    167
    I like simply very much, almost as much as a chocolate Icecream cone or shake!

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,633
    https://www.pjrc.com/teensy/td_timing.html and elapsedMillis looks handy, had not seen that.

  7. #7
    Senior Member
    Join Date
    Oct 2014
    Posts
    167
    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..

  8. #8
    Member
    Join Date
    Mar 2014
    Location
    Übersee, Germany
    Posts
    40
    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.

  9. #9
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    295
    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 by mlu; 05-06-2015 at 07:42 PM.

  10. #10
    Senior Member
    Join Date
    Oct 2014
    Posts
    167
    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •