Monday 2 May 2016

Arduino Toturial # 5 - Driving Mulitiple LED's


Introduction


In our earlier lesson we discuss about lighting a LED with arduino and today we are going to drive multiple LED by using Arduino.  We will connect 8 LED's to arduino and control then with 8 output pins.



Parts list for this experiment


1x Breadboard
1x Arduino Uno
8x LEDs
8x 330Ω Resistors
Jumper Wires



Let's connect the parts on breadboard.


Connect the components as shown in the figure.





Positive side of each LED should connect to resistor and other side of the resister should connect to Arduino pin 2,3,4,5,6,7,8 and 9 respectively. All the negatives should be grounded.




Let write the Sketch


We are going to use an "array to keep a track of the LED pins. An Array will let us to store a group of variables and we can refer them by their position or "index".
We are creating an array of eight integers, and initializing them to a set of values.


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


The first element of an array is index 0. And we have put the value "2" to it, "3" in index 1, etc. and the final index in the above array is 7, in this case it will contain the value "9". Since we are connecting Arduino pin 2 to 9 pins we have use the same in our array.

We are going to to use "for() loops" inside the setup function. we can use For()loops to get numbers to count up or down.  here we are stepping variables from  one value another and it will perform a set of instructions for each step.


for()loop has three statements which are separated by semicolons (;).
  •   Something to do before starting
  •   A test to perform; as long as it's true, keep looping
  •   Something to do after each loop (increase a variable)


we are using below three statements,
  1. index = 0;       Before starting, make index = 0.
  2. index <= 7;  →  If 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".)

Every time we go through the loop, the statements following the for() (within the brackets) will run.When the test in statement 2 is false, the sketch will continue.

Here instead of writing 8 separate statements, by using for()loop we will initialize all the LED pins as outputs.
  
The following 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.


void setup()
{
  int index;


  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
    // ledPins[index] is replaced by the value in the array.
    // For example, ledPins[0] is 2
  }
}



Following  loop() calls functions that are written further below. Some of them are disabled by commenting them out (putting"//" in front of them). You can try different LED displays by remove the "//" in front of the ones you'd like to run, and add "//" in front of those you don't to comment out (and disable) those lines.


void loop()
{


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

  //oneAfterAnotherLoop();  // Same as oneAfterAnotherNoLoop, but with much less typing

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

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

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

 // randomLED();            // Blink LEDs randomly
}



oneAfterAnotherNoLoop()

This will light one LED, delay for delayTime, then light the next LED, and repeat until all the LEDs are on. It will then turn them off in the reverse order.

And we have  not used a for() loop. This is the hard way and using for() loop is much easir. We have used for() loop for the next function (oneAfterAnotherLoop() )which is  further down.


void oneAfterAnotherNoLoop()
{
  int delayTime = 100; // time (milliseconds) to pause between LEDs
                       // make this smaller for faster switching

  // turn all the LEDs on:

  digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (pin 2)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (pin 3)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (pin 4)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (pin 5)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (pin 6)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[5], HIGH);  //Turns on LED #5 (pin 7)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[6], HIGH);  //Turns on LED #6 (pin 8)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[7], HIGH);  //Turns on LED #7 (pin 9)
  delay(delayTime);                //wait delayTime milliseconds  

  // turn all the LEDs off:

  digitalWrite(ledPins[7], LOW);   //Turn off LED #7 (pin 9)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[6], LOW);   //Turn off LED #6 (pin 8)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[5], LOW);   //Turn off LED #5 (pin 7)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[4], LOW);   //Turn off LED #4 (pin 6)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[3], LOW);   //Turn off LED #3 (pin 5)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[2], LOW);   //Turn off LED #2 (pin 4)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[1], LOW);   //Turn off LED #1 (pin 3)
  delay(delayTime);                //wait delayTime milliseconds
  digitalWrite(ledPins[0], LOW);   //Turn off LED #0 (pin 2)
  delay(delayTime);                //wait delayTime milliseconds  
}



oneAfterAnotherNoLoop()

This function does exactly the same thing as oneAfterAnotherNoLoop() by using for()loop and the array to do it with much less typing.


void oneAfterAnotherLoop()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // 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++)
  {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);                
  }                                  

  // Turn all the LEDs off:

  /*This for() loop will step index from 7 to 0n(putting "--" after a variable means subtract one from it) and will then use digitalWrite() to turn that LED off.
/*
  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], LOW);
    delay(delayTime);
  }               
}



oneOnAtATime()

Following  function will step through the LEDs, lighting only one at at time.


void oneOnAtATime()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs make this smaller for                                          // faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
}



pingPong()

This function will step through the LEDs lighting one at at time in both directions.



void pingPong()
{
  int index;
  int delayTime = 100; // milliseconds to pause between LEDs
                       // make this smaller for faster switching

  // step through the LEDs, from 0 to 7

  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }

  // step through the LEDs, from 7 to 0

  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], HIGH);  // turn LED on
    delay(delayTime);                    // pause to slow down
    digitalWrite(ledPins[index], LOW);   // turn LED off
  }
}



marquee()

Following will mimic "chase lights" like those around 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
  // ( 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(ledPins[index], HIGH);    // Turn a LED on
    digitalWrite(ledPins[index+4], HIGH);  // Skip four, and turn that LED on
    delay(delayTime);                      // Pause to slow down the sequence
    digitalWrite(ledPins[index], LOW);     // Turn the LED off
    digitalWrite(ledPins[index+4], LOW);   // Skip four, and turn that LED off
  }
}



randomLED()

This function will turn on random LEDs. Can you modify it so it also lights them for random times?   The random() function will return a semi-random number each time it is called. 

See :- http://arduino.cc/en/Reference/Random for tips on how to make random() even more random.



void randomLED()
{
  int index;
  int delayTime;


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

  digitalWrite(ledPins[index], HIGH);  // turn LED on
  delay(delayTime);                    // pause to slow down
  digitalWrite(ledPins[index], LOW);   // turn LED off
}



That is all about the sketch. You can download the sketch from here - Download


No comments:

Post a Comment