| 1 |
/* |
| 2 |
For Loop Iteration |
| 3 |
|
| 4 |
Demonstrates the use of a for() loop. |
| 5 |
Lights multiple LEDs in sequence, then in reverse. |
| 6 |
|
| 7 |
The circuit: |
| 8 |
- LEDs from pins 2 through 7 to ground |
| 9 |
|
| 10 |
created 2006 |
| 11 |
by David A. Mellis |
| 12 |
modified 30 Aug 2011 |
| 13 |
by Tom Igoe |
| 14 |
|
| 15 |
This example code is in the public domain. |
| 16 |
|
| 17 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/ForLoopIteration |
| 18 |
*/ |
| 19 |
|
| 20 |
int timer = 100; // The higher the number, the slower the timing. |
| 21 |
|
| 22 |
void setup() { |
| 23 |
// use a for loop to initialize each pin as an output: |
| 24 |
for (int thisPin = 2; thisPin < 8; thisPin++) { |
| 25 |
pinMode(thisPin, OUTPUT); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
void loop() { |
| 30 |
// loop from the lowest pin to the highest: |
| 31 |
for (int thisPin = 2; thisPin < 8; thisPin++) { |
| 32 |
// turn the pin on: |
| 33 |
digitalWrite(thisPin, HIGH); |
| 34 |
delay(timer); |
| 35 |
// turn the pin off: |
| 36 |
digitalWrite(thisPin, LOW); |
| 37 |
} |
| 38 |
|
| 39 |
// loop from the highest pin to the lowest: |
| 40 |
for (int thisPin = 7; thisPin >= 2; thisPin--) { |
| 41 |
// turn the pin on: |
| 42 |
digitalWrite(thisPin, HIGH); |
| 43 |
delay(timer); |
| 44 |
// turn the pin off: |
| 45 |
digitalWrite(thisPin, LOW); |
| 46 |
} |
| 47 |
} |