]> git.piffa.net Git - sketchbook_andrea/blobdiff - multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino
Millis
[sketchbook_andrea] / multitasking / BlinkWithoutDelay_5_cleanup / BlinkWithoutDelay_5_cleanup.ino
index 17fe931f5eeae757503abbeeee6877e0dcddefd0..2a74c5c6364d1a3e302b629a85cda3763e71c405 100644 (file)
@@ -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