# Double Blink and delay for 1000 MS without delay()

**URL:** <https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431>\
**Category:** Programming\
**Created:** [September 11, 2021, 1:27pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431 "2021-09-11T13:27:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 1:27pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/1 "2021-09-11T13:27:44Z")

</div>

Hi, I want to make a LED blink project without delay() LIKE A POLICE STROBE LIGHT. The project is like

LED 13 ON DELAY(50)  
LED 13 OFF DELAY(50)  
LED 13 ON DELAY(50)  
LED 13 OFF

DELAY(1000)

But the LED only blink for 1000 ms...

Pls solution.

```cpp
int ledPin = 13; // the LED pin connected to NANO
unsigned long OnTime = 50; // milliseconds of on-time
unsigned long noOfTime = 1000; //milliseconds of time for blinker
unsigned long previousMillis = 0; // will store last time LED was updated
bool ledState = LOW;
unsigned long previousMillis1 = 0; //for blinker timing

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

void loop()
{
  if ( ( millis() - previousMillis1 >= noOfTime))
  {
    blinker();
    previousMillis1 = millis();
  }
}

void blinker() {
  if (ledState == LOW) ledState = HIGH;
  else ledState = LOW;
  {
    digitalWrite(ledPin, ledState); // Update the actual LED
  }
  previousMillis = millis();
}

```

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 11, 2021, 1:53pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/2 "2021-09-11T13:53:14Z")

</div>

Hello  
A timer and a structured array containing led state and time of on/off duration should work.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 11, 2021, 1:57pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/3 "2021-09-11T13:57:17Z")

</div>

You have a variable

```cpp
    unsigned long OnTime = 50; // milliseconds of on-time`

```

that you never use. It looks like you haven't completely thought through how you are even trying to do this.

_Yet._

Also: you do this

```
  previousMillis = millis();

```

in blinker() AND in loop(). It is redundant in blinker(), remove it from that function to clarify.

a7

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 11, 2021, 2:19pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/4 "2021-09-11T14:19:39Z")

</div>

An alternative way to do this would be to update a counter every 50ms. When the counter reaches 20, set it back to zero. If the counter is 0 or 2, switch on the led, else switch off the led.

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:24pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/5 "2021-09-11T14:24:41Z")

</div>

I have use `unsigned long noOfTime = 1000; `  
as per ur statement .,, the LED still blink for 1000ms....

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:32pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/6 "2021-09-11T14:32:39Z")

</div>

```cpp
void loop()
{

  for (int counter = 0; counter <= 20; counter++) 
  {
    blinker();
  }
}

void blinker() {
  if ( ( millis() - previousMillis >= OnTime))
  {
    if (ledState == LOW) ledState = HIGH;
    else ledState = LOW;
    digitalWrite(ledPin, ledState); // Update the actual LED

    previousMillis = millis();
  }

}

```

See modified the code. Now the led blink continously for 50ms. Not stopped while counter reached 20.

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:36pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/7 "2021-09-11T14:36:58Z")

</div>

Thanks . I tried a structured like this.

```nohighlight
{
	// Class Member Variables
	// These are initialized at startup
	int ledPin; // the number of the LED pin
	long OnTime; // milliseconds of on-time
	long OffTime; // milliseconds of off-time

	// These maintain the current state
	int ledState; // ledState used to set the LED
	unsigned long previousMillis; // will store last time LED was updated

  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
	ledPin = pin;
	pinMode(ledPin, OUTPUT);     
	  
	OnTime = on;
	OffTime = off;
	
	ledState = LOW; 
	previousMillis = 0;
  }

  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
    	ledState = LOW; // Turn it off
      previousMillis = currentMillis; // Remember the time
      digitalWrite(ledPin, ledState); // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH; // turn it on
      previousMillis = currentMillis; // Remember the time
      digitalWrite(ledPin, ledState); // Update the actual LED
    }
  }
};

Flasher led1(13, 50, 1000);

 
void setup() 
{ 

} 
 
 
void loop() 
{ 
  led1.Update();
 }

```

But i need double blink or tripple blink etc. Now the led blink for 50ms and off for 1000ms as per the programme

---

<div class="post-metadata">

**Author:** ![magentaline](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@magentaline](https://forum.arduino.cc/u/magentaline)\
**Post date:** [September 11, 2021, 2:40pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/8 "2021-09-11T14:40:03Z")

</div>

> [@PaulRB](#):
>
> An alternative way to do this would be to update a counter every 50ms. When the counter reaches 20, set it back to zero. If the counter is 0 or 2, switch on the led, else switch off the led

Nice

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 11, 2021, 2:43pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/9 "2021-09-11T14:43:19Z")

</div>

> [@t\_tuku](#):
>
> LED 13 ON DELAY(50)  
> LED 13 OFF DELAY(50)  
> LED 13 ON DELAY(50)  
> LED 13 OFF
> 
> DELAY(1000)

Check if the following sketch serves your purpose. The sketch makes 4 blinks; where the first 31/2 blinks are with 1000 ms on-period and off-period. The last off-time of the last blink is 3000 ms. This sketch uses "Blink Without Delay" approach.

```arduino
unsigned long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
const int ledPin = LED_BUILTIN;// the number of the LED pin
int ledState = LOW; // ledState used to set the LED
byte counter = 0;

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

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    if(interval == 3000)
    {
      interval = 1000;
      counter = 0;
    }
    previousMillis = currentMillis;
    if (ledState == LOW)
    {
      ledState = HIGH; //ON
    }
    else
    {
      ledState = LOW; //OFF
    }
    counter++;
    digitalWrite(ledPin, ledState);
    if (counter == 4)
    {
      interval = 3000;
    } 
  }
}

