# Help blink leds with Millis() function

**URL:** <https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939>\
**Category:** Programming\
**Created:** [August 6, 2020, 3:38am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939 "2020-08-06T03:38:07Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 6, 2020, 5:39pm UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/21 "2020-08-06T17:39:42Z")

</div>

const int led1 = 10;  
const int led2 = 11;  
const int led3 = 12;  
const int led4 = 13; // the pin numbers for the LEDs

int Reset = 4;  
int Received = 0;

const int led\_Interval = 2300; // number of millisecs Motor is ON  
const int blinkDuration = 2500; // number of millisecs that Motor is OFF  
const int led\_Interval2 = 2100;  
const int blinkDuration2 = 2700;  
const int led\_Interval3 = 1800;  
const int blinkDuration3 = 3000;  
//------- VARIABLES (will change)

byte led1\_State = LOW; // used to record whether the LEDs are on or off  
byte led2\_State = HIGH; // LOW = off  
byte led3\_State = HIGH; //  
byte led4\_State = HIGH;  
//------- Time  
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()  
unsigned long previousled1Millis = 0; // will store last time the LED was updated  
unsigned long previousled2Millis = 0;  
unsigned long previousled3Millis = 10;  
void setup() {  
Serial.begin(9600);

[//Serial.println](https://Serial.println)("Starting SeveralThingsAtTheSameTimeRev1.ino"); // so we know what sketch is running

//------- set the Led pins as output:  
pinMode(led1, OUTPUT);  
pinMode(led2, OUTPUT);  
pinMode(led3, OUTPUT);  
pinMode(led4, OUTPUT);  
digitalWrite(led4, LOW);  
digitalWrite(Reset, HIGH);  
delay(200);  
pinMode(Reset, OUTPUT);  
Serial.println("wait");  
}

void loop() {  
if (Serial.available()\>0){  
Received = Serial.read();  
}  
// Notice that none of the action happens in loop() apart from reading millis()  
// it just calls the functions that have the action code  
currentMillis = millis(); // capture the latest value of millis()  
// this is equivalent to noting the time from a clock  
// use the same time for all LED flashes to keep them synchronized  
// call the functions that do the work  
updateled1\_State();  
updateled2\_State();  
switchLeds();  
}  
//========  
void updateled1\_State() {  
if (led1\_State == LOW) {  
if (currentMillis - previousled1Millis \>= blinkDuration) {  
led1\_State = HIGH;  
previousled1Millis += blinkDuration;  
}  
}  
else {  
if (currentMillis - previousled3Millis \>= led\_Interval) {  
led1\_State = LOW;  
previousled3Millis += led\_Interval;  
}  
}  
if (Received == 'a' || Received == 'b'){  
led1\_State = HIGH;  
led2\_State = HIGH;  
led4\_State = HIGH;  
}  
}  
//=========  
void updateled2\_State(){  
if (led2\_State == HIGH) {  
if (currentMillis - previousled3Millis \>= led\_Interval) {  
led2\_State = LOW;  
previousled3Millis += led\_Interval;  
}  
}  
else {  
if (currentMillis - previousled2Millis \>= blinkDuration) {  
led2\_State = HIGH;  
previousled2Millis += blinkDuration;  
}  
}  
if (Received == 'a' || Received == 'b'){  
led1\_State = HIGH;  
led2\_State = HIGH;  
led4\_State = HIGH;  
}  
}

//=======  
void switchLeds() {  
if (Received == '1' || Received == 'c'){  
// this is the code that actually switches the LEDs on and off  
digitalWrite(led1, led1\_State);  
digitalWrite(led2, led2\_State);  
}  
else{  
digitalWrite(led1, HIGH);  
digitalWrite(led2, HIGH);  
digitalWrite(led4, HIGH);  
}

//-------- Program Reset both app and arduino program  
if (Received == '9'){  
Serial.println("wait");  
digitalWrite(Reset, LOW);  
digitalWrite(Reset, HIGH);  
delay(200);

}  
}

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [August 6, 2020, 6:16pm UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/22 "2020-08-06T18:16:40Z")

</div>

Code needs to go in the timer “if” statement.

Your best bet to understand this would not to be to try to integrate into another (more complex) sketch yet.

Start with the steps I outlined from Blink Without Delay.

Once you understand how to modify that sketch to produce the blink patterns you want, then you are ready to move on to modifying more complex sketches.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 5:56am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/23 "2020-08-07T05:56:55Z")

</div>

unsigned long startMillis; //some global variables available anywhere in the program  
unsigned long currentMillis;  
const unsigned long period = 1000; //the value is a number of milliseconds  
const byte ledPin = 13; //using the built in LED

void setup()  
{  
Serial.begin(115200); //start Serial in case we need to print debugging info  
pinMode(ledPin, OUTPUT);  
startMillis = millis(); //initial start time  
}

void loop()  
{  
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)  
if (currentMillis - startMillis \>= period) //test whether the period has elapsed  
{  
digitalWrite(ledPin, !digitalRead(ledPin)); //if so, change the state of the LED. Uses a neat trick to change the state  
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.  
}  
}

