Arduino Millis() Examples

Example code using millis()

You’ve recently learned about millis() and can’t way to delete all of your references to delay().  The problem is that you don’t know quite how to convert your code into millis()-compatible code.  Here is a (running) list of millis() examples I’ve put together to help.  If you want to know when more are added make sure you subscribe to the RSS feed or email notification list.

Baldengineer’s Arduino millis() Examples

  1. Arduino Multitasking – Step by step examples of how to convert delay() code into millis() based code, to simulate multitasking.
  2. Police Lights – Flash two LEDs like strobing police lights
  3. Control ON and OFF time for a flashing LED. – More control than “blink without delay”
  4. Stopwatch Example – Calculate how much time code takes to execute
  5. Chasing LEDs – Larson-scanner style chasing pattern
  6. De-bounch a button (or switch) – No need for de-bouncing capacitor
  7. Delayed events after a button push – Timed events (button push is just example.)
  8. analogWrite() PWM Fading – No delay() and a simple function to keep a LED fading with PWM
  9. Detect Short and Long Button Press – Give one button multiple functions

You might also want to check out my “Blink Without Delay – Line by Line  Tutorial.” It is a much more in-depth explanation than the comments provided with the Arduino IDE example.

Long comments, URLs, and code tend to get flagged for spam moderation. No need to resubmit.

ALL comments submitted with fake or throw-away services are deleted, regardless of content.

Don't be a dweeb.

Leave a comment

