Coding Timers and Delays in Arduino

36,095

10

26

Introduction: Coding Timers and Delays in Arduino

5th Sept 2019 update: Removing delay() calls is the first step to achieving simple multi-tasking on any Arduino board. The instructable Simple Multi-tasking in Arduino on Any Board covers all the other necessary steps.

5th May 2019 update: Renamed isFinished() to justFinished(), as it only returns TRUE once just after the delay finishes. Added Freeze/Pause delay example

Also see Arduino For Beginners – Next Steps
How to write Timers and Delays in Arduino (this one)
Safe Arduino String Processing for Beginners
Simple Arduino Libraries for Beginners
Simple Multi-tasking in Arduino

Introduction

Don't use delay( )

Using delay() causes your system to be stuck while waiting for the delay to expire. However replacing delays requires some care. This page explains in a step by step way how to replace Arduino delay() with a non-blocking version that allows you code to continue to run while waiting for the delay to time out.

Here are a number of simple sketches each of which turn a Led on when the Arduino board is powered up (or reset) and then 10sec later turns it off. The first one is an example of how you should NOT write the code. The second is an example of code the works and the third is an example of using the millisDelay library to simplify the code. There are also examples of single-shot and repeating timers.

If you already understand why you should not use delay() and are familiar with Arduino, the importance of using unsigned longs, overflow and unsigned subtraction, then you can just skip to Using the millisDelay library (Step 4)

The millisDelay library provides functionality delays and timers, is simple to use and easy to understand for those new to Arduino.

This instructable is also on-line at How to code Timers and Delays in Arduino

Step 1: How Not to Code a Delay in Arduino

Here is how NOT to code a delay in a sketch.

int led = 13;
bool ledOn;

void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT); // initialize the digital pin as an output.
digitalWrite(led, HIGH); // turn led on
ledOn = true; // led is on
}

void loop() {
if (ledOn) {
delay(10000);
digitalWrite(led, LOW); // turn led off
ledOn = false; // prevent this code being run more then once
Serial.println("Turned LED Off");
}
// Other loop code here . . .
Serial.println("Run Other Code");
}

In the setup() method, which Arduino calls once on starting up, the led is turned on. Once setup() is finished, Arduino calls the loop() method over and over again. This is where most of you code goes, reading sensors sending output etc. In the sketch above, the first time loop() is called, the delay(10000) stops everything for 10secs before turning the led off and continuing. If you run this code you will see that the Run Other Code is not printed out for 10sec after the startup, but after the led is turned off (ledOn equals false) then is printed out very fast as loop() is called over and over again.

The point to note here is that you really should not the delay() function at all in the loop() code. It is sometimes convenient to use delay() in the setup() code and you can often get away with very small using very small delays of a few milliseconds in the loop() code, but you really should avoid using the at all in the loop() method

Step 2: How to Write a Non-blocking Delay in Arduino

The previous sketch used a blocking delay, i.e. one that completely stopped the code from doing any thing else while the delay was waiting to expire. This next sketch shows you how to write a non-blocking delay that allows the code to continue to run while waiting for the delay to expire.

int led = 13;
unsigned long delayStart = 0; // the time the delay started bool delayRunning = false; // true if still waiting for delay to finish void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, HIGH); // turn led on delayStart = millis(); // start delay delayRunning = true; // not finished yet } void loop() { // check if delay has timed out after 10sec == 10000mS if (delayRunning && ((millis() - delayStart) >= 10000)) { delayRunning = false; // // prevent this code being run more then once digitalWrite(led, LOW); // turn led off Serial.println("Turned LED Off"); } // Other loop code here . . . Serial.println("Run Other Code"); }

In the sketch above, in the setup() method, the delayStart variable is set to the current value of millis().

millis() is a built-in method that returns the number of milliseconds since the board was powered up. It starts as 0 each time the board is reset and is incremented each millisecond by a CPU hardware counter. More about millis() later. Once setup() is finished, Arduino calls the loop() method over and over again.

Each time loop() is called the code checks
a) that the delay is still running, and
b) if the millis() has move on 10000 mS (10sec) from the value stored in delayStart.