> 74younus:  
> For your simplicity I posted only necessary working portion(loop section) of it . Do check if code or syntax is correct.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 7, 2020, 6:04am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/24 "2020-08-07T06:04:11Z")

</div>

Does that sketch blink the LED every 2 seconds ?

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 6:06am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/25 "2020-08-07T06:06:12Z")

</div>

I found this code posted by someone . This code blinks led . I tried to blink two LEDs with this code but I failed to do so . I am unable to understand this.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 7, 2020, 6:11am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/26 "2020-08-07T06:11:52Z")

</div>

> I found this code posted by someone .

EDIT  
~~It is the Blink example from the IDE~~  
Actually it is the BlinkWithoutDelay example from the IDE

Does it turn the LED on for 2 seconds and off for 2 seconds ?

> I am unable to understand this

Do you mean that you don't understand this sketch ? Which parts are you stuck on ?

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 6:20am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/27 "2020-08-07T06:20:17Z")

</div>

No every second as period is set to 1000 milliseconds.  
I tried to blink two LEDs with this code but I failed...  
I also tried to change the Off time of the built in led but failed.  
I am using this forum on mobile so I have some problems while I write reply.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 6:27am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/28 "2020-08-07T06:27:38Z")

</div>

I does make it On for 1sec and Off for one sec . I want to elongate its off time and also I want to use another led that will also blink but out of period with first led. OK

Sorry for my English .I use mobile for all these replies.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 6:35am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/29 "2020-08-07T06:35:15Z")

</div>

It works fine only blinking of LEDs is causing me trouble when on goes off another goes on which I hate the most. I want to keep other off for some time whoever first one goes off.  
Thank you for your reply.

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [August 7, 2020, 6:46am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/30 "2020-08-07T06:46:36Z")

</div>

> 74younus:  
> I does make it On for 1sec and Off for one sec . I want to elongate its off time and also I want to use another led that will also blink but out of period with first led. OK
> 
> Sorry for my English .I use mobile for all these replies.

"out of period with first led"--\> [When 1 is OFF 2 is ON] -AND- [when 1 is ON 2 is OFF] ?

-jim lee

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 7, 2020, 6:47am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/31 "2020-08-07T06:47:29Z")

</div>

> It is the Blink example from the IDE

Sorry for any confusion.  
It is actually the BlinkWithoutDeley function from the IDE

> I want to elongate its off time

Start with this

```arduino
const byte ledPin =  LED_BUILTIN;

byte ledState = LOW;
unsigned long previousMillis = 0; 
unsigned long interval = 1000; 

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    if (ledState == LOW)
    {
      ledState = HIGH;
      interval = 500;
    }
    else
    {
      ledState = LOW;
      interval = 3000;
    }
    digitalWrite(ledPin, ledState);
  }
}

```

Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 8:48am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/32 "2020-08-07T08:48:43Z")

</div>

Yes and when one goes off I want a pause and then other is on

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 9:02am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/33 "2020-08-07T09:02:00Z")

</div>

I have some issue with your suggested code. I tried but libraries idlers or TLSlib Tools not responding and I am not able to use them i even I visited you GITHUB . I also tried after installing blinker library.

---

