Arduino Starter’s Guide  (2/7): Controlling Multiple LEDs

We tried flashing one LED, now it’s time to try not flashing multiple LEDs simultaneously, but with different light sequences. This circuit is also a nice setup to experiment with writing your own programs and to get a feel for how Arduino works.

Along with controlling the LEDs we start looking into a few simple programming methods to keep your programs small.

Hardware Used:

  • 5 – LED

  • 5 – 100 ohm Resistor
  • 
    
  • 1 – breadboard
  • 
    
  • Arduino Uno
  • 
    
    

    You can buy all this Hardware at Createlabz.

    Software Used:

    Arduino IDE

    Set up the Hardware:

    Code:

    /*     ---------------------------------------------------------
     *     |  Arduino Experimentation Kit Example Code             |
     *     |  CIRC-02 .: 5 LED Fun :. (Multiple LEDs)   |
     *     ---------------------------------------------------------
     *  
     *  A few Simple LED animations
     *
     * For more information on this circuit http://tinyurl.com/d2hrud
     *
     */
     
    //LED Pin Variables
    int ledPins[] = {2,3,4,5,6}; //An array to hold the pin each LED is connected to
                                       //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
                                       //to address an array use ledPins[0] this would equal 2
                                       
     
    /*
     * setup() - this function runs once when you turn your Arduino on
     * We the three control pins to outputs
     */
    void setup()
    {
      
      //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
      for(int i = 0; i < 8; i++){         //this is a loop and will repeat eight times
          pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
      }                                   //the code this replaces is below
     
      /* (commented code will not run)
       * these are the lines replaced by the for loop above they do exactly the
       * same thing the one above just uses less typing
      pinMode(ledPins[0],OUTPUT);
      pinMode(ledPins[1],OUTPUT);
      pinMode(ledPins[2],OUTPUT);
      pinMode(ledPins[3],OUTPUT);
      pinMode(ledPins[4],OUTPUT);
    
    }
     
     
    /*
     * loop() - this function will start after setup finishes and then repeat
     * we call a function called oneAfterAnother(). if you would like a different behaviour
     * uncomment (delete the two slashes) one of the other lines
     */
    void loop()                     // run over and over again
    {
      oneAfterAnotherNoLoop();   //this will turn on each LED one by one then turn each off
      //oneAfterAnotherLoop();   //does the same as oneAfterAnotherNoLoop but with 
                                 //much less typing
      //oneOnAtATime();          //this will turn one LED on then turn the next one
                                 //on turning the 
                                 //former off (one LED will look like it is scrolling 
                                 //along the line
      //pingPong();
      //randomLED();
    }
     
    /*
     * oneAfterAnotherNoLoop() - Will light one LED then delay for delayTime then light
     * the next LED until all LEDs are on it will then turn them off one after another
     *
     * this does it without using a loop which makes for a lot of typing. 
     * oneOnAtATimeLoop() does exactly the same thing with less typing
     */
    void oneAfterAnotherNoLoop(){
      int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                           //make smaller for quicker switching and larger for slower
      digitalWrite(ledPins[0], HIGH);  //Turns on LED #0 (connected to pin 2 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[1], HIGH);  //Turns on LED #1 (connected to pin 3 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[2], HIGH);  //Turns on LED #2 (connected to pin 4 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[3], HIGH);  //Turns on LED #3 (connected to pin 5 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[4], HIGH);  //Turns on LED #4 (connected to pin 6 )
      delay(delayTime);                //waits delayTime milliseconds
    
     
    //Turns Each LED Off
      
      digitalWrite(ledPins[4], LOW);  //Turns on LED #3 (connected to pin 5 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[3], LOW);  //Turns on LED #4 (connected to pin 6 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[2], LOW);  //Turns on LED #5 (connected to pin 7 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[1], LOW);  //Turns on LED #6 (connected to pin 8 )
      delay(delayTime);                //waits delayTime milliseconds
      digitalWrite(ledPins[0], LOW);  //Turns on LED #7 (connected to pin 9 )
      delay(delayTime);                //waits delayTime milliseconds  
    }
     
    /*
     * oneAfterAnotherLoop() - Will light one LED then delay for delayTime then light
     * the next LED until all LEDs are on it will then turn them off one after another
     *
     * this does it using a loop which makes for a lot less typing. 
     * than oneOnAtATimeNoLoop() does exactly the same thing with less typing
     */
    void oneAfterAnotherLoop(){
      int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                           //make smaller for quicker switching and larger for slower
     
    //Turn Each LED on one after another
      for(int i = 0; i <= 5; i++){
        digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
        delay(delayTime);                //gets one added to it so this will repeat 
      }                                  //8 times the first time i will = 0 the final
                                         //time i will equal 7;
     
    //Turn Each LED off one after another
      for(int i = 5; i >= 0; i--){  //same as above but rather than starting at 0 and counting up
                                    //we start at seven and count down
        digitalWrite(ledPins[i], LOW);  //Turns off LED #i each time this runs i
        delay(delayTime);                //gets one subtracted from it so this will repeat 
      }                                  //8 times the first time i will = 7 the final
                                         //time it will equal 0
                                         
                                         
    }
     
    /*
     * oneOnAtATime() - Will light one LED then the next turning off all the others
     */
    void oneOnAtATime(){
      int delayTime = 100; //the time (in milliseconds) to pause between LEDs
                           //make smaller for quicker switching and larger for slower
      
      for(int i = 0; i <= 5; i++){
        int offLED = i - 1;  //Calculate which LED was turned on last time through
        if(i == 0) {         //for i = 1 to 7 this is i minus 1 (i.e. if i = 2 we will
          offLED = 5;        //turn on LED 2 and off LED 1)
        }                    //however if i = 0 we don't want to turn of led -1 (doesn't exist)
                             //instead we turn off LED 7, (looping around)
        digitalWrite(ledPins[i], HIGH);     //turn on LED #i
        digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time
        delay(delayTime);
      }
    }
     
    /*
     * inAndOut() - This will turn on the two middle LEDs then the next two out
     * making an in and out look
     */
    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 <= 5; 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 = 5; index >= 0; index--)
      {
        digitalWrite(ledPins[index], HIGH);  // turn LED on
        delay(delayTime);                    // pause to slow down
        digitalWrite(ledPins[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?
    */
    
    void randomLED()
    {
      int index;
      int delayTime;
    
      // 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.
    
      index = random(5);    // 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
    }

    Code Breakdown:

    oneAfterAnotherNoLoop();   
      //oneAfterAnotherLoop();   
      //oneOnAtATime();          
      //pingPong();
      //randomLED();

    The code shown above is a function.

    https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/

    Other codes that was shown here were explained in the blinking LED and if you want to know more about functions just click the link. https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/

     

    Try MORE!!!!!

    Extra animations:

    Tired of one animation? Try the other sample animations. Just remove the // and upload the program to your arduino and enjoy the new light! (note: In the loop, 3 must have the // so that it would work!)

    Random
    OneAtATime
    PingPong

     

    The post Arduino Starter’s Guide (2/7): Controlling Multiple LEDs appeared first on CreateLabz.

    ArduinoBlinkFunctionKnowledgebaseLedMultiple ledsOneatatimePingpongStarter kit

    Leave a comment

    All comments are moderated before being published