Beginning Arduino: Delay Without Delay()

19,889

30

12

About: Jack passed away May 20, 2018 after a long battle with cancer. His Instructables site will be kept active and questions will be answered by our son-in-law, Terry Pilling. Most of Jack's instructables are tu...

When you use the delay() function your program stops and nothing else can happen during the delay.

That is easy, but what if you want to have something else going on during the delay?

The answer; use millis().

This tutorial is a simple sketch and circuit to show how this is done.

You will need:

  • Arduino
  • Breadboard
  • Jumper wires
  • 2 - LEDs, I used one red and one green
  • 2 - 330-560 Ohm resistors, for LEDs
  • Pushbutton switch

Step 1: The Circuit

Follow the diagram and build the circuit from the parts list on the previous page.

Step 2: The Code

/*********************************************************
 * Demonstration using millis() instead of delay() so 
 * another activity can happen within the delay.
 *
 * The anode of a red LED is connected to pin 10 with a 
 * resistor in series connected to ground. 
 *
 * The anode of a green LED is connected to pin 11 with a 
 * resistor in series connected to ground. 
 *
 * A pushbutton switch is connected to pin 12 and ground.
 *
 * The red LED blinks on for one second then off for one
 * second.
 *
 * The green LED lights when the button is pressed.
 *
 *********************************************************/


unsigned long time = millis();

int toggle = 1;

/**********************************************
 * setup() function
 **********************************************/
void setup()
{
  pinMode(10, OUTPUT);        //Red LED
  pinMode(11, OUTPUT);        //Green LED

  pinMode (12, INPUT_PULLUP); //Switch
  
  digitalWrite(10, HIGH);     //Initial state of red LED
}

/**********************************************
 * loop() function
 **********************************************/
void loop()
{
  if(millis()-time > 1000)     //Has one second passed?
  {
    toggle = !toggle;          //If so not toggle 
    digitalWrite(10, toggle);  //toggle LED
    time = millis();           //and reset time.
  }
 
  digitalWrite(11, !digitalRead(12)); //Light green LED if Button pressed.
}

Share

    Recommendations

    • Cardboard Challenge

      Cardboard Challenge
    • PCB Contest

      PCB Contest
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest

    12 Discussions

    0
    None
    drmpf

    Tip 3 months ago

    I have posted a more detailed instructable about avoiding Arduino delays()
    CODING TIMERS AND DELAYS IN ARDUINO
    https://www.instructables.com/id/Coding-Timers-and...
    It explains why you need to use (millis() - time) and includes a simple library that does the logic for you, millisDelay.
    Also covers repeating timers and stopping delays before they expire.

    millisDelayCropped.jpgmillisDelay.jpg
    0
    None
    chuckr44

    1 year ago

    Hmm. Would millis() be used if I want to poll a pin to check the value once every 10 seconds or so? Like a pin with a temperature reading?

    0
    None
    HughB20

    1 year ago

    Hi I tried this as my second ever Arduino project. Follow the wiring guide, I thought successfully, copied and compiled and downloaded the code, but nothing! Can you tell from a photo where I might have gone wrong???? Thanks for any help you might be able to give...... Hugh

    arduino switch.jpg
    1 reply
    0
    None
    JRV31HughB20

    Reply 1 year ago

    I can't see anything from the photo.

    Is the polarity of the LEDs correct?

    0
    None
    fieryfredo

    1 year ago

    Thank you for this, I had googled some other ways to using millis() instead of delay() but nothing clicked as to how I could get it to work until your example.

    1 reply
    0
    None
    JRV31fieryfredo

    Reply 1 year ago

    I'm glad you liked it, glad I cold help.

    0
    None
    JRV31

    3 years ago on Introduction

    I just posted an instructable where I show how to do multiple timed events with different timing at the same time, look here:

    https://www.instructables.com/id/Beginning-Arduino-delay-without-delay-multiple-tim/

    2 replies
    0
    None
    DivideWorks

    4 years ago

    Thanks for sharing. I have a project where I want to combine two sketches and run the loops simultaneously on the same arduino, but both loops contain delays which interfere with each other. I was about to resort to using two arduinos, but hope this will do the trick.

    1 reply
    0
    None
    JRV31DivideWorks

    Reply 4 years ago on Introduction

    Good luck with your project, let me know how it turns out.

    You should be able to have a time1 and a time2 running at the same time.