<div class="post-metadata">

**Author:** ![pcbbc](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@pcbbc](https://forum.arduino.cc/u/pcbbc)\
**Post date:** [August 7, 2020, 9:37am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/34 "2020-08-07T09:37:20Z")

</div>

Well done...

```cpp
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000;  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
 Serial.begin(115200);  //start Serial in case we need to print debugging info
 pinMode(ledPin, OUTPUT);
 startMillis = millis();  //initial start time
}

void loop()
{
 currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
 if (currentMillis - startMillis >= period)  //test whether the period has elapsed
 {
  digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
  startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
 }
}

```

Step by step....

1. Can you see what part of this code makes the LED stay ON/OFF for 1 second?  
Hint: 1 second = 1000 milliseconds.  
Change this so the LED stats ON and OFF for 2 seconds each time.  
Test it.

2. Can you see the statement that turns the LED ON and OFF?  
Hint: It involves the [digitalWrite()](https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/) function.  
Study the digitalWrite function documentation. You will need it later.  
Remove this line of code. Replace it with the code you originally wrote in post #17.  
Somehow that code has disappeared, but you can find a commented copy of it in my reply #18.

3. Actually make your code turn the LEDs ON and OFF.  
Hint: It involves the digitalWrite() function you studied earlier and the “if” statements you already wrote.  
Test it.  
Probably it doesn’t work yet because you have not fixed the other issues with that code as you wrote it...

4. Remove the count = 0 statement.  
Instead, where my comment is about resetting the counter to 0 when it reaches 4, make that happen correctly.  
Hint: It involves another “if” statement to check for count == 4, and then the count = 0 statement you misplaced previously.  
Test it.

Now does the code do what you want?  
If not please post back the code you ended up with and we will help you correct it.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 10:15am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/35 "2020-08-07T10:15:05Z")

</div>

When Jim goes down I am already down waiting him.  
When he reaches to me , we stay down for some time.  
Now I go up and come back down to Jim and wait sometime with Jim . Now Jim again goes up and come backed to me and wait some time

This is what I want from this forum to solve with Millie() and two LEDs thank you.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 10:45am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/36 "2020-08-07T10:45:10Z")

</div>

It is actually the BlinkWithoutDeley function from the IDE  
Start with this

```cpp
const byte ledPin =  LED_BUILTIN;

byte ledState = LOW;
unsigned long previousM

void loop()
{
  unsigned long currentMillis = millis();
 

Yes your code is very good and it worked for one led and I will declare new variables for led2 . But should I also declare a new currentMillis for led 2??

Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

```

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 11:35am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/37 "2020-08-07T11:35:07Z")

</div>

Yes your code worked

I will declare all new variables for led 2  
but should I also declare a new " current Milli()" for led 2 or not

void loop()  
{  
unsigned long current Milli= milli()

Now add a completely separate set of variables for a second LED, a second test for the end of period and turn the second LED on/off as required

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [August 7, 2020, 11:40am UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/38 "2020-08-07T11:40:01Z")

</div>

@pcbbc

Not sure what was changed in the post.  
Report closed.

---

<div class="post-metadata">

**Author:** ![74younus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/74younus/32/443647_2.png) [@74younus](https://forum.arduino.cc/u/74younus)\
**Post date:** [August 7, 2020, 12:14pm UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/39 "2020-08-07T12:14:39Z")

</div>

I want this function in milli()

//blink two LEDs  
digital write(led 1, HIGH);  
Delay(2000);

Digital write (led 1, LOW);

Delay(2000);

Digital write (led 2, HIGH);

Delay(2000);

Digital write (led 2, LOW);

Delay (2000);  
Please suggest your codes that do this in milli(). :o

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 7, 2020, 12:18pm UTC](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939/40 "2020-08-07T12:18:21Z")

</div>

> should I also declare a new " current Milli()" for led 2 or not

You only need one currentMillis variable. The best place to give it a value from millis() is at the start of loop(). You could have 2 variables but it is not necessary and could be confusing

[Previous page](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939.md?page=1)

[Next page](https://forum.arduino.cc/t/help-blink-leds-with-millis-function/668939.md?page=3)
