In the blog Blink LED By Arduino, we learn how to control a single LED. In this blog, we learn we learn how to control 8 LEDs by in a different manner by changing the code. We learn the following pattern.


These led blinking differently by changing some code in the upper code 4 in the void loop()

  • Light up all the LEDs in turn--------------------oneAfterAnotherLoop()
  • Turn on one LED at a time----------------------oneOnAtATime();
  • Light the LEDs middle to the edges------------pingPong();
  • Chase lights like you see on signs--------------marquee();
  • Blink LEDs randomly-----------------------------randomLED();

 

Hardware Required

  • Arduino UNO R3 Board with cable
  • Eight LEDs
  • Eight 330 ohm resistors
  • Breadboard
  • Nine Jumper wire

First, connect a jumper wire from GND to the negative rail on the breadboard.

The Cathode of the eight LEDs are connected in series with a 330-ohm resistor and connected to the negative rail of the breadboard as shown in the figure. Now Anode of eight LEDs are connected to Arduino board’s pin no 2,3,4,5,6,7,8,9 respectively.

 

 


Initialize the Arduino pins and tell which pin is connected to which LED (int led no = arduino pin no)

Then in the void setup tell which pin work as output or input pinMode(led no, OUTPUT);

After that in void loop function write your actual command for controlling the LEDs.