```

---

<div class="post-metadata">

**Author:** ![almytom](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/almytom/32/345495_2.png) [@almytom](https://forum.arduino.cc/u/almytom)\
**Post date:** [September 11, 2021, 2:46pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/10 "2021-09-11T14:46:54Z")

</div>

Not that it really matters here, but just to keep in mind, when you update previousMillis by setting it to either currentMillis or millis() the time will drift and be longer than you expect. To keep it accurate, do this instead:

```cpp
previousMillis += OnTime;

```

That keeps the long term timing accurate no matter what you do in code.

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:53pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/11 "2021-09-11T14:53:10Z")

</div>

Thanks but can't understand counter=4; but the led light for 2times. why?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 11, 2021, 2:54pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/12 "2021-09-11T14:54:22Z")

</div>

You have wanted:  
ON for 50 ms (in my sketch 1000 ms)  
OFF for 50 ms (in my sketch 1000 ms)  
ON for 50 ms (in my sketch 1000 ms)  
and then OFF for 1000 ms (in my sketch 3000 ms)  
and then repeats

So, counter = 3+1 = 4. (two blinks)

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:55pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/13 "2021-09-11T14:55:29Z")

</div>

thnks .. many many thnks...  
let me add it to the structure that i mentioned in above post..

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 2:58pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/14 "2021-09-11T14:58:20Z")

</div>

I did this type of earlier... But when i put counter=10, the led blink for 4 times... Now I understand...

😑

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 11, 2021, 3:02pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/15 "2021-09-11T15:02:54Z")

</div>

> [@t\_tuku](#):
>
> I did this type of earlier... But when i put counter=10, the led blink for 4 times... Now I understand.

counter == 10 means the Led has blinked 41/2 times; there remains the last off-time of 3000 ms (in your case 1000 ms). I have checked my sketch, and it blinks exactly for five times with last off-time for 3000 ms.

---

<div class="post-metadata">

**Author:** ![magentaline](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@magentaline](https://forum.arduino.cc/u/magentaline)\
**Post date:** [September 11, 2021, 3:05pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/16 "2021-09-11T15:05:32Z")

</div>

Imo, PaulRB's method of counting in 50ms intervals and not changing the interval but turning the led off at count 0 or 2 and otherwise on is a more elegant way.

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 3:06pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/17 "2021-09-11T15:06:52Z")

</div>

pls make a small code..I cant understand what he told. I am just beginner.. learning from you all.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 11, 2021, 3:09pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/18 "2021-09-11T15:09:14Z")

</div>

> [@t\_tuku](#):
>
> I am just beginner.

If you are a beginner, then why have you introduced **object** based codes in Post-7? Have you copied it from somewhere? I have preapred my skecth of Post-9 based on the Arduino's example of "[Blink Without Delay](https://www.arduino.cc/en/Tutorial/BuiltInExamples)".

---

<div class="post-metadata">

**Author:** ![magentaline](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@magentaline](https://forum.arduino.cc/u/magentaline)\
**Post date:** [September 11, 2021, 3:09pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/19 "2021-09-11T15:09:21Z")

</div>

> [@t\_tuku](#):
>
> pls make a small code..I cant understand what he told. I am just beginner.. learning from you all.

Code below implements PaulRB's approach:

```arduino
// https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431
// implements counter as suggested by PaulRB

/*
  BASED ON Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin

// Variables will change:
//int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change:
const long interval = 50; // interval at which to blink (milliseconds)
byte counter = 0;

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // turn led on at count 0 or 2, otherwise off
    if (counter == 0 || counter == 2)
      digitalWrite(ledPin, HIGH);
    else
      digitalWrite(ledPin, LOW);

    counter++;
    //reset counter
    if (counter == 20) counter = 0;

  }
}

```

---

<div class="post-metadata">

**Author:** ![t\_tuku](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/t_tuku/32/527557_2.png) [@t\_tuku](https://forum.arduino.cc/u/t_tuku)\
**Post date:** [September 11, 2021, 3:12pm UTC](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431/20 "2021-09-11T15:12:14Z")

</div>

Yes sir I copied. and learning the why they giving tutorials.

copied from

> **[Multi-tasking the Arduino - Part 1](https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-clean-sweep)**
>
> Once you have mastered the basic blinking leds, simple sensors and buzzing motors, it’s time to move on to bigger and better projects. That usually involves combining bits and pieces of simpler sketches and trying to make them work together. The...

[Next page](https://forum.arduino.cc/t/double-blink-and-delay-for-1000-ms-without-delay/904431.md?page=2)
