]> git.piffa.net Git - sketchbook_andrea/blob - programming/loops/loop_3_multi_led/loop_3_multi_led.ino
for loop
[sketchbook_andrea] / programming / loops / loop_3_multi_led / loop_3_multi_led.ino
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 9 to ground
9  
10  Schemi:
11  - http://lab.piffa.net/schemi/8_led_single_res_bb.png
12  - http://lab.piffa.net/schemi/8_led_single_res_schem.png
13  
14  
15  http://www.arduino.cc/en/Tutorial/ForLoop
16  */
17
18 int timer = 100;           // The higher the number, the slower the timing.
19
20 void setup() {
21   // use a for loop to initialize each pin as an output:
22   for (int thisPin = 2; thisPin <= 9; thisPin++)  {
23     pinMode(thisPin, OUTPUT);      
24   }
25 }
26
27 void loop() {
28   // loop from the lowest pin to the highest:
29   for (int thisPin = 2; thisPin <= 9; thisPin++) {
30     // turn the pin on:
31     digitalWrite(thisPin, HIGH);  
32     delay(timer);                  
33     // turn the pin off:
34     digitalWrite(thisPin, LOW);    
35   }
36
37   // loop from the highest pin to the lowest:
38   for (int thisPin = 9; thisPin >= 2; thisPin--) {
39     // turn the pin on:
40     digitalWrite(thisPin, HIGH);
41     delay(timer);
42     // turn the pin off:
43     digitalWrite(thisPin, LOW);
44   }
45 }