New Media Arduino Array Practice from SlyPork on Vimeo.
Below is my code for the array formation shown in the video above. This setup utilizes ten light emitting diodes, five green and five red. Pins 4 through 13 are used and for color coordination, five green jumpers are linked to the green LEDs and five red jumpers are linked to the red LEDs. In between the LEDs and jumpers is one 220 Ohm resistor for each pin and one white jumper links all of the LEDs to ground.
int timer = 100;
int ledPins[] = {
4,5,6,7,8,9,10,11,12,13
};
int pinCount = 10;
int count = 5;
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
//declaring functions
void allOff() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], LOW);
}
}
void allOn() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
}
}
void greenOff() {
for (int thisPin = pinCount – 1; thisPin >= 1; thisPin-=2) {
digitalWrite(ledPins[thisPin], LOW);
}
}
void greenOn() {
for (int thisPin = pinCount – 1; thisPin >= 1; thisPin-=2) {
digitalWrite(ledPins[thisPin], HIGH);
}
}
void togglegreen() {
for (int thisPin = pinCount – 1; thisPin >= 1; thisPin-=2) {
digitalWrite(ledPins[thisPin], !digitalRead(ledPins[thisPin]));
}
}
void togglered() {
for (int thisPin = 0; thisPin < pinCount; thisPin+=2) {
digitalWrite(ledPins[thisPin], !digitalRead(ledPins[thisPin]));
}
}
//utilizing functions
void loop() {
//light LEDs from 0 to 9
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
//light LEDs from 9 to 0
for (int thisPin = pinCount – 1; thisPin >= 0; thisPin–) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
//light red LEDs from high to low pins
for (int thisPin = pinCount – 1; thisPin >= 1; thisPin-=2) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer+timer);
digitalWrite(ledPins[thisPin], LOW);
delay(timer+timer);
}
//light green LEDs from low to high pins
for (int thisPin = 0; thisPin < pinCount; thisPin+=2) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer+timer);
digitalWrite(ledPins[thisPin], LOW);
delay(timer+timer);
}
//alternate green and red LEDs
allOn();
delay(timer+timer);
while(count>0) {
togglegreen();
delay(timer+timer);
togglered();
delay(timer+timer);
count = count -1;
}
allOff();
count = 5;
}