When the time has move on by 10000mS or more, then delayRunning is set to false to prevent the code in the if statement being executed again and the led turned off.

If you run this sketch, you will see Run Other Code printed out very quickly and after 10sec the Led will be turned off and if you are quick you might just see the Turned LED Off message before it scrolls off the screen.

See Step 4, below for how the millisDelay library simplifies this code

Step 3: Unsigned Long, Overflow and Unsigned Subtraction

If you are familiar with unsigned longs, overflow, unsigned arithmetic and the importance of using an unsigned long variable, then you can just skip to Step 4 Using the millisDelay library.

The important part of the previous sketch is the test

(millis() - delayStart) >= 10000 

This test has to be coded in this very specific way for it to work.

Unsigned Long and Overflow

The delayStart variable and number returned from the millis() built-in function is an unsigned long. That is a number from 0 up to 4,294,967,295.

If you add 1 to an unsigned long holding the maximum value of 4,294,967,295 the answer will be 0 (zero). That is the number overflowed and wrapped around back to 0. You can imagine the overflow bit just gets dropped. e.g. in a 3 bit unsigned 111 is the maximum value (7) adding 1 gives 1000 (8) but the leading 1 overflows the 3 bit storage and is dropped so wrapping back to 000.

This means, eventually, when the cpu adds one more it variable holding the millis() result it will wrap around to 0. That is millis() will start counting from 0 again. This will happen if you leave your Arduino board running for 4,294,967,295mS i.e. about 49day 17hrs, say 50days.

Now let's consider another way of coding the test (millis() - delayStart) >= 10000

Arithmetically this test is equal to millis() >= (delayStart + 10000)

However if you start the delay after almost 50 days, for example when millis() returns 4,294,966,300 mS, then delayStart + 10000 will overflow to 995 and the test, millis() >= (delayStart + 10000), will immediately be true and there will be no delay at all. So this form of the test does not always work.

Unfortunately you are unlikely to come across this during your testing, but for it may crop up un-expectedly in a long running device, like a garage door control the runs continually for months. You will get a similar problem if you try and use
delayEnd = millis() + 10000
and then the test (millis() >= delayEnd)

Finally the delayStart variable must be an unsigned long. If you instead use a long (i.e. long int) or int or unsigned int , the maximum value they can hold is smaller than the unsigned long returned from millis(). Eventually the value retuned from millis() will overflow the smaller variable it is being stored in and you will find time has suddenly gone backwards. For example, if you use an unsigned int for startDelay, this will happen after 65 secs on an Uno board.

Unsigned Subtraction

One other point of interest is what happens to result of millis() - delayStart when delayStart is say 4,294,966,300 and we want a 10000mS delay.

millis() will wrap around to 0 before that happens. Remember that adding 1 to the maximum value an unsigned long can store wraps around back to 0. So one way of looking at calculating millis() - delayStart, where millis() has wrapped around and is smaller then delayStart, is to say "What number do I have to add to delayStart to equal millis() (after overflow)?" i.e. what is X in the equation delayStart + X == millis()

For example using a 3 bit unsigned variable again, to calculate 2 – 4 (unsigned), think of a clock face starting at 0 and adding 1 all the way round to 111 (7) and then back to 0. Now to get from 4 to 2 you need to add 6 (5,6,7,0,1,2) so 2-4 = 6 and this is in effect how the calculation works, although the CPU will perform the calculation differently.

So the difference of two unsigned longs will always be a positive number in the range 0 to 4,294,967,295. For example if startDelay is 1 and millis() has wrapped around to 0 (after 50 days) then millis() - startDelay will equal 4,294,967,295. This means that you can specify a DELAY_TIME anywhere in the range 0 to 4,294,967,295 mS and (millis() - delayStart) >= DELAY_TIME will always work as expected, regardless on when the delay is started.

Step 4: Using the MillisDelay Library

To install the millisDelay library. Downloaded the millisDelay.zip file.

Unzip this file to your Arduino/libraries directory (open the IDE File->preferences window to see where your local Arduino directory is). Some times the instructions for How to Install a Library - Automatic installation work, but not always. Unzipping the file manually is safest. Once you install the millisDelay library, there will be three interactive examples available that you can load on your Arduino board and then open the Serial Monitor (within 5sec) at 9600baud to use them. Here is the previous non-blocking delay sketch re-written using the millisDelay library.

