X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=multitasking%2FBlinkWithoutDelay_5_cleanup%2FBlinkWithoutDelay_5_cleanup.ino;h=2a74c5c6364d1a3e302b629a85cda3763e71c405;hb=17b4fe57f20ce15396a69ef7fdc87a7342ba0cb7;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..2a74c5c 100644 --- a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino +++ b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -1,20 +1,23 @@ /* 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 +const int ledB = 12; //Secondo LED +// int ledStateB = LOW; // Possiamo leggere lo stato del registro del LED + // con digitalRead() long previousMillisB = 0; // will store last time LED was updated void setup() { @@ -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