HOW TO MAKE 2 LEDS BLINK AT THE SAME TIME?

Hello, I started using the Arduino board today, so I am a total beginner. I am using a simulator called 123D circuits.

I have made my Arduino so that the led lights flash one after another:

int led = 13;
int led2 = 12;
int led3 = 11;

// 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(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
delay(1000);
digitalWrite(led3, HIGH);
delay(1000);
digitalWrite(led3, LOW);
delay(1000);// wait for a second
}

However, I want to make the led lights blink at the same time.

Help?

Please use code tags when posting.

Put statements together:

digitalWrite(led, HIGH);
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led, LOW);
digitalWrite(led2, LOW);
1 Like

If you want the LEDs to blink in different sequences, without using 'delay()', ('millis()'-based timing), check out this thread:-
Demonstration code for several things at the same time

Edit: This may be beyond what you want to achieve at the moment, but it's still worth a look. The sooner you become acquainted with 'millis()'-based timing, the better. :slight_smile:

I want my program to do this:

4 LEDS to be on at the same time, expect all four are blinking at different rates (ex. one is blinking faster/slower than the other).

Then, I want all of the different speed blinking LEDs to stop for a fixed time.

Then, I want them to start again...and so on..

I have figured out how to connect the LEDs to the arduino. I know how to make the LEDS blink simultaneously one after another at different rates in one program, and I know how to make them blink on and off at the same time in another. I don't quite know how to join the two...aka implement this program.

Any help would be appreciated. Thanks!

1 Like

See reply #2 in your other post.

How can 4 LEDs that blink at different rates be on at the same time? You need to seriously reconsider your requirements or how you phrase them.

And, you need to stop crossposting.

Here's a short sketch that blinks 3 LEDs randomly without delays, maybe you can get some ideas.

const byte red = 5, grn = 6, yel = 7; // LED pins
const byte dur = 30; // duration of blink
const int mn = 500, mx = 2000; // interval between blinks
unsigned long strt1, strt2, strt3;
unsigned long ival1, ival2, ival3; // interval between blinks
byte cnt;

void setup() {
  Serial.begin(9600);
  Serial.println (F(__FILE__"\n"));
  pinMode(red,OUTPUT);
  pinMode(grn,OUTPUT);
  pinMode(yel,OUTPUT);
  strt1 = strt2 = strt3 = millis();
}

void loop() {
  if(cnt > 25){
    randomSeed(analogRead(5)); // new seed after 25 red blinks
    cnt = 0;
  }  
  if(millis() - strt1 > ival1) digitalWrite(red,HIGH);
  if(millis() - strt1 > ival1 + dur){
    digitalWrite(red,LOW);
    ival1 = random(mn,mx);
    strt1 = millis(); cnt++;
  }
  if(millis() - strt2 > ival2) digitalWrite(grn,HIGH);
  if(millis() - strt2 > ival2 + dur){
    digitalWrite(grn,LOW);
    ival2 = random(mn,mx);
    strt2 = millis();
  }
  if(millis() - strt3 > ival3) digitalWrite(yel,HIGH);
  if(millis() - strt3 > ival3 + dur){
    digitalWrite(yel,LOW);
    ival3 = random(mn,mx);
    strt3 = millis();
  }

}

How to do multiple things at once ... like cook bacon and eggs

It starts like this:

This question comes up practically every day on the Arduino forum - "how do I blink two LEDs at different rates?" or "how do I turn on and off two motors at different times?".

One of the problems is that beginners look at the "blink" tutorial program, which is:

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

void loop()
{
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}

Now this works fine, to blink one LED. But to blink two LEDs you run into problems. It's OK if you want to blink them both at once:

void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(12, HIGH); // set the first LED on
digitalWrite(13, HIGH); // set the second LED on
delay(1000); // wait for a second

digitalWrite(12, LOW); // set the first LED off
digitalWrite(13, LOW); // set the second LED off
delay(1000); // wait for a second
}

Or, if you want to do one after the other:

void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}

void loop()
{
digitalWrite(12, HIGH); // set the first LED on
delay(1000); // wait for a second
digitalWrite(12, LOW); // set the first LED off
delay(1000); // wait for a second

digitalWrite(13, HIGH); // set the second LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the second LED off
delay(1000); // wait for a second
}

But what if you want to blink the two LEDs at different rates? Like, once a second for LED 1 and twice a second for LED 2?