digitalWrite(led no, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

digitalWrite(led no, LOW);    // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

 

Open the Arduino program and upload the following code to your Arduino board. This code is the very basic of controlling LEDs.

 

**********************************************************************

// Code 1

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int led1 = 2;

int led2 = 3;

int led3 = 4;

int led4 = 5;

int led5 = 6;

int led6 = 7;

int led7 = 8;

int led8 = 9;

// Setup the 8 pins as OUTPUT

void setup() {              

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(led6, OUTPUT);

pinMode(led7, OUTPUT);

pinMode(led8, OUTPUT);

}

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led3, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led4, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                            // wait for a second

digitalWrite(led5, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led6, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led7, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led8, HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                         // wait for a second

digitalWrite(led1, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

digitalWrite(led2, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

digitalWrite(led3, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

digitalWrite(led4, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

digitalWrite(led5, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                     // wait for a second

digitalWrite(led6, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                     // wait for a second

digitalWrite(led7, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                         // wait for a second

digitalWrite(led8, LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                       // wait for a second

}

 

**********************************************************************

Let's see what happen……….

 

Now we reduce the code

 

From

To

int led1 = 2;

int led2 = 3;

int led3 = 4;

int led4 = 5;

int led5 = 6;

int led6 = 7;

int led7 = 8;

int led8 = 9;

int ledPin[] = {2,3,4,5,6,7,8,9};

 

 

 

To keep track of all the LED pins, we'll use an "array". An array lets you store a group of variables, and refer to them by their position, or "index". Here we're creating an array of eight integers and initializing them to a set of values

 

The first element of an array is the index 0. We've put the value "2" in index 0, "3" in index 1, etc. The final index in the above array is 7, which contains the value "9".

We're using the values in this array to specify the pin numbers that the eight LEDs are connected to. LED 0 is connected to pin 2, LED 1 is connected to pin 3, etc.

 

 

From

To

// Setup the 8 pins as OUTPUT

void setup() {              

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(led6, OUTPUT);

pinMode(led7, OUTPUT);

pinMode(led8, OUTPUT);

}

void setup()

{

int index;

 for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

 }

}

 

 

 

In this sketch, we'll use "for() loops" to step variables from one value to another, and perform a set of instructions for each step. For() loops are a very handy way to get numbers to count up or down.

Every for() loop has three statements separated by semicolons (;):

1. Something to do before starting

2. A test to perform; as long as it's true, keep looping

3. Something to do after each loop (increase a variable)

 

For the for() loop below, these are the three statements:

1. index = 0;    Before starting, make index = 0.

2. index<= 7;   If the index is less or equal to 7, run the following code.

  (When index = 8, continue with the sketch.)

3. index++   Putting "++" after a variable means "add one to it".

(You can also use "index = index + 1".)

4. index--   Putting "--" after a variable means "subtract one to it".

  (You can also use "index = index - 1".)

 

Every time you go through the loop, the statements following the for() (within the brackets) will run. 

When the test in statement 2 is finally false, the sketch will continue.

Here we'll use a for() loop to initialize all the LED pins to OUTPUTs. This is much easier than writing eight separate statements to do the same thing. 

This for() loop will make index = 0, then run the pinMode() statement within the brackets. It will then do the same thing

for index = 2, index = 3, etc. all the way to index = 7 

 

 ledPins[index] is replaced by the value in the array. For example, ledPin[0] is 2

 

From

To

digitalWrite(led1, HIGH);   

digitalWrite(ledPin[0], HIGH);

digitalWrite(led1, LOW);

digitalWrite(ledPin[0], LOW);

 

Now code become following

**********************************************************************

// Code 2

// initialize the pins and tell which pin are connected to which LED

int ledPin[] = {2,3,4,5,6,7,8,9};

 

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

 }

}

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(ledPin[0], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[1], HIGH);   // turn the LED (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[2], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[3], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[4], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[5], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[6], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[7], HIGH);   // turn the LED on (HIGH is the voltage level at 5 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[0], LOW);   // turn the LED on ((LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[1], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[2], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[3], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[4], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[5], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[6], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

digitalWrite(ledPin[7], LOW);   // turn the LED on (LOW  is the voltage level at 0 volt)

delay(100);                                     // wait for a second

}

**********************************************************************

 

Let's see what happen………. 

Further, reduce the code.

 

From

To

void loop() {

digitalWrite(ledPin[0], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[1], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[2], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[3], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[4], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[5], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[6], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[7], HIGH);   // turn the LED on (HIGH is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[0], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[1], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[2], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[3], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[4], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[5], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[6], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

digitalWrite(ledPin[7], LOW);   // turn the LED on (LOW is the voltage level)

  delay(100);                 // wait for a second

}

 

void loop()

{

int index;

int delayTime = 100; // delay time

  // Turn all the LEDs on:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

    delay(delayTime);               

  }

  // Turn all the LEDs off:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }          

 

 

In the main void loop function, we use delay time as a function by the code intdelayTime = 100; where 100 in the millisecond to pause between LEDs. You can change it for making smaller for faster switching.

For turn all the LEDs on:

This for() loop will step index from 0 to 7, (putting "++" after a variable means add one to it) and will then use digitalWrite() to turn that LED on.

for(index = 0; index <= 7; index++)

For turn all the LEDs off:

This for() loop will step index from 7 to 0 (putting "--" after a variable means subtract one from it) and will then use digitalWrite() to turn that LED off.

for(index = 0; index <= 7; index++)

 

Now code become following

 

**********************************************************************

//Code 3

// initialize the pins and tell which pin are connected to which LED

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

 }

}

// the loop routine runs over and over again forever:

void loop()

{

int index;

int delayTime = 100; // delay time

  // Turn all the LEDs on:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

delay(delayTime);               

  }

  // Turn all the LEDs off:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], LOW);

delay(delayTime);

  }              

}

**********************************************************************

 

Let's see what happen…………

 

Now change the sequence of led off mode.

 

From

To

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }              

for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }              

 

index--   Putting "--" after a variable means "subtract one to it".

  (You can also use "index = index - 1".)

 

Now code become following

***************************************************************

//code 4

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

}

}

 

// the loop routine runs over and over again forever:

void loop()

{

int index;

int delayTime = 100; // delay time

  // Turn all the LEDs on:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

delay(delayTime);               

  }

  // Turn all the LEDs off:

for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

delay(delayTime);

  }                             

}

***************************************************************

Let's see what happen…………

 

 

These led blinking differently by changing some code in the upper code 4 in the void loop()

·         Light up all the LEDs in turn--------------------oneAfterAnotherLoop()

·         Turn on one LED at a time----------------------oneOnAtATime();

·         Light the LEDs middle to the edges-------------pingPong();

·         Chase lights like you see on signs--------------marquee();

