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