This is where the delay function doesn't really help.

Let's look at an analogy. Say you want to cook breakfast. You need to cook:

Coffee - takes 1 minute
Bacon - takes 2 minutes
Eggs - takes 3 minutes

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time.
Glance at watch from time to time. When one minute is up then ...
Start cooking bacon. Look at watch and note the time.
Glance at watch from time to time. When another minute is up then ...
Put coffee on. Look at watch and note the time.
When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

So, adapting this idea to blinking two LEDs at different rates, we can't afford to use:

delay (1000);

... because this blocks. That is, you can't be doing anything else during those 1000 milliseconds.

Instead we note the time, like this:

unsigned long when_eggs_started; // declare variable
..

when_eggs_started = millis (); // note the time now

Later on we can see if 3 minutes are up by subtracting when we started from the time now. This, by the way, works even if the millis () function call wraps around after approximately 50 days.

if ((millis () - when_eggs_started) >= (1000UL * 60 * 3))
{
// eggs are cooked
}

The "UL" after 1000 is to force the compiler to make an "unsigned long" literal, which can hold a larger number than the default of an int (integer) which can only hold -32768 to +32767.

Note that millis () returns milliseconds, thus 1000 milliseconds are a single second. So 1000 milliseconds * 60 * 3 is three minutes.

The full sketch that blinks two LEDs at different rates is:

// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 500;
const unsigned long redLEDinterval = 1000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
greenLEDtimer = millis ();
redLEDtimer = millis ();
} // end of setup

void toggleGreenLED ()
{
if (digitalRead (greenLED) == LOW)
digitalWrite (greenLED, HIGH);
else
digitalWrite (greenLED, LOW);

// remember when we toggled it
greenLEDtimer = millis ();
} // end of toggleGreenLED

void toggleRedLED ()
{
if (digitalRead (redLED) == LOW)
digitalWrite (redLED, HIGH);
else
digitalWrite (redLED, LOW);

// remember when we toggled it
redLEDtimer = millis ();
} // end of toggleRedLED

void loop ()
{

// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();

// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();

/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */

} // end of loop

1 Like

GoForSmoke:
How to do multiple things at once ... like cook bacon and eggs

It starts like this:

What pins do the two go in?

does one go in pin 13 and GND and what pin does the other go in?

Arduino pin 13 lights the on-board led. The leds can connect to any IO pin, Nick picked 12 and 13 but those can be changed.

// Which pins are connected to which LED
const byte greenLED = 12;
const byte redLED = 13;

Thank you GoForSmoke! Great explanation in #7 and helped me tremendously.
You're the best!
Mark

markmaidique:
Thank you GoForSmoke! Great explanation in #7 and helped me tremendously.
You're the best!
Mark

That is Nick Gammon's site, he was here years before me.

His blogs cover a lot of subjects that come up regularly on the forum, all done very well.

When you learn timing you can set code to run on time events, start to write with and for events. Change in pin state is another kind of event as is Serial.available() > 0, real-time, real world code that you're started on.

The next big in terms of useful lesson is the State Machine, how to make code that responds to changing conditions, even changes it makes. Nick's State Machine example is about halfway down his Serial Without Blocking tutorial.

Spend time picking up logical tricks before you write anything -big- but write/change examples just to burn connections in your wetware, practice gets you solid where just reading does not.

// police light

int led = 13;
int led1 = 11;
int led2 = 12;

void setup() {
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);}

void loop()
//red
{digitalWrite(led, HIGH); //1
delay(75);
digitalWrite(led, LOW);
delay(75);
digitalWrite(led, HIGH); //2
delay(75);
digitalWrite(led, LOW);
delay(75);
digitalWrite(led, HIGH); //3
delay(75);
digitalWrite(led, LOW);
delay(75);
digitalWrite(led, HIGH); //4
delay(75);
digitalWrite(led, LOW);
delay(75);

digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led1, LOW);
delay(20);

//green
digitalWrite(led2, HIGH); //1
delay(75);
digitalWrite(led2, LOW);
delay(75);
digitalWrite(led2, HIGH); //2
delay(75);
digitalWrite(led2, LOW);
delay(75);
digitalWrite(led2, HIGH); //3
delay(75);
digitalWrite(led2, LOW);
delay(75);
digitalWrite(led2, HIGH); //4
delay(75);
digitalWrite(led2, LOW);
delay(75);
}

Demo of blinking 1 led at a time says what?