X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=blobdiff_plain;f=programming%2Fstructured_data_types%2Farray_loop%2Farray_loop.ino;fp=programming%2Fstructured_data_types%2Farray_loop%2Farray_loop.ino;h=002650881e38b2eda86d2ae652831767a38c36dd;hp=0000000000000000000000000000000000000000;hb=389f454b12f2c526fbc707d6b4903e7dfb8f5620;hpb=487c31ee83fb81e856593ae272d2d76a2a5c1a78 diff --git a/programming/structured_data_types/array_loop/array_loop.ino b/programming/structured_data_types/array_loop/array_loop.ino new file mode 100644 index 0000000..0026508 --- /dev/null +++ b/programming/structured_data_types/array_loop/array_loop.ino @@ -0,0 +1,77 @@ +/* Knight Rider 2 + * -------------- + * + * Array e uso dei cicli iterativi. + * + + + 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 timer = 100; + +void setup() { + // we make all the declarations at once + for (int count = 0; count < 9; count++) { + pinMode(pinArray[count], OUTPUT); + } +} + +void loop() { + for (int count = 0; count < 8; count++) { // 8 e' un numero magico + digitalWrite(pinArray[count], HIGH); + delay(timer); + digitalWrite(pinArray[count], LOW); + delay(timer); + } + + // Ciclo inverso: dall'alto in basso + for (int 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 +*/ + +