·         Blink LEDs randomly-----------------------------randomLED();

 

Now we check Light up all the LEDs in turn by twisting some code

 

From

To

void loop()

{

int index;

int delayTime = 100; //delay time

  // Turn all the LEDs on:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

    delay(delayTime);               

  }

  // Turn all the LEDs off:

for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }                             

}

 

void loop()

{

oneAfterAnotherLoop();  // Light up all the LEDs in turn

}

 

void oneAfterAnotherLoop()

{

int index;

int delayTime = 100; // delay time

  // Turn all the LEDs on:

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

    delay(delayTime);               

  }  

  // Turn all the LEDs off:

  for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }              

}

 

Now code become following

**********************************************************************

// code 5

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

 

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

 

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

}

}

 

// the loop routine runs over and over again forever:

void loop()

{

oneAfterAnotherLoop();  // Light up all the LEDs in turn

}

 

void oneAfterAnotherLoop()

{

int index;

int delayTime = 100; // delay time

  // Turn all the LEDs on:

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

delay(delayTime);               

  }                                 

 

  // Turn all the LEDs off:

for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

delay(delayTime);

  }              

}

 

**********************************************************************

Let's see what happen…………


Turn on one LED at a time scrolling down

 

Change the code

 

From

To

void loop()

{

oneAfterAnotherLoop();  // Light up all the LEDs in turn

}

 

void oneAfterAnotherLoop()

{

int index;

int delayTime = 100; // delay time

 

  // Turn all the LEDs on:

 

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);

    delay(delayTime);               

  }                                 

 

  // Turn all the LEDs off:

  for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], LOW);

    delay(delayTime);

  }              

}

void loop()

{

oneOnAtATime();  //Turn on one LED at a time scrolling down

}

 

void oneOnAtATime()

{

int index;

int delayTime = 100; // delay time

 

  // step through the LEDs, from 0 to 7

 

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

 

oneOnAtATime() this the function  will step through the LEDs, lighting only one at a time.

 

Now code become following

**********************************************************************

// code 6

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

 

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

 

 }

}

 

// the loop routine runs over and over again forever:

void loop()

{

oneOnAtATime();  //Turn on one LED at a time scrolling down

}

 

void oneOnAtATime()

