how to replace delay() to millis()

Hii

I am looking to use the basic blink without delay program in multiple led blinks.I have made a program using delay().How can i replace delay() with millis().Please advice to what are the changes i have to be done for achieving the target.

i am new in Arduino UNO and try to study the program.

int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT); 
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(timer);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(timer); 
  digitalWrite(led2, HIGH);
delay(timer);
digitalWrite(led2, LOW);
delay(timer);}
digitalWrite(led3, HIGH);
delay(timer);
digitalWrite(led3, LOW);
delay(timer);}// wait for a second
}

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking and it blinks a few LEDs. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Have this on me

unsigned long startTimes[] = {0, 0, 0, 0};  //start time of current blink for each LED
unsigned long periods[] = {500, 510, 520, 530};    //period for each LED
const byte ledPins[] = {3, 5, 6, 9};  //the LED pins
const int numberOfLeds = sizeof(ledPins) / sizeof(ledPins[0]);  //calculate the number of LEDs

void setup()
{
  Serial.begin(115200);
  Serial.print("Number of LEDs : ");
  Serial.println(numberOfLeds);
  for (int pin = 0; pin < numberOfLeds; pin++)  //use the number of LEDs to set pinMode()s
  {
    pinMode(ledPins[pin], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis(); //get current time for all timing in loop()
  for (int led = 0; led < numberOfLeds; led++)  //for each LED
  {
    if (currentTime - startTimes[led] > periods[led]) //if the period has ended
    {
      digitalWrite(ledPins[led], !digitalRead(ledPins[led])); //change the state of the LED
      startTimes[led] = currentTime; //save the start time of the blink for the current LED
    } //end of test for period ended
  } //end of for loop
} //end of loop()

Please read the code and comments so that you learn something. It presumably does not do exactly what you want but it is easy to change the LED pin numbers, how many LEDs there are and the periods. Note the use of arrays and a for loop to shorten the code and the need to keep loop() running freely

UKHeliBob:
Have this on me

Please read the code and comments so that you learn something. It presumably does not do exactly what you want but it is easy to change the LED pin numbers, how many LEDs there are and the periods. Note the use of arrays and a for loop to shorten the code and the need to keep loop() running freely

Sir,
in my program led will get ON one by one.That is first led will ON after 1 minute it will get OFF and Led2 get ON again after 1 minute Led2 get OFF and Led3 get ON ..This cycle will repeat..
but as per your program leds getting ON/OFF Randomly.

as per your program leds getting ON/OFF Randomly.

The fact that you say that tells me that you do not understand how the program works so probably don't understand the use of millis() for timing. The LEDs most certainly do not go on and off randomly

I am sorry but your description of what you want is not clear, possibly because you did not use any commas in the description so it is impossible to tell what timing belongs to which LED. From your program that uses delay()s I believe that what you want is for each LED to come on in turn for 1 minute then off for 5 minutes

If that is the case then a 1 minute time period using millis() would be good. Set a counter to zero initially. When the period ends increment the counter. When it reaches 6 set it back to zero. The program can be in one of 6 states. In 3 of them an LED is on, in 3 of them all LEDs are off

Then, in loop() if the counter is 0 turn on LED1 and turn the others off. If the counter is 2 turn on LED2 and turn the others off. If the counter is 4 turn on LED3 and turn the others off. If the counter is 1, 3 or 5 turn off all of the LEDs

If that is not the pattern that you want then please explain in more detail what it is you want

Think of how you handle time.

If you are going out at 6pm and its noon, you don't sit and look at your watch for 6 hours waiting for 6pm
(well I hope not). You do other things and regularly check the time. When its time, you go out.

So instead of

void loop()
{
  delay(6 * HOUR);
  go_out();
}

You do

void loop()
{
  if (millis() >= go_out_time)
  {
    go_out();
    go_out_time = next_time_to_go_out();
  }
  do_other_checks_and stuff () ;
}

Use if and millis(), never while or delay(), just like you in real life.

UKHeliBob:
If that is not the pattern that you want then please explain in more detail what it is you want

i want to ON the 3 LED one by one.I have 3 led .

LED1-ON (1Mts on time)
LED2-OFF
LED3-OFF

LED1-OFF
LED2-ON(1Mts on time)
LED3-OFF

LED1-OFF
LED2-OFF
LED3-ON (1Mts on time)

I have made the program using delay() and its working(program already posted in ma first post).Now i want to repalce the delay() with millis().

i think you may get my point.

sreekanthmp:
I have made the program using delay() and its working(program already posted in ma first post).Now i want to repalce the delay() with millis().

Have you carefully studied the example programs you have been given in Replies #1 and #2? What have you learned from them?

…R

LED1-ON (1Mts on time)
LED2-OFF
LED3-OFF

LED1-OFF
LED2-ON(1Mts on time)
LED3-OFF

LED1-OFF
LED2-OFF
LED3-ON (1Mts on time)

That is not what your program using delay()s does. In that program there is a delay() between turning LED1 off and LED2 on

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(timer);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(timer);
  digitalWrite(led2, HIGH);
  delay(timer);
  digitalWrite(led2, LOW);
  delay(timer);

I suppose that I should point out that the original program that you posted does not compile because the loop() function ends too early and so there is code outside of a function

UKHeliBob:
I suppose that I should point out that the original program that you posted does not compile because the loop() function ends too early and so there is code outside of a function

sorry sir,
Now i corrected that mistake and compile,its ok

int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT); 
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(timer);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led2, HIGH);
delay(timer);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
delay(timer);
digitalWrite(led3, LOW);
}

