Here are some examples using the DIY LED baord we discussed earlier at http://www.getmicros.net/diy-led-board.php
Cycle all LEDs
const int Led1 = 2;
const int Led2 = 3;
const int Led3 = 4;
const int Led4 = 5;
const int Led5 = 6;
const int Led6 = 7;
const int Led7 = 8;
const int Led8 = 9;
void setup()
{
// set pins as outputs
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 loop()
{
// flash each of the LEDs for 1 second
blinkLED(Led1, 100);
blinkLED(Led2, 100);
blinkLED(Led3, 100);
blinkLED(Led4, 100);
blinkLED(Led5, 100);
blinkLED(Led6, 100);
blinkLED(Led7, 100);
blinkLED(Led8, 100);
}
void blinkLED(int pin, int duration)
{
digitalWrite(pin, HIGH); // turn LED on
delay(duration);
digitalWrite(pin, LOW); // turn LED off
delay(duration);
}
Cycle All LEDs on and off (better way)
// choose the pin for each of the LEDs
const int NumberLeds = 8;
const int LedPins[] = {2,3,4,5,6,7,8,9};
void setup()
{
// set pins as outputs
for (int led = 0; led < NumberLeds; led++)
{
pinMode(LedPins[led], OUTPUT);
}
}
void loop()
{
// flash each of the LEDs on then off
for (int led = 0; led <= NumberLeds; led++)
{
digitalWrite(LedPins[led], HIGH);
delay(500);
digitalWrite(LedPins[led], LOW);
delay(500);
}
}
LEDS on then off
// choose the pin for each of the LEDs
const int NumberLeds = 8;
const int LedPins[] = {2,3,4,5,6,7,8,9};
void setup()
{
// set pins as outputs
for (int led = 0; led < NumberLeds; led++)
{
pinMode(LedPins[led], OUTPUT);
}
}
void loop()
{
// flash each of the LEDs on then off
for (int led = 0; led < NumberLeds; led++)
{
digitalWrite(LedPins[led], HIGH);
delay(500);
}
for (int led = NumberLeds; led > 0; led–)
{
digitalWrite(LedPins[led], LOW);
delay(500);
}
}