{

int index;

int delayTime = 100; // delay time

  // step through the LEDs, from 0 to 7

 

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

**********************************************************************

Let's see what happen…………


Light the LEDs middle to the edges

 

Change the code

 

From

To

void loop()

{

oneOnAtATime();  //Turn on one LED at a time scrolling down

}

void oneOnAtATime()

{

int index;

int delayTime = 100; // delay time

 

  // step through the LEDs, from 0 to 7

 

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

void loop()

{

pingPong();             // Light the LEDs middle to the edges

}

void pingPong()

{

int index;

int delayTime = 100; // delay time

 

  // step through the LEDs, from 0 to 7

 

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

 

  // step through the LEDs, from 7 to 0

 

  for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delaytime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

 

 

in the loop function we define pingpong() is the function for the light the LEDs middle to the edges. This function will step through the LEDs, lighting one at a time in both directions.

 

Now code become following

**********************************************************************

// Code 7

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

 

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

}

}

 

// the loop routine runs over and over again forever:

void loop()

{

pingPong();             // Light the LEDs middle to the edges

}

voidpingPong()

{

int index;

int delayTime = 100; // delay time

 

  // step through the LEDs, from 0 to 7

 

for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

 

  // step through the LEDs, from 7 to 0

 

for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

**********************************************************************

Let's see what happen………. 

Chase lights like you see on signs

 

Change the code

 

From

To

void loop()

{

pingPong();             // Light the LEDs middle to the edges

}

 

void pingPong()

{

int index;

int delayTime = 100; // delay time

 

  // step through the LEDs, from 0 to 7

 

  for(index = 0; index <= 7; index++)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

 

  // step through the LEDs, from 7 to 0

 

  for(index = 7; index >= 0; index--)

  {

digitalWrite(ledPin[index], HIGH);  // turn LED on

    delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

  }

}

 

void loop()

{

marquee();              // Chase lights like you see on signs

}

 

void marquee()

{

int index;

int delayTime = 200; // delay time

 

  for(index = 0; index <= 3; index++) // Step from 0 to 3

  {

digitalWrite(ledPin[index], HIGH);    // Turn a LED on

digitalWrite(ledPin[index+4], HIGH);  // Skip four, and turn that LED on

    delay(delayTime);                      // Pause to slow down the sequence

digitalWrite(ledPin[index], LOW);     // Turn the LED off

digitalWrite(ledPin[index+4], LOW);   // Skip four, and turn that LED off

  }

}

 

marquee() This function will mimic "chase lights" like those around signs. In this idea, we divide it by 2 sections of 4 LEDs.

Step through the first four LEDs. (We'll light up one in the lower 4 and one in the upper 4)


Code Become Following

******************************************************************************************

//Code 8

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

}

}

 

// the loop routine runs over and over again forever:

void loop()

{

marquee();              // Chase lights like you see on signs

}

void marquee()

{

int index;

int delayTime = 200; // milliseconds to pause between LEDs

                       // Make this smaller for faster switching

 

  // Step through the first four LEDs

  // (We'll light up one in the lower 4 and one in the upper 4)

 

for(index = 0; index <= 3; index++) // Step from 0 to 3

  {

digitalWrite(ledPin[index], HIGH);    // Turn a LED on

digitalWrite(ledPin[index+4], HIGH);  // Skip four, and turn that LED on

delay(delayTime);                      // Pause to slow down the sequence

digitalWrite(ledPin[index], LOW);     // Turn the LED off

digitalWrite(ledPin[index+4], LOW);   // Skip four, and turn that LED off

  }

}

**********************************************************************

Let's see what happen………. 


Blink LEDs randomly

 

Change the code following

 

From

To

void loop()

{

marquee();              // Chase lights like you see on signs

}

 

void marquee()

{

int index;

int delayTime = 200; // milliseconds to pause between LEDs

                       // Make this smaller for faster switching

 

  // Step through the first four LEDs

  // (We'll light up one in the lower 4 and one in the upper 4)

 

  for(index = 0; index <= 3; index++) // Step from 0 to 3

  {

digitalWrite(ledPin[index], HIGH);    // Turn a LED on

digitalWrite(ledPin[index+4], HIGH);  // Skip four, and turn that LED on

    delay(delayTime);                      // Pause to slow down the sequence

digitalWrite(ledPin[index], LOW);     // Turn the LED off

digitalWrite(ledPin[index+4], LOW);   // Skip four, and turn that LED off

  }

}

void loop()

{

randomLED();            // Blink LEDs randomly

}

 

void randomLED()

{

int index;

int delayTime;

 

  // The random() function will return a semi-random number each

  // time it is called.

 

  index = random(8);    // pick a random number between 0 and 7

delayTime = 100;

 

digitalWrite(ledPin[index], HIGH);  // turn LED on

  delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

}

 

randomLED() This function will turn on random LEDs. Can you modify it so it also lights them for random times?

 

 

Now code become following

**********************************************************************

//Code 9

// initialize the pins and tell which pin are connected to which LED (int led no = arduino pin no)

int ledPin[] = {2,3,4,5,6,7,8,9};

// Setup the 8 pins as OUTPUT

void setup()

{

int index;

for(index = 0; index <= 7; index++)

  {

pinMode(ledPin[index],OUTPUT);

 }

}

 

// the loop routine runs over and over again forever:

void loop()

{

randomLED();            // Blink LEDs randomly

}

 

voidrandomLED()

{

int index;

int delayTime;

 

  // The random() function will return a semi-random number each

  // time it is called.

 

index = random(8);    // pick a random number between 0 and 7

delayTime = 100;

 

digitalWrite(ledPin[index], HIGH);  // turn LED on

delay(delayTime);                    // pause to slow down

digitalWrite(ledPin[index], LOW);   // turn LED off

}

**********************************************************************

Let's see what happen……….

 

Enjoy the playing code of Arduino for controlling the blinking of the sequence of multiple LEDs.