From b7c9fe551bc862d7b6ba235bd8bac08acbb3e039 Mon Sep 17 00:00:00 2001 From: eaman Date: Mon, 6 Feb 2017 03:39:56 +0100 Subject: [PATCH] Multitasking --- basic/blinks/blink_0/blink_0.ino | 2 +- .../blink_1_variabili/blink_1_variabili.ino | 17 ++++++++++++++--- .../BlinkWithoutDelay_1.ino | 12 ++++++++++-- .../BlinkWithoutDelay_2_led.ino | 12 ++++++------ .../BlinkWithoutDelay_2_led_cleanup.ino | 12 ++++++------ .../BlinkWithoutDelay_3_funzione.ino | 18 ++++++------------ .../BlinkWithoutDelay_4_argomento.ino | 18 ++++++------------ .../BlinkWithoutDelay_5_cleanup.ino | 8 ++++---- .../BlinkWithoutDelay_6_1_interrupt.ino | 4 ++-- .../BlinkWithoutDelay_6_class.ino | 4 ++-- .../BlinkWithoutDelay_7_struct.ino | 4 ++-- .../BlinkWithoutDelay_8_struct_pointer.ino | 4 ++-- 12 files changed, 61 insertions(+), 54 deletions(-) diff --git a/basic/blinks/blink_0/blink_0.ino b/basic/blinks/blink_0/blink_0.ino index f26f96d..3998d68 100644 --- a/basic/blinks/blink_0/blink_0.ino +++ b/basic/blinks/blink_0/blink_0.ino @@ -25,7 +25,7 @@ void setup() { // loop: Le istruzioni vengono eseguite all'infinito void loop() { digitalWrite(led, HIGH); // Mette il PIN del LED in stato acceso - delay(1000); // Aspetta un secondo (mille millisecondi) + delay(200); // Aspetta un secondo (mille millisecondi) digitalWrite(led, LOW); // Mette il PIN del LED in stato spento delay(500); // Aspetta mezzo secondo } diff --git a/basic/blinks/blink_1_variabili/blink_1_variabili.ino b/basic/blinks/blink_1_variabili/blink_1_variabili.ino index 86a1171..9a4ed5b 100644 --- a/basic/blinks/blink_1_variabili/blink_1_variabili.ino +++ b/basic/blinks/blink_1_variabili/blink_1_variabili.ino @@ -13,8 +13,8 @@ // Pin 13 ha un LED preconfigurato su molte schede Arduino int led = 13; -int breve = 200; // Variabile richiambile nel corso dell'esecuzione -int lunga = 1000; +int breve = 100; // Variabile richiambile nel corso dell'esecuzione +int lunga = 400; // ///////////////// // Setup: eseguita una volta sola all'accensione della scheda @@ -27,10 +27,21 @@ void setup() { // loop: Le istruzioni vengono eseguite all'infinito void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(breve +40); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(breve +40); // wait for a second + + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(breve); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(breve); // wait for a second - + + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(breve -40) + ; // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(breve -40); // wait for a second + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(lunga); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW diff --git a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino index b7a04a9..408fd16 100644 --- a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino +++ b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -56,9 +56,11 @@ void loop() // the LED is bigger than the interval at which you want to // blink the LED. - if (millis() > previousMillis + interval) { + if (millis() >= previousMillis + interval) { // Aggiorniamo il contatore previousMillis - previousMillis = millis(); + previousMillis += interval ; + // previousMillis = millis(); // 3) Cosa succederebbe se fosse + // passato piu' di 1ms dall'evento all'azione? // if the LED is off turn it on and vice-versa: if (ledState == LOW) @@ -79,4 +81,10 @@ void loop() dovranno aggiungere. 2. E' ora agevole cambiare gli intervalli dei due LED? Modificare gli intervalli dei due led (es 500ms - 320ms) + + + + Risposta + 3. Si sarebbe introdotto uno slip (ritardo) nei tempi dello sketch + */ diff --git a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino index 2100ead..fb411e9 100644 --- a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino +++ b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -14,7 +14,7 @@ by David A. Mellis modified 8 Feb 2010 by Paul Stoffregen - modified by eaman + modified by Andrea Manni This example code is in the public domain. @@ -48,9 +48,9 @@ void setup() { void loop() { // Primo LED - if (millis() > previousMillisA + intervalA) { - // save the last time you blinked the LED - previousMillisA = millis(); + if (millis() >= previousMillisA + intervalA) { + // Aggiornimo il riferimento temporale + previousMillisA += intervalA; // if the LED is off turn it on and vice-versa: if (ledStateA == LOW) @@ -62,9 +62,9 @@ void loop() } // Secondo LED - if (millis() > previousMillisB + intervalB) { + if (millis() >= previousMillisB + intervalB) { // save the last time you blinked the LED - previousMillisB = millis(); + previousMillisB += intervalB; // if the LED is off turn it on and vice-versa: if (ledStateB == LOW) diff --git a/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino b/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino index 21c26eb..7f338a9 100644 --- a/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino +++ b/multitasking/BlinkWithoutDelay_2_led_cleanup/BlinkWithoutDelay_2_led_cleanup.ino @@ -14,7 +14,7 @@ by David A. Mellis modified 8 Feb 2010 by Paul Stoffregen - modified by eaman + modified by Andrea Manni This example code is in the public domain. @@ -48,9 +48,9 @@ void setup() { void loop() { // Primo LED - if (millis() - previousMillisA > intervalA) { - // save the last time you blinked the LED - previousMillisA = millis(); + if (millis() - previousMillisA >= intervalA) { + // Timestamp + timestamp = delta temporale + previousMillisA += intervalA ; // if the LED is off turn it on and vice-versa: ledStateA = !ledStateA; @@ -59,9 +59,9 @@ void loop() } // Secondo LED: contratta - if (millis() - previousMillisB > intervalB) { + if (millis() - previousMillisB >= intervalB) { digitalWrite(ledB, !digitalRead(ledB)); - previousMillisB = millis(); + previousMillisB += intervalB ; } } diff --git a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino index a77faca..db0ed89 100644 --- a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino +++ b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino @@ -49,15 +49,12 @@ void loop() // Funzioni: void lightLedA () { - if (millis() > previousMillisA + intervalA) { + if (millis() - previousMillisA >= intervalA) { // save the last time you blinked the LED - previousMillisA = millis(); + previousMillisA += intervalA; // if the LED is off turn it on and vice-versa: - if (ledStateA == LOW) - ledStateA = HIGH; - else - ledStateA = LOW; + ledStateA = !ledStateA ; // set the LED with the ledState of the variable: digitalWrite(ledA, ledStateA); } @@ -67,15 +64,12 @@ void lightLedA () { void lightLedB () { long intervalB = 500; static int ledStateB ; // https://www.arduino.cc/en/Reference/Static - if (millis() > previousMillisB + intervalB) { + if (millis() - previousMillisB >= intervalB) { // save the last time you blinked the LED - previousMillisB = millis(); + previousMillisB += intervalB ; // if the LED is off turn it on and vice-versa: - if (ledStateB == LOW) - ledStateB = HIGH; - else - ledStateB = LOW; + ledStateB = !ledStateB; // set the LED with the ledState of the variable: digitalWrite(ledB, ledStateB); } diff --git a/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino index e2b28a9..11c7338 100644 --- a/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino +++ b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino @@ -34,15 +34,12 @@ 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(); + previousMillisA += interval; // if the LED is off turn it on and vice-versa: - if (ledStateA == LOW) - ledStateA = HIGH; - else - ledStateA = LOW; + ledStateA = !ledStateA; // set the LED with the ledState of the variable: digitalWrite(ledA, ledStateA); } @@ -52,15 +49,12 @@ void lightLedA (int interval) { void lightLedB (int interval) { // Illumina il ledB secondo un intervallo passato come argomento - if (millis() > previousMillisB + interval) { + if (millis() - previousMillisB >= interval) { // save the last time you blinked the LED - previousMillisB = millis(); + previousMillisB += interval; // if the LED is off turn it on and vice-versa: - if (ledStateB == LOW) - ledStateB = HIGH; - else - ledStateB = LOW; + ledStateB = !ledStateB; // set the LED with the ledState of the variable: digitalWrite(ledB, ledStateB); } diff --git a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino index bce667d..b0ba86e 100644 --- a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino +++ b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -38,9 +38,9 @@ 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(); + previousMillisA += interval; // if the LED is off turn it on and vice-versa: ledStateA = !ledStateA ; // Inverti il LED @@ -51,8 +51,8 @@ void lightLedA (int interval) { void lightLedB (int interval) { // Illumina il ledB secondo un intervallo passato come argomento - if (millis() - previousMillisB > interval) { - previousMillisB = millis(); + if (millis() - previousMillisB >= interval) { + previousMillisB += interval; 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. diff --git a/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino b/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino index 175f705..fd77d6c 100644 --- a/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino +++ b/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino @@ -28,9 +28,9 @@ public: void Update () { // Illumina il ledB secondo un intervallo passato come argomento - if (millis() > previousMillis + interval) { + if (millis() - previousMillis >= interval) { // save the last time you blinked the LED - previousMillis = millis(); + previousMillis += interval; // if the LED is off turn it on and vice-versa: ledState = !ledState ; // Inverti il LED diff --git a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino index 9ce5e9b..628d112 100644 --- a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino +++ b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino @@ -28,9 +28,9 @@ public: void Update () { // Illumina il ledB secondo un intervallo passato come argomento - if (millis() > previousMillis + interval) { + if (millis() - previousMillis >= interval) { // save the last time you blinked the LED - previousMillis = millis(); + previousMillis += interval; // if the LED is off turn it on and vice-versa: ledState = !ledState ; // Inverti il LED diff --git a/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino b/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino index e726662..8b55527 100644 --- a/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino +++ b/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino @@ -37,9 +37,9 @@ void loop() struct blinkLed lightLed(struct blinkLed temp) { // dataType tipo_di_struct nome_funzione(argomenti) // Illumina il ledA secondo un intervallo passato come argomento - if (millis() > temp.previousMillis + temp.interval) { // gli elementi dello struct sono accessibili tramite l'operatore [punto] + if (millis() - temp.previousMillis >= temp.interval) { // gli elementi dello struct sono accessibili tramite l'operatore [punto] // save the last time you blinked the LED - temp.previousMillis = millis(); + temp.previousMillis += temp.interval ; // if the LED is off turn it on and vice-versa: temp.ledState = !temp.ledState ; // Inverti il LED diff --git a/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino b/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino index f038199..d5cd783 100644 --- a/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino +++ b/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino @@ -43,8 +43,8 @@ void loop() void lightLed(struct blinkLed *temp) { // temp ora e' un pointer e non una struttura autonoma: pass by reference (not by value) // Illumina il ledA secondo un intervallo passato come argomento - if(millis() - (*temp).previousMillis > (*temp).interval) { // l'operatore punto ha priorita' maggiore rispetto al pointer asterisco - (*temp).previousMillis = millis(); + if(millis() - (*temp).previousMillis >= (*temp).interval) { // l'operatore punto ha priorita' maggiore rispetto al pointer asterisco + (*temp).previousMillis += (*temp).interval ; // if the LED is off turn it on and vice-versa: temp->ledState = !temp->ledState ; // Forma contratta, deference operator: temp->ledState == (*temp).ledState -- 2.39.2