#include "millisDelay.h" 
int led = 13;
millisDelay ledDelay;

void setup() {
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(led, HIGH); // turn led on 

  ledDelay.start(10000);  // start a 10sec delay
}

void loop() {
  // check if delay has timed out
  if (ledDelay.justFinished()) {
    digitalWrite(led, LOW); // turn led off
    Serial.println("Turned LED Off");
  }
//  Other loop code here . . .
  Serial.println("Run Other Code");
}

If you look at the millisDelay library code you will see that the previous sketch's code has just been moved to the start() and justFinished() methods in the library.

Is this a ledDelay or a ledTimer? You can use which ever term you like. I tend to use ...delay for single-shot delays that execute once and use …timer for repeating ones.

Step 5: Delay and Timer Examples

Here are two basic delay and timer sketches and their millisDelay library equivalents. These examples are for a once off (single-shot) delay and a repeating delay/timer.

Single-Shot Delay

A single shot delay is one that only runs once and then stops. It is the most direct replacement for the Arduino delay() method. You start the delay and then when it is finished you do something. BasicSingleShotDelay is the plain code and SingleShotMillisDelay uses the millisDelay library.

BasicSingleShotDelay

This sketch is available in BasicSingleShotDelay.ino

int led = 13; // Pin 13 has an LED connected on most Arduino boards.
unsigned long DELAY_TIME = 10000; // 10 sec unsigned long delayStart = 0; // the time the delay started bool delayRunning = false; // true if still waiting for delay to finish void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, HIGH); // turn led on // start delay delayStart = millis(); delayRunning = true; } void loop() { // check if delay has timed out if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) { delayRunning = false; // finished delay -- single shot, once only digitalWrite(led, LOW); // turn led off } }

In the code above the loop() continues to run without being stuck waiting for the delay to expire.
During each pass of the loop(), the difference between the current millis() and the delayStart time is compared to the DELAY_TIME. When the timer exceeds the value of the interval the desired action is taken. In this example the delay timer is stopped and the LED turned off.

SingleShotMillisDelay

Here is the BasicSingleShotDelay sketch re-written using the millisDelay library. This sketch is available in SingleShotMillisDelay.ino

Here is the millisDelay version where the code above has be wrapped in class methods of the millisDelay class.

#include <millisDelay.h><br><br>int led = 13;
// Pin 13 has an LED connected on most Arduino boards.
millisDelay ledDelay;
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH); // turn led on
  // start delay
  ledDelay.start(10000);
}
void loop() {
  // check if delay has timed out
  if (ledDelay.justFinished()) {
    digitalWrite(led, LOW); // turn led off
  }
}

Repeating Timers

These are simple examples of a repeating delay/timer. BasicRepeatingDelay is the plain code and RepeatingMillisDelay uses the millisDelay library.

BasicRepeatingDelay

This sketch is available in BasicRepeatingDelay.ino

int led = 13; // Pin 13 has an LED connected on most Arduino boards.
unsigned long DELAY_TIME = 1500; // 1.5 sec unsigned long delayStart = 0; // the time the delay started bool delayRunning = false; // true if still waiting for delay to finish bool ledOn = false; // keep track of the led state void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, LOW); // turn led off ledOn = false; // start delay delayStart = millis(); delayRunning = true; } void loop() { // check if delay has timed out if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) { delayStart += DELAY_TIME; // this prevents drift in the delays // toggle the led ledOn = !ledOn; if (ledOn) { digitalWrite(led, HIGH); // turn led on } else { digitalWrite(led, LOW); // turn led off } } }

The reason for using
delayStart += DELAY_TIME;
to reset the delay to run again, is it allows for the possibility that the millis()-delayStart may be > DELAY_TIME because the millis() has just incremented or due to some other code in the loop() that slows it down. For example a long print statement. (See the Adding a Loop Montor in Step 7)

Another point is to start the delay at the end of startup(). This ensures the timer is accurate at the start of the loop(), even if startup() takes some time to execute.