OK, now the code matches your description and it compiles

Now you only have 3 states instead of 6

Start by writing a small program using millis() for timing that increments a counter (starting at zero) each time the timing period ends. When the counter reaches 3 set it back to zero. At the start of each timing period print the value of the counter. Don't worry about the LEDs at this time

HINT : most of the work is already done for you in the BlinkWithoutDelay example. Come back when you have got the simple (no LED) program working or if you are stuck.

Here is your code with millis() function in place of delay() function.

int led = 13;
int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  timeDelay();//delay(timer);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led2, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led3, LOW);
}

void timeDelay()
{
  unsigned long presentMillis = millis();//read present time of Arduino Timer
  while(millis()- presentMillis <= 1000)  //checking if 1000 ms has elapsed
  {
    ; //do nothing
  }
}

ARKF:
Here is your code with millis() function in place of delay() function.

int led = 13;

int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(led3, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 timeDelay();//delay(timer);               // wait for a second
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 digitalWrite(led2, HIGH);
 timeDelay();//delay(timer);
 digitalWrite(led2, LOW);
 digitalWrite(led3, HIGH);
 timeDelay();//delay(timer);
 digitalWrite(led3, LOW);
}

void timeDelay()
{
 unsigned long presentMillis = millis();//read present time of Arduino Timer
 while(millis()- presentMillis <= 1000)  //checking if 1000 ms has elapsed
 {
   ; //do nothing
 }
}

thank you ARKF…its working perfectly…

Here is your code with millis() function in place of delay() function.

Congratulations. You have just invented the delay() function.

Your code blocks the free running of loop() just as effectively as if you had used delay().

Here is a challenge for you. Add code to blink a fourth LED or print a message every 100 milliseconds whilst the three LEDs continue their sequence.

sreekanthmp:
thank you ARKF...its working perfectly...

See my comment above. The delay() function has been replaced by a function that still blocks the free running of loop(). If that is acceptable then you might just as well use delay()

UKHeliBob:
Congratulations. You have just invented the delay() function.

It has been there for years; we failed to discover it; but, @ARKF (with only 5 posts) has brought it to the OP. Congratulation is not enough; I would offer + for his quick understanding of what the OP has wanted – a simple functional replacement of delay() function by millis() function.

ARKF:
Here is your code with millis() function in place of delay() function.

int led = 13;

int led2 = 12;
int led3 = 11;
int timer = 1000;
// the setup routine runs once when you press reset:
void setup()
{
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()
{
  digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
  timeDelay();//delay(timer);              // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(led2, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  timeDelay();//delay(timer);
  digitalWrite(led3, LOW);
}

void timeDelay()
{
  unsigned long presentMillis = millis();//read present time of Arduino Timer
  while(millis()- presentMillis <= 1000)  //checking if 1000 ms has elapsed
  {
    ; //do nothing
  }
}

He might as well use delay as this code blocks.

Romonaga:
He might as well use delay as this code blocks.

The OP has not asked for non-blocking codes. Probably, he has wished to get familiar with the usage of millis() function.

GolamMostafa:
The OP has not asked for non-blocking codes. Probably, he has wished to get familiarization with the usage of millis() function.

:slight_smile: fair enough.

GolamMostafa:
The OP has not asked for non-blocking codes. Probably, he has wished to get familiar with the usage of millis() function.

That sounds exceptionally unlikely.

The whole purpose of using millis() instead of delay() is to replace blocking code with non-blocking code.

Using millis() to create a different version of delay() would be the equivalent of re-inventing the wheel.

...R

GolamMostafa:
The OP has not asked for non-blocking codes

At least, not yet