X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=programming%2Floops%2Floop_1_array_loop%2Floop_1_array_loop.ino;fp=programming%2Floops%2Floop_1_array_loop%2Floop_1_array_loop.ino;h=42871ef279b1c2192f11272cb45d3e393fc105f2;hb=7eb899c40c1fc5f52e0f9c016e6f638f51fe14ff;hp=0000000000000000000000000000000000000000;hpb=3af536aeadb354f5a63959633f0833b3e53721be;p=sketchbook_andrea diff --git a/programming/loops/loop_1_array_loop/loop_1_array_loop.ino b/programming/loops/loop_1_array_loop/loop_1_array_loop.ino new file mode 100644 index 0000000..42871ef --- /dev/null +++ b/programming/loops/loop_1_array_loop/loop_1_array_loop.ino @@ -0,0 +1,70 @@ +/* Knight Rider 2 + * -------------- + * + * Reducing the amount of code using for(;;). + * + * + * (cleft) 2005 K3, Malmo University + * @author: David Cuartielles + * @hardware: David Cuartielles, Aaron Hallborg + + + Schema semplificato: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + */ + +int pinArray[8] = {2, 3, 4, 5, 6, 7, 8, 9}; +int count = 0; +int timer = 100; + +void setup(){ + // we make all the declarations at once + for (count=0;count<9;count++) { + pinMode(pinArray[count], OUTPUT); + } +} + +void loop() { + for (count=0;count<8;count++) { // 8 e' un numero magico + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } + for (count=8;count>=0;count--) { + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } +} + +/* Domande: + + 1. Come posso fare per saltare un elemento del loop? + 2. Come posso fare per uscire completamente dal loop? + 3. 8 e' un numero magico: come posso evitarlo? + +. +. +. +. +. +. +. +. +. +. +. +Soluzioni: + 1. utilizzare continue + 2. utilizzare break + 3. Utilizzare un variabile sarebbe gia' un inizio, ancora meglio estrarre il + valore tramite la funzione sizeof(). +Links: +- http://www.tutorialspoint.com/cprogramming/c_continue_statement.htm +- https://www.arduino.cc/en/Reference/Sizeof +*/ + +