58 thoughts on “Arduino Millis() Examples

  1. Dear bald engineer, (Excuse my english)
    Something puzzle me about millis()
    You used millis() in a way to overcome a roll over problem.
    if (millis() – previousMillis > 1000)

    I don’t understand where the problem is with
    NextTime = millis() + 1000;
    if (millis() >= NextTime)
    As I understand it, if millis() is at his unsigned long limit when I add a Delay of say 1000 to it, NextTime will have a roll over value of 1000. and when millis() is incremented it will revert to 0 and go up until it attain 1000 and the condition will be true. Where is my error in my reasonning?
    Thank’s for helping me understand this question that puzzle me..

    • The problem is how often the if-statement is checked. Here is a practical example where using the “increment” variable fails.

      Let’s say the max millis value is 4,294,967,295. And let’s say when you run “NextTime = millis() + 1000;” millis is 4,294,966,000.
      Now let’s say your code is:

      NextTime = millis() + 1000;
      if (millis() >= NextTime){}
      

      NextTime has the value of 295, because it rolled over. However, millis() has not rolled over yet. So on the next statement, millis() is 4,294,966,002. The if-statement is:

      if (4,294,966,000 > 295). That evaluates as true! And will be true until millis() finally rolls over.

      • Perfect example James. Now I understand why your method is better in the long run.
        Thank you very much.

        Continue your great work, it help a lot of us coding better programs.

  2. Delays[] contain all the delay for the events. The program will add each one in turn to the millis() counter and each time the loop is executed it will check if millis() is now greater than the old millis()+delay implying that it is now the time to switch something on or off. The GoBlink will check which event is due using the array Events[] and switch on or off the led accordingly.

    I made the assumption that your code run indefinitely that blinking pattern and that you wanted your program to do other thing simultaneously. But if this is not the case you could tell me what exactly you want to do. If you want to do many blinking patterns commanded by push button we could alter the solution to suit your need. Just tell me more of what you want to do in detail so I can help you. How many pattern, how many push button, etc.

    • Thanks very much for your reply and help. Basically, I have 1 push button I am using to toggle through 3 modes, (1) mode is all leds on, (2) mode strobe pattern (3) mode leds off. The problem was all the delays and the push button not responding. Perhaps I could put your function into mode 2, call that function on the 2 nd button push.

      void loop() {
      if (digitalRead(3) != state) {
      state = digitalRead(3);
      if (digitalRead(3) == HIGH) {
      mode += 1;
      if (mode > 2) {
      mode = 0;
      }

      if (mode == 0);{

      //all leds on

      }

      if (mode == 1) {

      GoBlink();

      }

      if (mode == 2){

      //all leds off

      }

      something like this is what Im trying to do. Before, with all the delays in the code, it would hang up the digitalRead on pin 3, so it took like 10 pushes just to get a toggle

      • Good idea,

        You shoud reset the mode = 2 variables when a button is pushed just in case the next mode = 2;

        if (digitalRead(3) == HIGH) {
        mode += 1;
        if (mode > 2) mode = 0;
        NextTime = 0;
        NextEvent = 0;
        }

        for mode = 1 you should test if it is time for the next event before jumping to GoBlink()

        if (mode == 1 & millis() >= NextTime) GoBlink();

        When I have a single very short statement in a “if” i don’t use the bracket. I find it cleaner. But maybe it is not a good practice. Do it the way you prefer.

        Good luck!

        • well basically Im not trying to reset, just need to toggle between modes
          your code works great, have the strobes on now, but I want to toggle with a button push, to another mode, like off, on, strobes, then another mode perhaps, front and back on, with side marker leds strobing

          I dont know how to implement that with your code, into the mode toggles

          • Hi Tim, sorry for the delay. ;O)

            Here is the code you need. Was tested with success.
            Work only for around 49 continous days until millis roll over. ;O)

            By the way where are you from?
            I’m from Quebec, Canada

            #define MaxEvent 9 // number of event
            #define ledwstrobe 2 // Define white led pin
            #define ledrstrobe 3 // Define Red led pin
            #define ButtonPin 4 // Define button pin

            // Buttons with Pull-Ups are “backwards” so LOW = Pushed, HIGH = Released

            byte Mode = 0; // Mode 0=On, 1=Blink pattern, 2=Off

            enum EventType {BothLo,BothHi,RedLo,RedHi,WLo,WHi}; // Type of event for blink

            EventType Event[] = {BothLo,RedHi,RedLo,WHi,WLo,WHi,WLo,WHi,WLo}; // Actual event suite
            int Delays[] = {125,125,750,62,63,62,63,125,0}; // Actual delay after event

            int NextEvent = 0; // Keep track of the next event value 0 – MaxEvent
            unsigned long NextTime = 0; // When millis() >= NextTime then Event is due
            unsigned long LastDebounceTime = 0; // When millis() >= NextButtonTime then bounce time is due
            unsigned long DebounceDelay = 50; // 50ms for general debounce delay

            int ButtonState = HIGH; //Variable for current button reading
            int LastButtonState = HIGH; //Variable for last known button reading

            void setup() {
            pinMode(ledwstrobe,OUTPUT);
            pinMode(ledrstrobe,OUTPUT);
            pinMode(ButtonPin,INPUT_PULLUP);
            }

            void loop() {

            // Check the push button
            int reading = digitalRead(ButtonPin);

            //in the case that the reading is not equal to the last state set the last debounce time to current millis time
            if (reading != LastButtonState) {
            LastDebounceTime = millis();
            }
            //check the difference between current time and last registered button press time, if it’s greater
            if ((millis()-LastDebounceTime) > DebounceDelay) {
            if (reading != ButtonState) {
            ButtonState = reading;
            if (ButtonState == LOW) { // Button Pushed
            Mode++; // increment Mode
            if (Mode == 3) Mode=0; // Roll over if Mode is too big
            NextEvent = 0; // Reset to first Event
            NextTime = millis(); // Reset the Nextime Event Delay
            }
            }
            }
            LastButtonState = reading; // Keep the last button state

            // do what the Mode variable says
            switch (Mode) {
            case 0:
            digitalWrite(ledwstrobe,HIGH); // LED White On
            digitalWrite(ledrstrobe,HIGH); // LED RED On
            break;
            case 1:
            if (millis() >= NextTime) GoBlink(); // If delay is passed, GoBlink
            break;
            case 2:
            digitalWrite(ledwstrobe,LOW); // LED White Off
            digitalWrite(ledrstrobe,LOW); // LED Red Off
            break;
            }

            // do whatever here
            }

            // Blinking routine

            void GoBlink() {
            switch (Event[NextEvent]) {
            case BothLo:
            digitalWrite(ledwstrobe,LOW);
            digitalWrite(ledrstrobe,LOW);
            break;
            case BothHi:
            digitalWrite(ledwstrobe,HIGH);
            digitalWrite(ledrstrobe,HIGH);
            break;
            case RedHi:
            digitalWrite(ledrstrobe,HIGH);
            break;
            case RedLo:
            digitalWrite(ledrstrobe,LOW);
            break;
            case WHi:
            digitalWrite(ledwstrobe,HIGH);
            break;
            case WLo:
            digitalWrite(ledwstrobe,LOW);
            break;
            }
            NextTime = millis() + Delays[NextEvent];
            NextEvent++;
            if (NextEvent == MaxEvent) NextEvent = 0;
            }

          • Hi Tim, Sorry for the delay(). ;O)

            Here is your program tested and working.
            By the way, where are you from?
            I’m from Quebec, Canada

            Have fun!

            #define MaxEvent 9 // number of event
            #define ledwstrobe 2 // Define white led pin
            #define ledrstrobe 3 // Define Red led pin
            #define ButtonPin 4 // Define button pin

            // Buttons with Pull-Ups are “backwards” so LOW = Pushed, HIGH = Released

            byte Mode = 0; // Mode 0=On, 1=Blink pattern, 2=Off

            enum EventType {BothLo,BothHi,RedLo,RedHi,WLo,WHi}; // Type of event for blink

            EventType Event[] = {BothLo,RedHi,RedLo,WHi,WLo,WHi,WLo,WHi,WLo}; // Actual event suite
            int Delays[] = {125,125,750,62,63,62,63,125,0}; // Actual delay after event

            int NextEvent = 0; // Keep track of the next event value 0 – MaxEvent
            unsigned long NextTime = 0; // When millis() >= NextTime then Event is due
            unsigned long LastDebounceTime = 0; // When millis() >= NextButtonTime then bounce time is due
            unsigned long DebounceDelay = 50; // 50ms for general debounce delay

            int ButtonState = HIGH; //Variable for current button reading
            int LastButtonState = HIGH; //Variable for last known button reading

            void setup() {
            pinMode(ledwstrobe,OUTPUT);
            pinMode(ledrstrobe,OUTPUT);
            pinMode(ButtonPin,INPUT_PULLUP);
            }

            void loop() {

            // Check the push button
            int reading = digitalRead(ButtonPin);

            //in the case that the reading is not equal to the last state set the last debounce time to current millis time
            if (reading != LastButtonState) {
            LastDebounceTime = millis();
            }
            //check the difference between current time and last registered button press time, if it’s greater
            if ((millis()-LastDebounceTime) > DebounceDelay) {
            if (reading != ButtonState) {
            ButtonState = reading;
            if (ButtonState == LOW) { // Button Pushed
            Mode++; // increment Mode
            if (Mode == 3) Mode=0; // Roll over if Mode is too big
            NextEvent = 0; // Reset to first Event
            NextTime = millis(); // Reset the Nextime Event Delay
            }
            }
            }
            LastButtonState = reading; // Keep the last button state

            // do what the Mode variable says
            switch (Mode) {
            case 0:
            digitalWrite(ledwstrobe,HIGH); // LED White On
            digitalWrite(ledrstrobe,HIGH); // LED RED On
            break;
            case 1:
            if (millis() >= NextTime) GoBlink(); // If delay is passed, GoBlink
            break;
            case 2:
            digitalWrite(ledwstrobe,LOW); // LED White Off
            digitalWrite(ledrstrobe,LOW); // LED Red Off
            break;
            }

            // do whatever here
            }

            // Blinking routine

            void GoBlink() {
            switch (Event[NextEvent]) {
            case BothLo:
            digitalWrite(ledwstrobe,LOW);
            digitalWrite(ledrstrobe,LOW);
            break;
            case BothHi:
            digitalWrite(ledwstrobe,HIGH);
            digitalWrite(ledrstrobe,HIGH);
            break;
            case RedHi:
            digitalWrite(ledrstrobe,HIGH);
            break;
            case RedLo:
            digitalWrite(ledrstrobe,LOW);
            break;
            case WHi:
            digitalWrite(ledwstrobe,HIGH);
            break;
            case WLo:
            digitalWrite(ledwstrobe,LOW);
            break;
            }
            NextTime = millis() + Delays[NextEvent];
            NextEvent++;
            if (NextEvent == MaxEvent) NextEvent = 0;
            }

      • Third time I try to put the code. Hope this time it will work

        Here is your code Tim. Program is tested and working

        Have fun!

        #define MaxEvent   9   // number of event
        #define ledwstrobe 2   // Define white led pin
        #define ledrstrobe 3   // Define Red led pin
        #define ButtonPin	 4	  // Define button pin
        		
        // Buttons with Pull-Ups are "backwards" so LOW = Pushed, HIGH = Released
        
        byte Mode = 0;									// Mode 0=On, 1=Blink pattern, 2=Off 
        
        enum EventType {BothLo,BothHi,RedLo,RedHi,WLo,WHi};    				// Type of event for blink
        
        EventType Event[] = {BothLo,RedHi,RedLo,WHi,WLo,WHi,WLo,WHi,WLo}; // Actual event suite
        int Delays[] = {125,125,750,62,63,62,63,125,0};                   // Actual delay after event
        
        int NextEvent = 0;            		// Keep track of the next event value 0 - MaxEvent
        unsigned long NextTime = 0;   		// When millis() >= NextTime then Event is due
        unsigned long LastDebounceTime = 0;	// When millis() >= NextButtonTime then bounce time is due 
        unsigned long DebounceDelay = 50;	// 50ms for general debounce delay
        
        int ButtonState = HIGH;   				//Variable for current button reading
        int LastButtonState = HIGH;			//Variable for last known button reading
        
        void setup() {
             pinMode(ledwstrobe,OUTPUT);
             pinMode(ledrstrobe,OUTPUT);
        	  pinMode(ButtonPin,INPUT_PULLUP);
        }
        
        void loop() {
        	
        	// Check the push button
        	int reading = digitalRead(ButtonPin);
          
        	//in the case that the reading is not equal to the last state set the last debounce time to current millis time
        	if (reading != LastButtonState)  {
        		LastDebounceTime = millis();
        	}
        	//check the difference between current time and last registered button press time, if it's greater 
        	if ((millis()-LastDebounceTime) > DebounceDelay) {
        		if (reading != ButtonState) {
        			ButtonState = reading;
        			if (ButtonState == LOW) {				// Button Pushed
        				Mode++;									//	increment Mode
        				if (Mode == 3) Mode=0;				// Roll over if Mode is too big
        				NextEvent = 0;							// Reset to first Event
        				NextTime = millis();					// Reset the Nextime Event Delay
        			}
        		}
        	}		
        	LastButtonState = reading;						// Keep the last button state
        	
        	
        	// do what the Mode variable says
        	switch (Mode) {
        		case 0:
        			digitalWrite(ledwstrobe,HIGH);				// LED White On
        			digitalWrite(ledrstrobe,HIGH);				// LED RED On
        			break;
        		case 1:
        			if (millis() >= NextTime) GoBlink();		// If delay is passed, GoBlink
        			break;
        		case 2:
        			digitalWrite(ledwstrobe,LOW);					// LED White Off
        			digitalWrite(ledrstrobe,LOW);					// LED Red Off
        			break;
        	}
           
           // do whatever here 
        }
        
        // Blinking routine
        
        void GoBlink() {
           switch (Event[NextEvent]) {
              case BothLo:
                 digitalWrite(ledwstrobe,LOW);
                 digitalWrite(ledrstrobe,LOW);
                 break;
              case BothHi:
                 digitalWrite(ledwstrobe,HIGH);
                 digitalWrite(ledrstrobe,HIGH);
                 break;
              case RedHi:
                 digitalWrite(ledrstrobe,HIGH);
                 break;
              case RedLo:
                 digitalWrite(ledrstrobe,LOW);
                 break;
              case WHi:
                 digitalWrite(ledwstrobe,HIGH);
                 break;
              case WLo:
                 digitalWrite(ledwstrobe,LOW);
                 break;
           }
           NextTime = millis() +  Delays[NextEvent];
           NextEvent++;
           if (NextEvent == MaxEvent) NextEvent = 0;
        }
        
        • dude your a genuis….is it possible to add another function that only has the ledrstrobe, for example, strobing and the ledwstrobe HIGH, like to simulate the front and rear lights HIGH (no strobe) and side marker leds, strobing? im still processing how the events map to the delays to the millis..awesome job dude! thanks so much

          • Hi Tim,
            I’m very far from a genius. James is probably nearer to that definition.
            Like I said earlier, i’m French Canadian so English is a second language for me, so I don’t fully understand what you want exactly.

            Correct me if i’m wrong and you can complete.
            So from what I understand, you would like another strobing pattern added. I suppose from another push of the same button. In that new configuration you would have not 2 but 4 lights (LED) In that case the front red light would strobe and the white light would be on at all time. In the back the red would be On and what is side marker?

            Or maybe you have 6 LED. 2 for the front, 2 for the back and 2 for the light on the top of the car

            So please explain in greater detail so I can try to help you better.

            Don’t fuck yourself understanding the code right now cause the next one will be inspired by, but relatively different.

            What are you trying to do by the way? Building something you can put on your car to simulate a police car. ;O)

        • Thanks again for your help and reply. I have total 8 lights, 2 in front, 2 in back and 2 on each side, of the bike

          So for the last function, I would like to keep the 2 fronts and 2 backs on or HIGH, and strobe the 4 side lights. Just a cool patter I would like to achieve.

          This is just for my bike, to be seen from all angles at night

          So this last function would be mode 4, same push button

  3. int switch1 = 2;
    int i[] = {3, 4, 5, 6, 7, 8};
    int BASE = 3 ; // the I/O pin for the first LED
    int NUM = 6; // number of LEDs
    int ledDelay(45);// Delay
    
    void setup() {
      pinMode(2, INPUT_PULLUP);
      for (int i = BASE; i < BASE + NUM; i ++)
      { pinMode(i, OUTPUT); // set I/O pins as output
        digitalWrite(i, HIGH);
        delay(ledDelay * 8);
      }
    }
    
    void loop() {
      switch1 = digitalRead(2);
      if (switch1 == HIGH) {
        for (int i = BASE; i < BASE + NUM; i ++)
        { digitalWrite(i, HIGH);
        }
      } else {
        for (int i = BASE; i < BASE + NUM; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay); // delay
        }
        for (int i = BASE; i < BASE + NUM; i ++)
        {
          digitalWrite(i, LOW); // set I/O pins as “high”, turn on LEDs one by one
          delay(ledDelay); // delay
        }
        for (int i = BASE + NUM ; i >= BASE; i --)
        {
          digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
          delay(ledDelay); // delay
        }
        for (int i = BASE + NUM ; i >= BASE; i --)
        {
          digitalWrite(i, LOW); // set I/O pins as “high”, turn on LEDs one by one
          delay(ledDelay); // delay
        }
        for (int i = BASE; i < BASE + NUM; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        for (int i = BASE + NUM ; i >= BASE; i --)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          digitalWrite(i - 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        for (int i = BASE; i < BASE + NUM; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        for (int i = BASE; i < BASE + NUM - 1; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        for (int i = BASE; i < BASE + NUM - 2; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        for (int i = BASE; i < BASE + NUM - 3; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        for (int i = BASE; i < BASE + NUM - 4; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        for (int i = BASE; i < BASE + NUM - 5; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(3, HIGH);
        for (int i = BASE; i < BASE + NUM - 6; i ++)
        {
          digitalWrite(i, HIGH); // set I/O pins as “low”, turn off LEDs one by one.
          delay(ledDelay);
          digitalWrite(i + 1, HIGH);
          digitalWrite(i, LOW);
          delay(ledDelay); // delay
        }
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(3, HIGH);;
        delay(ledDelay * 2); // delay
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(3, LOW);
        delay(ledDelay * 2); // delay
        delay(ledDelay * 2); // delay
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(3, HIGH);
        delay(ledDelay * 2); // delay
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(3, LOW);
        delay(ledDelay * 2); // delay
        digitalWrite(8, HIGH);
        digitalWrite(7, HIGH);
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(3, HIGH);
        delay(ledDelay * 2); // delay
        digitalWrite(8, LOW);
        digitalWrite(7, LOW);
        digitalWrite(6, LOW);
        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(3, LOW);
        delay(ledDelay * 2); // delay
      }
    }
    
    
    • You need to implement a state machine. This is not something I can help you with. If you need one-on-one help, please post your questions on the “Programming” section of the Arduino.cc forums.

  4. In response to Tim, Here is how i do it.
    Hope this help!

    #define MaxEvent   9   // number of event
    #define ledwstrobe 2   // Define white led pin
    #define ledrstrobe 3   // Define Red led pin
    
    enum EventType {BothLo,BothHi,RedLo,RedHi,WLo,WHi};     // Type of event
    
    EventType Event[] = {BothLo,RedHi,RedLo,WHi,WLo,WHi,WLo,WHi,WLo};  // Actual event suite
    int Delays[] = {125,125,750,62,63,62,63,125,0};                    // Actual delay after event
    
    unsigned long NextTime = 0;   // When millis() &gt; NextTime then Event is due
    int NextEvent = 0;            // Keep track of the next event value 0 - MaxEvent
    
    void setup() {
         pinMode(ledwstrobe,OUTPUT);
         pinMode(ledrstrobe,OUTPUT);
    }
    
    void loop() {
       if (millis() > NextTime) GoBlink();
       
       // do whatever here 
    
    }
    
    void GoBlink() {
       switch (Event[NextEvent]) {
          case BothLo:
             digitalWrite(ledwstrobe,LOW);
             digitalWrite(ledrstrobe,LOW);
             break;
          case BothHi:
             digitalWrite(ledwstrobe,HIGH);
             digitalWrite(ledrstrobe,HIGH);
             break;
          case RedHi:
             digitalWrite(ledrstrobe,HIGH);
             break;
          case RedLo:
             digitalWrite(ledrstrobe,LOW);
             break;
          case WHi:
             digitalWrite(ledwstrobe,HIGH);
             break;
          case WLo:
             digitalWrite(ledwstrobe,LOW);
             break;
       }
       NextTime = millis() +  Delays[NextEvent];
       NextEvent++;
       if (NextEvent == MaxEvent) NextEvent = 0;
    }
    
    • Hi thanks again for your code and help. Does this code you provided solve by delay problem? I have too many delays in my code. It seems you moved the delays into the int Delays[ ]

      • Hello Tim,
        I’m sorry but english is not my first language so i don’t understand your question. (Solve by delay problem, Do you mean Solve my delay problem. If this is the question, the answer is yes) You could add as many event as you want. Just add them in the two arrays Event[] and Delays[] and update the value of MaxEvent to reflect the total number of event. That way your code is free to run anything you want. Each time the loop is executed the program test if the next event is due, if not, it continue, if yes, it go to the GoBlink routine. It is as simple as that.

        Tell me if it work for you.

  5. Dear Bald Engineer,

    I was wondering if there is a way to turn the strobe pattern below into mills code?

    digitalWrite(ledwstrobe, LOW);       //set off white strobe
      digitalWrite(ledrstrobe, LOW);       //set off red strobe
        delay(125);
      digitalWrite(ledrstrobe, HIGH);     //set on red strobe
        delay(125);
      digitalWrite(ledrstrobe, LOW);       //set off red strobe
        delay(750);
      digitalWrite(ledwstrobe, HIGH);     //set white strobe on
        delay(62);
      digitalWrite(ledwstrobe, LOW);       //set white strobe off
        delay(63);
      digitalWrite(ledwstrobe, HIGH);      //set white strobe on
        delay(62);
      digitalWrite(ledwstrobe, LOW);       //set white strobe off
        delay(63);
      digitalWrite(ledwstrobe, HIGH);     //set white strobe on
        delay(125);
      digitalWrite(ledwstrobe, LOW);      //set white strobe off
        delay(0);
    }
    
    • Thank you for your reply and help.

      Are these int Delays[] = {125,125,750,62,63,62,63,125,0}; // Actual

      or are those timestamps, for use in the milis code?

      im trying to use a push button to toggle through modes, and it wont work with all m delays. will your code solve this issue?

  6. Hi, I am trying this on TInkerCad and the turn LED does not blink. I have the sketch exactly like above but the only thing I changed was the codes for the remote buttons.ANy idea why

    • TinkerCad is a 3D modeling tool (that is no longer supported.) So I have no idea how you would run code there. It works fine on a real Arduino. If it isn’t working in your code, then there is an issue with your code.