From: Andrea Manni Date: Mon, 30 Mar 2015 10:54:50 +0000 (+0200) Subject: for loop X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=8f9853f962b96d5b01e1db7cef031783c851c8dc for loop --- diff --git a/README b/README index e432432..6601373 100644 --- a/README +++ b/README @@ -108,6 +108,12 @@ basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino +For loop +========== + +sketchbook_andrea/programming/loops/ +Random e interruzione del flusso del programma tramite if + Status ========== diff --git a/basic/buttons/button_state_3/button_state_3.ino b/basic/buttons/button_state_3/button_state_3.ino index 83b274f..7eee7e4 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -6,6 +6,8 @@ */ int switchPin = 2; // switch connesso al pin 2 + // Nota: le prossime due variabili sono + // solo "dichiarate" e non "definite" int statoAttuale; // Variabile per leggere lo stato del bottone int ultimoStato; // Variabile per registrare l'ultimo stato del bottone @@ -19,8 +21,9 @@ void setup() { void loop(){ statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e lo resistra in val - delay(20) // riduce l'effetto bounce - if (statoAttuale != ultimoStato) { // lo stato del bottone e' cambiato + delay(20); // riduce l'effetto bounce + if (statoAttuale != ultimoStato) { + // verifica due condizioni che devono realizzarsi contemporaneamente if (statoAttuale == HIGH) { // il bottone e' stato premuto Serial.println("Bottone premuto"); } diff --git a/programming/loops/loop_3_multi_led/loop_3_multi_led.ino b/programming/loops/loop_3_multi_led/loop_3_multi_led.ino new file mode 100644 index 0000000..e736eb0 --- /dev/null +++ b/programming/loops/loop_3_multi_led/loop_3_multi_led.ino @@ -0,0 +1,45 @@ +/* + For Loop Iteration + + Demonstrates the use of a for() loop. + Lights multiple LEDs in sequence, then in reverse. + + The circuit: + * LEDs from pins 2 through 9 to ground + + Schemi: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + + + http://www.arduino.cc/en/Tutorial/ForLoop + */ + +int timer = 100; // The higher the number, the slower the timing. + +void setup() { + // use a for loop to initialize each pin as an output: + for (int thisPin = 2; thisPin <= 9; thisPin++) { + pinMode(thisPin, OUTPUT); + } +} + +void loop() { + // loop from the lowest pin to the highest: + for (int thisPin = 2; thisPin <= 9; thisPin++) { + // turn the pin on: + digitalWrite(thisPin, HIGH); + delay(timer); + // turn the pin off: + digitalWrite(thisPin, LOW); + } + + // loop from the highest pin to the lowest: + for (int thisPin = 9; thisPin >= 2; thisPin--) { + // turn the pin on: + digitalWrite(thisPin, HIGH); + delay(timer); + // turn the pin off: + digitalWrite(thisPin, LOW); + } +} diff --git a/programming/loops/loop_4_multi_led_array/loop_4_multi_led_array.ino b/programming/loops/loop_4_multi_led_array/loop_4_multi_led_array.ino new file mode 100644 index 0000000..3b5ddef --- /dev/null +++ b/programming/loops/loop_4_multi_led_array/loop_4_multi_led_array.ino @@ -0,0 +1,63 @@ +/* + For Loop Iteration + + Demonstrates the use of a for() loop. + Lights multiple LEDs in sequence, then in reverse. + + The circuit: + * LEDs from pins 2 through 9 to ground + + Schemi: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + + http://www.arduino.cc/en/Tutorial/ForLoop + */ + +byte ledPins[8] = { // Domanda: cosa succede se uso int? + 2,3,4,5,6,7,8,9} +; //Array +int timer = 100; // Pausa per far brillare i LED + +void setup() { + Serial.begin(9600); + // use a for loop to initialize each pin as an output: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + pinMode(ledPins[thisPin], OUTPUT); + Serial.print("Inizializzato pin n. "); + Serial.println( thisPin); + } + + Serial.print("Dimesione array: "); + Serial.println(sizeof(ledPins)); +} + +void loop() { + // loop from the lowest pin to the highest: + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + Serial.print("Accensione pin n. "); + Serial.println(thisPin); + // turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(timer); + // turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + // Debug + + } + + // loop from the highest pin to the lowest: + for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) { + Serial.print("Accensione pin n. "); // Gli array sono indicizzati da 0 + Serial.println(thisPin); + // ><>> Rainbow! <<<"); + for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) { + // turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(timer / 2); + // turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + // Debug + + } + + // loop from the highest pin to the lowest: + for (int thisPin = sizeof(ledPins) -1 ; thisPin > 0; thisPin--) { + // >< Match found! Counter was at: "); // serial message Serial.println(count); digitalWrite(GREEN, HIGH); delay(WAIT); count++ ; + break; // Interrompe il ciclo } - //Serial.println(count); - count++ ; - delay(REST); } - + Serial.println(); Serial.println("Counter resetted."); // serial staff + count = 0; digitalWrite(RED, HIGH); delay(WAIT); count++ ; @@ -68,3 +70,4 @@ void loop() { // put your main code here, to run repeatedly: } } +