| 1 |
/* |
| 2 |
Arrays |
| 3 |
|
| 4 |
Demonstrates the use of an array to hold pin numbers in order to iterate over |
| 5 |
the pins in a sequence. Lights multiple LEDs in sequence, then in reverse. |
| 6 |
|
| 7 |
Unlike the For Loop tutorial, where the pins have to be contiguous, here the |
| 8 |
pins can be in any random order. |
| 9 |
|
| 10 |
The circuit: |
| 11 |
- LEDs from pins 2 through 7 to ground |
| 12 |
|
| 13 |
created 2006 |
| 14 |
by David A. Mellis |
| 15 |
modified 30 Aug 2011 |
| 16 |
by Tom Igoe |
| 17 |
|
| 18 |
This example code is in the public domain. |
| 19 |
|
| 20 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Arrays |
| 21 |
*/ |
| 22 |
|
| 23 |
int timer = 100; // The higher the number, the slower the timing. |
| 24 |
int ledPins[] = { |
| 25 |
2, 7, 4, 6, 5, 3 |
| 26 |
}; // an array of pin numbers to which LEDs are attached |
| 27 |
int pinCount = 6; // the number of pins (i.e. the length of the array) |
| 28 |
|
| 29 |
void setup() { |
| 30 |
// the array elements are numbered from 0 to (pinCount - 1). |
| 31 |
// use a for loop to initialize each pin as an output: |
| 32 |
for (int thisPin = 0; thisPin < pinCount; thisPin++) { |
| 33 |
pinMode(ledPins[thisPin], OUTPUT); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
void loop() { |
| 38 |
// loop from the lowest pin to the highest: |
| 39 |
for (int thisPin = 0; thisPin < pinCount; thisPin++) { |
| 40 |
// turn the pin on: |
| 41 |
digitalWrite(ledPins[thisPin], HIGH); |
| 42 |
delay(timer); |
| 43 |
// turn the pin off: |
| 44 |
digitalWrite(ledPins[thisPin], LOW); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
// loop from the highest pin to the lowest: |
| 49 |
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { |
| 50 |
// turn the pin on: |
| 51 |
digitalWrite(ledPins[thisPin], HIGH); |
| 52 |
delay(timer); |
| 53 |
// turn the pin off: |
| 54 |
digitalWrite(ledPins[thisPin], LOW); |
| 55 |
} |
| 56 |
} |