X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=multitasking%2FBlinkWithoutDelay_5_cleanup%2FBlinkWithoutDelay_5_cleanup.ino;h=bce667de1a3015e2aee024cbe6f07f23656ba20e;hb=81656cead477f7c23b904e2e2ec07bfe645bace2;hp=17fe931f5eeae757503abbeeee6877e0dcddefd0;hpb=f2798a8e8a34fac133b9f37b620af585ee33ba10;p=sketchbook_andrea diff --git a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino index 17fe931..bce667d 100644 --- a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino +++ b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -1,21 +1,24 @@ /* Blink without Delay - Pulizia -Semplificato il ciclo condizionale +Semplificato il ciclo condizionale, la seconda funzione non necessita +di una variabile di stato per tracciare il LED. + */ // constants won't change. Used here to // set pin numbers: // First LED -int ledA = 13; // the number of the LED pin +const int ledA = 13; // the number of the LED pin // Variables will change: int ledStateA = LOW; // ledState used to set the LED long previousMillisA = 0; // will store last time LED was updated // Second LED data -int ledB = 12; //Secondo LED -int ledStateB = LOW; // ledState used to set the LED -long previousMillisB = 0; // will store last time LED was updated +const int ledB = 12; //Secondo LED +// int ledStateB = LOW; // Possiamo leggere lo stato del registro del LED + // con digitalRead() +unsigned long previousMillisB = 0; // millis() ritorna sempre un unsigned long void setup() { // set the digital pin as output: @@ -35,7 +38,7 @@ void loop() void lightLedA (int interval) { // Illumina il ledA secondo un intervallo passato come argomento - if(millis() - previousMillisA > interval) { + if (millis() > previousMillisA + interval) { // save the last time you blinked the LED previousMillisA = millis(); @@ -48,14 +51,12 @@ void lightLedA (int interval) { void lightLedB (int interval) { // Illumina il ledB secondo un intervallo passato come argomento - if(millis() - previousMillisB > interval) { - // save the last time you blinked the LED - previousMillisB = millis(); - - // if the LED is off turn it on and vice-versa: - ledStateB = !ledStateB ; // Inverti il LED + if (millis() - previousMillisB > interval) { + previousMillisB = millis(); + digitalWrite(ledB, !digitalRead(ledB)); + // Leggiamo direttamente il registro di ledB e scriviamo il suo opposto, + // questo ci permette di non dover avere una variabile per tracciare lo stato. } - digitalWrite(ledB, ledStateB); } /* Domande: 1. E' possibile avere una sola funzione che permetta di gestire