]> git.piffa.net Git - sketchbook_andrea/blob - programming/loops/loop_2_array_loop_serial/loop_2_array_loop_serial.ino
Loop e data type strutturati
[sketchbook_andrea] / programming / loops / loop_2_array_loop_serial / loop_2_array_loop_serial.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  http://www.arduino.cc/en/Tutorial/ForLoop
15  */
16
17 byte ledPins[8] = {  // Domanda: cosa succede se uso int?
18   2, 3, 4, 5, 6, 7, 8, 9
19   } ; //Array
20 int timer = 100;           // Pausa per far brillare i LED
21
22 void setup() {
23   Serial.begin(9600);
24   // use a for loop to initialize each pin as an output:
25   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++)  {
26     pinMode(ledPins[thisPin], OUTPUT);
27     Serial.print("Inizializzato pin n. ");
28     Serial.println(thisPin);
29   }
30
31   Serial.print("Dimesione array: ");
32   Serial.println(sizeof(ledPins));
33 }
34
35 void loop() {
36   // loop from the lowest pin to the highest:
37   for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) {
38     Serial.print("Accensione pin n. ");
39     Serial.println(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     // Debug
46
47   }
48
49   // loop from the highest pin to the lowest:
50   for (int thisPin = sizeof(ledPins) - 1 ; thisPin > 0; thisPin--) {
51     Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0
52     Serial.println(thisPin);
53     digitalWrite(ledPins[thisPin], HIGH);
54     delay(timer);
55     digitalWrite(ledPins[thisPin], LOW);
56
57   }
58 }