Double Blink and delay for 1000 MS without delay()

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.

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();
}

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

1 Like

You have a variable

    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

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.

2 Likes

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

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.

Thanks . I tried a structured like this.

{
	// 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

Nice

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.

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;
    } 
  }
}
1 Like

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:

previousMillis += OnTime;

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

1 Like

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

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)

1 Like

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

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

:expressionless:

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.

1 Like

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.

1 Like

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

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".

1 Like

Code below implements PaulRB's approach:

// 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;

  }
}
2 Likes

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

copied from