From: eaman Date: Mon, 20 Jun 2016 15:29:35 +0000 (+0200) Subject: Millis X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=29947de733ff92152bf348508efc3fd4f6acf767 Millis --- diff --git a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino index bdc5802..f7fee43 100644 --- a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino +++ b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -24,7 +24,7 @@ // constants won't change. Used here to // set pin numbers: -const int ledPin = 13; // the number of the LED pin +const int ledPin = 13; // Variables will change: int ledState = LOW; // ledState used to set the LED @@ -32,7 +32,7 @@ long previousMillis = 0; // will store last time LED was updated // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. -long interval = 1000; // interval at which to blink (milliseconds) +const long interval = 1000; // interval at which to blink (milliseconds) void setup() { // set the digital pin as output: @@ -49,7 +49,7 @@ void loop() // blink the LED. if(millis() - previousMillis > interval) { - // save the last time you blinked the LED + // Aggiorniamo il contatore previousMillis previousMillis = millis(); // if the LED is off turn it on and vice-versa: @@ -57,7 +57,7 @@ void loop() ledState = HIGH; else ledState = LOW; - // e' possibile semplificare queta operazione? + // e' possibile semplificare questa operazione? // Hint: lo stato del LED e' binario: ha solo due stati possibili. // set the LED with the ledState of the variable: diff --git a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino index 89f5793..0283339 100644 --- a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino +++ b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -24,20 +24,20 @@ // constants won't change. Used here to // set pin numbers: -const int ledA = 13; // the number of the LED pin -int ledB = 12; //Secondo LED +const int ledA = 13; // Primo LED +const int ledB = 12; // Secondo LED -// Variables will change: +// Variabbili di stato int ledStateA = LOW; // ledState used to set the LED int ledStateB = LOW; // ledState used to set the LED - + long previousMillisA = 0; // will store last time LED was updated long previousMillisB = 0; // will store last time LED was updated // the follow variables is a long because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long intervalA = 1000; // interval at which to blink (milliseconds) -long intervalB = 500; // interval at which to blink (milliseconds) +long intervalB = 500; // interval at which to blink (milliseconds) void setup() { // set the digital pin as output: @@ -47,14 +47,7 @@ void setup() { void loop() { - // here is where you'd put code that needs to be running all the time. - - // check to see if it's time to blink the LED; that is, if the - // difference between the current time and last time you blinked - // the LED is bigger than the interval at which you want to - // blink the LED. - -// First LED +// Primo LED if(millis() - previousMillisA > intervalA) { // save the last time you blinked the LED previousMillisA = millis(); @@ -68,7 +61,7 @@ void loop() digitalWrite(ledA, ledStateA); } -// Second LED +// Secondo LED if(millis() - previousMillisB > intervalB) { // save the last time you blinked the LED previousMillisB = millis(); diff --git a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino index a5db67b..93387bd 100644 --- a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino +++ b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino @@ -12,7 +12,7 @@ ///////////// // 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 @@ -22,8 +22,9 @@ long intervalA = 1000; // interval at which to blink (milliseconds) void lightLedA () ; ////////////// -// Second LED -int ledB = 12; //Secondo LED +// Second LED +// Now with less global variables thanks to static (see function body) +const int ledB = 12; //Secondo LED // ledState used to set the LED long previousMillisB = 0; // will store last time LED was updated // interval at which to blink (milliseconds) diff --git a/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino index 8810f27..ae2205a 100644 --- a/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino +++ b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino @@ -24,7 +24,7 @@ void setup() { void loop() { - lightLedA(1000); + lightLedA(333); lightLedB(500); } 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 diff --git a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino index 2744341..7a1f27e 100644 --- a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino +++ b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino @@ -11,7 +11,7 @@ class Lampeggiatore { int ledPin ; // il numero del LED pin int ledState ; // stato attuale del LED long interval ; // milliseconds di intervallo nel lampeggiare - long previousMillis ; //precedente cambio di stato + long previousMillis ; // precedente cambio di stato // Constructor: come viene instanziato un oggetto facente parte della classe public: diff --git a/multitasking/blink_0_soluzione/blink_0_soluzione.ino b/multitasking/blink_0_soluzione/blink_0_soluzione.ino index 25b4afd..53aec5d 100644 --- a/multitasking/blink_0_soluzione/blink_0_soluzione.ino +++ b/multitasking/blink_0_soluzione/blink_0_soluzione.ino @@ -4,6 +4,12 @@ Aggiungere un secondo LED e farlo brillare ogni 500ms mentre il primo brilla ogni 1000ms + Massimo comun denominatore 1000 MCD 500 = 500ms + Durata Periodo = 500ms + + + Stati: + a | b Changes ======== ========= 1 | 1 x | x @@ -11,7 +17,6 @@ 0 | 1 x | x 0 | 0 | x - Periodo = 500ms */ @@ -73,3 +78,4 @@ void loop() { +