]> git.piffa.net Git - sketchbook_andrea/blob - programming/structured_data_types/array_loop/array_loop.ino
Loop e data type strutturati
[sketchbook_andrea] / programming / structured_data_types / array_loop / array_loop.ino
1 /* Knight Rider 2
2  * --------------
3  *
4  * Array e uso dei cicli iterativi.
5  *
6
7
8    Schema semplificato:
9    - http://lab.piffa.net/schemi/8_led_single_res_bb.png
10    - http://lab.piffa.net/schemi/8_led_single_res_schem.png
11  */
12
13 int pinArray[8] = {2, 3, 4, 5, 6, 7, 8, 9};
14 int timer = 100;
15
16 void setup() {
17   // we make all the declarations at once
18   for (int count = 0; count < 9; count++) {
19     pinMode(pinArray[count], OUTPUT);
20   }
21 }
22
23 void loop() {
24   for (int count = 0; count < 8; count++) { // 8 e' un numero magico
25     digitalWrite(pinArray[count], HIGH);
26     delay(timer);
27     digitalWrite(pinArray[count], LOW);
28     delay(timer);
29   }
30
31   // Ciclo inverso: dall'alto in basso
32   for (int count = 8; count >= 0; count--) {
33     digitalWrite(pinArray[count], HIGH);
34     delay(timer);
35     digitalWrite(pinArray[count], LOW);
36     delay(timer);
37   }
38 }
39
40 /* Domande:
41
42  1. Come posso fare per saltare un elemento del loop?
43  2. Come posso fare per uscire completamente dal loop?
44  3. 8 e' un numero magico: come posso evitarlo?
45
46 .
47 .
48 .
49 .
50 .
51 .
52 .
53 .
54 .
55 .
56 .
57 .
58 .
59 .
60 .
61 .
62 .
63 .
64 .
65 .
66 .
67 Soluzioni:
68  1. utilizzare continue
69  2. utilizzare break
70  3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il
71     valore tramite la funzione sizeof().
72 Links:
73 - http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm
74 - https://www.arduino.cc/en/Reference/Sizeof
75 */
76
77