RepeatingMillisDelay

Here is the BasicRepeatingDelay sketch re-written using the millisDelay library. This sketch is available in RepeatingMillisDelay.ino

#include <millisDelay.h>
int led = 13; // Pin 13 has an LED connected on most Arduino boards. bool ledOn = false; // keep track of the led state millisDelay ledDelay; void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, LOW); // turn led off ledOn = false; // start delay ledDelay.start(1500); } void loop() { // check if delay has timed out if (ledDelay.justFinished()) { ledDelay.repeat(); // start delay again without drift // toggle the led ledOn = !ledOn; if (ledOn) { digitalWrite(led, HIGH); // turn led on } else { digitalWrite(led, LOW); // turn led off } } }

Step 6: Other MillisDelay Library Functions

In addition to the start(delay), justFinished() and repeat() functions illustrated above, the millisDelay library also has
stop() to stop the delay timing out,

isRunning() to check if it has not already timed out and has not been stopped,

restart() to restart the delay from now, using the same delay interval,

finish() to force the delay to expire early,

remaining() to return the number of milliseconds until the delay is finished and

delay() to return the delay value that was passed to start()

Microsecond version of the library

millisDelay counts the delay in milliseconds. You can also time by microseconds. It is left as an exercise to the reader to write that class. (Hint: rename the class to microDelay and replace occurrences of millis() with micros() )

Freezing/Pausing a Delay

You can freeze or pause a delay by saving the remaining() milliseconds and stopping the delay and then later un-freeze it by restarting it with the remaining mS as the delay. e.g. see the FreezeDelay.ino example

mainRemainingTime = mainDelay.remaining();  // remember how long left to run in the main delay
mainDelay.stop(); // stop mainDelay  NOTE: mainDelay.justFinished() is NEVER true after stop()
…
mainDelay.start(mainRemainingTime);  // restart after freeze

Step 7: Word of Warning – Add a Loop Monitor

Unfortunately many of the standard Arduino libraries use delay() or introduce pauses, such as AnalogRead and SoftwareSerial. Usually the delays these introduce are small but they can add up so I suggest you add a monitor at the top of your loop() to check how quickly it runs.

The loop monitor is very similar to the blink example. A small piece of code at the top of the loop() method just toggles the Led each time loop() is executed. You can then use a digital multimeter with at Hz scale to measure the frequency of the output on the LED pin (pin 13 in this case)

The code is:-

// Loop Monitor – this checks that the loop() is executed at least once every 1mS
// (c)2013 Forward Computing and Control Pty. Ltd. // <a href="http://www.forward.com.au"> www.forward.com.au> // // This example code is in the public domain. int led = 13; // don't use on FioV3 when battery connected // Pin 13 has an LED connected on most Arduino boards. // if using Arduino IDE 1.5 or above you can use pre-defined // LED_BUILTIN instead of 'led' // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); // add your other setup code here } // the loop routine runs over and over again forever: void loop() { // toggle the led output each loop The led frequency must measure >500Hz (i.e. <1mS off and <1mS on) if (digitalRead(led)) { digitalWrite(led, LOW); // turn the LED off by making the voltage LOW } else { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) } // add the rest of your loop code here }

You can download the monitor code here. When I run this code on my Uno board, the multimeter on the Hz range connected between pin 13 and GND reads 57.6Khz. i.e. about 100 times >500hz.

As you add your code to loop() the Hz reading will reduce. Just check it stays well above 500Hz (1mS per loop() execution) in all situations.

