Saturday, April 26, 2014

Arduino Basics 4: Multiple LEDs


Objective:
Create various lighting sequences using multiple LEDs

Parts:
8 x LEDs
8 x 330 Ohm Resistor



Sketch:

Using Function "oneAfterAnontherLoop"

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

void setup()
{
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
  oneAfterAnotherLoop();
}

void oneAfterAnotherLoop()
{
  int index;
  int delayTime = 100;
              
  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH);
    delay(delayTime);                
  }                                  

  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], LOW);
    delay(delayTime);
  }               
}




 Using function "oneOnAtATime"

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

void setup()
{
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
 oneOnAtATime()
}

 void oneOnAtATime()
{
  int index;
  int delayTime = 100; 
  
  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH); 
    delay(delayTime);                    
    digitalWrite(ledPins[index], LOW);
  }
}


Using function "pingPong"

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

void setup()
{
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
  pingPong();
}

void pingPong()
{
  int index;
  int delayTime = 100;
  
  for(index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], HIGH); 
    delay(delayTime);                
    digitalWrite(ledPins[index], LOW);  
  }
  
  for(index = 7; index >= 0; index--)
  {
    digitalWrite(ledPins[index], HIGH); 
    delay(delayTime);                  
    digitalWrite(ledPins[index], LOW); 
  }
}


Using function "marquee"

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

void setup()
{
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
 marquee();
}

void marquee()
{
  int index;
  int delayTime = 200;
  
  for(index = 0; index <= 3; index++) 
  {
    digitalWrite(ledPins[index], HIGH);    
    digitalWrite(ledPins[index+4], HIGH);  
    delay(delayTime);                     
    digitalWrite(ledPins[index], LOW);    
    digitalWrite(ledPins[index+4], LOW);  
  }
}



Using function "randomLED"

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

void setup()
{
  int index;
  for(index = 0; index <= 7; index++)
  {
    pinMode(ledPins[index],OUTPUT);
  }
}

void loop()
{
  randomLED();
}

void randomLED()
{
  int index;
  int delayTime;
  
  index = random(8);
  delayTime = 100;

  digitalWrite(ledPins[index], HIGH); 
  delay(delayTime);                    
  digitalWrite(ledPins[index], LOW);  
}



Analysis:
The LEDs [2,3,4,5,6,7,8,9] shows different patterns depending on the function used in the sketch.

The first function, oneAfterAnotherLoop shows the LEDs turning on one by one with a delay of 100ms from turning on the 1st LED until the 7th LED. After that it turns off the 7th LED until the 1st LED with the same pattern and time.

The second function, oneOnAtATime turns on one LED for 100ms and immediately turns it off while turning on the next LED with the same pattern and time until the 7th LED.

The third function, pingPong is quite similar to the second function but this time it continues backwards turning on LEDs after the 7th LED which results in a forward backward motion.

The fourth function, marquee shows the 1st and 5th LED turning on for 200ms, continuing with the next LEDs, the 2nd and the 6th LED while the LEDs before were turned off. The sequence is continued until the 4th and 8th LED.


The fifth function, randomLED, turns on the LEDs for 100ms in a random sequence.




No comments:

Post a Comment