Be the First to Share

    Recommendations

    • The 1000th Contest

      The 1000th Contest
    • Battery Powered Contest

      Battery Powered Contest
    • Hand Tools Only Challenge

      Hand Tools Only Challenge

    26 Discussions

    0
    cedrix00
    cedrix00

    2 months ago

    i have 3 voids in my void loop and i want to separate the one void's delay. i already did your instrucables it works but it also holds the other two voids. what i want is i want the other two voids to be function as normal but the other one i want to make a delay for that for 10mins of delay. but if i use your code the other two also afftects about the delay this is my void loop

    void loop(void)
    {
    Tempt();//normal delay
    DS3231();//normal delay
    VoltPerc();//10 mins delay
    }
    the thing is if i use millis function the other two also affects in 10 mins delay i want to just the VoltPerc() is the only one who is having a long delay npt the tempt and the ds3231. please help!

    0
    drmpf
    drmpf

    Reply 2 months ago

    Hi cedrix00,
    You can have three separate millisdelays, each with their own time setting, and control the calls accordingly.
    See Step 4 in https://www.instructables.com/id/Simple-Multi-task...
    for an example of this.
    If you are still having trouble, message me directly.

    0
    cedrix00
    cedrix00

    Reply 2 months ago

    Hello bro i already message you 😊

    0
    cedrix00
    cedrix00

    Reply 2 months ago

    Okay bro thank you for very fast response i will rewrite my code and use the steps in the link. I'll message you if i run into trouble

    0
    kratochviljan
    kratochviljan

    2 months ago

    Hi, listed examples of the code in Step 1 " How Not to Code a Delay in Arduino " and Step 2 " How to Write a Non-blocking Delay in Arduino " are exactly the same...

    0
    kratochviljan
    kratochviljan

    Reply 2 months ago

    Thanks. In the meantime, I already found your website, excellent, old, honest, website full of "real" straightforward information. Does not copycat superficial half-truths or misleading things like majority of similarly focused websites just to score some likes or whatever. It's obvious that you are really know how those things working. Thank you for that, I like it.
    And also thanks to Instructables, because they have better SEO, without them I would never have found your site. :-)

    0
    Bhanudas Ingale
    Bhanudas Ingale

    Question 5 months ago

    youe examples in this document are not non blocking delays? i have tested along with music paly through SD card. but only your program works and no sound through SD card. actualy SD card musi program is working independently. i think delay in this program block the microcontroller and program through SD card gete blocked.

    0
    drmpf
    drmpf

    Answer 5 months ago

    millisDelay certainly does not block, but whatever you are doing in the justFinished() might block or elsewhere in the code, could be blocking.
    SD card access can block. Also Serial.print can block
    Try adding a loopTimer() to get a better idea of what delays there are.
    from Simple Multitasking in Arduino on Any Board
    https://www.instructables.com/id/Simple-Multi-task...
    also see that instructable for non-blocking Serial I/O

    0
    PritPk
    PritPk

    Question 1 year ago on Step 3

    plesae help me for this programme.
    different 3 LED connect with 3 pins and i want to blink as below
    1LED(delay 5sec) -> 2LED (delay 5sec) -> 3LED (delay 5sec) -> all LED off [10 times]
    1LED(delay 4sec) -> 2LED (delay 4sec) -> 3LED (delay 4sec) -> all LED off [10 times]
    1LED(delay 3sec) -> 2LED (delay 3sec) -> 3LED (delay 3sec) -> all LED off [10 times]
    1LED(delay 2sec) -> 2LED (delay 2sec) -> 3LED (delay 2sec) -> all LED off [10 times]
    1LED(delay 1sec) -> 2LED (delay 1sec) -> 3LED (delay 1sec) -> all LED off [10 times]

    1
    drmpf
    drmpf

    Answer 1 year ago

    There is no very easy way to do this, the logic just gets messy, with multiple loops, sequence of leds, variable delays and number of repeats.

    Start small, 1 Led on for 5 sec then off for 5 sec and repeat ... for ever and ever (see the code in the answer below)
    Then add the 10 times counter to stop after 10 times (you have not said how long the leds all remain off)
    Then add a loop outside this to reset the 10 times counter and do it again and again, i.e. do it 10 times and then do it 10 times again. Use print statements to print out the current 10 time counter value so you can see what is happening.
    Then add another counter to count down the delays 5->4->3->2->1->stop each time you reset the 10 time counter for the next 10 times.
    Then you will have your 1 Led running.

    Finally instead of just turning 1 Led off when the delay finishes, turn 2 led on and also start another delay for it (see the code in the answer below) and so on. That is use another millisDelay for each Led and one for All LEDs off .

    An alternative is to use just one delay for all 3 leds and restart it for each led AND introduce a 'state' variable to keep track of which led is on and which one to turn on next. This 'state' variable can be as simple as an int, 1, 2, 3 for led 1,2,3 currently on.
    But try the separate delays first as it is a simpler approach

    0
    PritPk
    PritPk

    Reply 1 year ago

    thank you,
    i have done as below code.

    void setup() {
    pinMode(4,OUTPUT);
    pinMode(22,OUTPUT);
    pinMode(26,OUTPUT);
    pinMode(23,OUTPUT);
    Serial.begin(115200);
    }
    void loop()
    {
    for ( int i=5 ; i > 0; i-- )
    {
    for ( int j=0 ; j < 10 ; j++ )
    {
    digitalWrite(4,HIGH);
    delay(1000);
    digitalWrite(4,LOW);
    digitalWrite(22,HIGH);
    delay(1000);
    digitalWrite(22,LOW);
    digitalWrite(26,HIGH);
    delay(1000);
    digitalWrite(26,LOW);
    digitalWrite(23,HIGH);
    delay(1000);
    digitalWrite(23,LOW);
    }
    delay(1000*i);
    }
    }

    0
    drmpf
    drmpf

    Reply 1 year ago

    I think you wanted to vary the delays so perhaps
    delay(i*1000) instead.

    Delays are evil, but you won't notice that until you try and do something else as well as turn the leds on and off.

    0
    PritPk
    PritPk

    Reply 1 year ago

    ok thank you for soonest reply..

    i have another question related to bord ESP32 dev module v4
    what is the use of CMD pin,
    actually i am new in programming field.

    0
    drmpf
    drmpf

    Reply 12 months ago

    That is off topic here, email or msg me.

    0
    PritPk
    PritPk

    Reply 12 months ago

    Ok no problem.

    0
    Yakasaib
    Yakasaib

    1 year ago

    Hello i am trying to do something but i am getting a little confused. I'm trying to get a relay to switch on for 6 seconds, switch off for 30 seconds and then switch on for another 6 repeatedly. I just started getting into Arduino.

    0
    drmpf
    drmpf

    Reply 1 year ago

    There a couple of ways to do this. One uses just one millisDelay and keeps track of the relay state On/Off. A simplier approach is to use two millisDelays, one for the On and one for Off.
    Copy and paste the following code into Arduino and reformat it.

    // // flash led on Uno -- 6sec on and 30sec off using millisDelay
    #include <millisDelay.h>

    const int Led_pin = 13;
    millisDelay LedOnDelay;
    const unsigned long LED_ON_DELAY_MS = 6*1000; // 6 x 1000mS = 6sec
    millisDelay LedOffDelay;
    const unsigned long LED_OFF_DELAY_MS = 30*1000; // 30 x 1000mS = 30sec

    void setup() {
    // put your setup code here, to run once:
    pinMode(Led_pin, OUTPUT); // set led output
    // note some relay boards are active HIGH and some active LOW
    // start with led ON
    digitalWrite(Led_pin, HIGH); // turn led On
    LedOnDelay.start(LED_ON_DELAY_MS); // start delay
    }

    void loop() {
    // put your main code here, to run repeatedly:
    // check which timer has just finished

    if (LedOnDelay.justFinished()) {
    // turn led off and start offDelay
    digitalWrite(Led_pin, LOW); // turn led Off
    LedOffDelay.start(LED_OFF_DELAY_MS);
    }

    if (LedOffDelay.justFinished()) {
    // turn led on and start onDelay
    digitalWrite(Led_pin, HIGH); // turn led On
    LedOnDelay.start(LED_ON_DELAY_MS);
    }
    // other code here runs every loop

    }

    0
    drmpf
    drmpf

    Tip 1 year ago

    5th Sept 2019 update:
    Removing delay() calls is the first step to achieving simple multi-tasking on any Arduino board. The instructable Simple Multi-tasking in Arduino on Any Board covers all the other necessary steps.

    0
    drmpf
    drmpf

    1 year ago

    5th May 2019 update:
    Renamed isFinished() to justFinished(), as it only returns TRUE once just after the delay finishes.
    Added Freeze/Pause delay example