From: Andrea Manni Date: Wed, 29 Apr 2015 23:45:27 +0000 (+0200) Subject: cleanup multitasking X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=929cf678b6e52050584cb43055194830edc5523f cleanup multitasking --- diff --git a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino index bbed9de..605947d 100644 --- a/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino +++ b/multitasking/BlinkWithoutDelay_1/BlinkWithoutDelay_1.ino @@ -14,6 +14,7 @@ by David A. Mellis modified 8 Feb 2010 by Paul Stoffregen + modified by eaman This example code is in the public domain. @@ -46,11 +47,10 @@ void loop() // 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. - unsigned long currentMillis = millis(); - if(currentMillis - previousMillis > interval) { + if(millis() - previousMillis > interval) { // save the last time you blinked the LED - previousMillis = currentMillis; + previousMillis = millis(); // if the LED is off turn it on and vice-versa: if (ledState == LOW) @@ -65,5 +65,5 @@ void loop() /* Domande 1. Aggioungere un LED che brilli ogni 500ms - 2. E' possibile cambiare gli intervalli dei due LED? + 2. E' ora agevole cambiare gli intervalli dei due LED? */ diff --git a/multitasking/BlinkWithoutDelay_2_1_led_funzione/BlinkWithoutDelay_2_1_led_funzione.ino b/multitasking/BlinkWithoutDelay_2_1_led_funzione/BlinkWithoutDelay_2_1_led_funzione.ino deleted file mode 100644 index 7b812ee..0000000 --- a/multitasking/BlinkWithoutDelay_2_1_led_funzione/BlinkWithoutDelay_2_1_led_funzione.ino +++ /dev/null @@ -1,88 +0,0 @@ -/* Blink without Delay - - Soluzione - - 3. Provare a isolare il codice per accendere ogni singolo led in una funzione: - Quali variabili determinano il comportamento del LED? - Sono globali o locali? - - */ - -// constants won't change. Used here to -// set pin numbers: - -// First LED -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 - -// 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) - -// Second LED -int ledB = 12; //Secondo LED -int ledStateB = LOW; // ledState used to set the LED -long previousMillisB = 0; // will store last time LED was updated -long intervalB = 500; // interval at which to blink (milliseconds) - - -void setup() { - // set the digital pin as output: - pinMode(ledA, OUTPUT); - pinMode(ledB, OUTPUT); -} - -void loop() -{ - lightLedA(); - lightLedB(); -} - - -// Funzioni - -void lightLedA () { - if(millis() - previousMillisA > intervalA) { - // save the last time you blinked the LED - previousMillisA = millis(); - - // if the LED is off turn it on and vice-versa: - if (ledStateA == LOW) - ledStateA = HIGH; - else - ledStateA = LOW; - // set the LED with the ledState of the variable: - digitalWrite(ledA, ledStateA); - } - -} - -void lightLedB () { - if(millis() - previousMillisB > intervalB) { - // save the last time you blinked the LED - previousMillisB = millis(); - - // if the LED is off turn it on and vice-versa: - if (ledStateB == LOW) - ledStateB = HIGH; - else - ledStateB = LOW; - // set the LED with the ledState of the variable: - digitalWrite(ledB, ledStateB); - } -} - - -/* Domande - 2. Inserire un secondo LED con intervallo 500ms - 1. Trasformare il codice utilizzato in una State Machine - */ - - - - - diff --git a/multitasking/BlinkWithoutDelay_2_2_led_funzione_argomento/BlinkWithoutDelay_2_2_led_funzione_argomento.ino b/multitasking/BlinkWithoutDelay_2_2_led_funzione_argomento/BlinkWithoutDelay_2_2_led_funzione_argomento.ino deleted file mode 100644 index 7685471..0000000 --- a/multitasking/BlinkWithoutDelay_2_2_led_funzione_argomento/BlinkWithoutDelay_2_2_led_funzione_argomento.ino +++ /dev/null @@ -1,92 +0,0 @@ -/* Blink without Delay - Soluzione - - Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio - - */ - -// constants won't change. Used here to -// set pin numbers: - -// First LED -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 - -// 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. - - -// 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 - - - -void setup() { - // set the digital pin as output: - pinMode(ledA, OUTPUT); - pinMode(ledB, OUTPUT); -} - -void loop() -{ - lightLedA(40); - lightLedB(500); -} - - -// Funzioni - -void lightLedA (int interval) { - // Illumina il ledA secondo un intervallo passato come argomento - - if(millis() - previousMillisA > interval) { - // save the last time you blinked the LED - previousMillisA = millis(); - - // if the LED is off turn it on and vice-versa: - if (ledStateA == LOW) - ledStateA = HIGH; - else - ledStateA = LOW; - // set the LED with the ledState of the variable: - digitalWrite(ledA, ledStateA); - } - -} - -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: - if (ledStateB == LOW) - ledStateB = HIGH; - else - ledStateB = LOW; - // set the LED with the ledState of the variable: - digitalWrite(ledB, ledStateB); - } -} - -/* TODO -- Differenze tra fuznioni e programmazione a oggetti: integrazione tra -operativita' e dati -- Rapporto tra global scope e uso di pointers -- uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma -*/ - - - - - - - diff --git a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino index fa2c35c..293e217 100644 --- a/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino +++ b/multitasking/BlinkWithoutDelay_2_led/BlinkWithoutDelay_2_led.ino @@ -1,4 +1,4 @@ -/* Blink without Delay +/* Blink without Delay - due led Turns on and off a light emitting diode(LED) connected to a digital pin, without using the delay() function. This means that other code @@ -14,6 +14,7 @@ by David A. Mellis modified 8 Feb 2010 by Paul Stoffregen + modified by eaman This example code is in the public domain. @@ -52,11 +53,10 @@ void loop() // 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. - unsigned long currentMillis = millis(); - if(currentMillis - previousMillisA > intervalA) { + if(millis() - previousMillisA > intervalA) { // save the last time you blinked the LED - previousMillisA = currentMillis; + previousMillisA = millis(); // if the LED is off turn it on and vice-versa: if (ledStateA == LOW) @@ -67,9 +67,9 @@ void loop() digitalWrite(ledA, ledStateA); } - if(currentMillis - previousMillisB > intervalB) { + if(millis() - previousMillisB > intervalB) { // save the last time you blinked the LED - previousMillisB = currentMillis; + previousMillisB = millis(); // if the LED is off turn it on and vice-versa: if (ledStateB == LOW) @@ -82,9 +82,9 @@ void loop() } /* Domande - 2. Inserire un secondo LED con intervallo 500ms - 1. Trasformare il codice utilizzato in una State Machine - + 1. Provare a isolare il codice per accendere ogni singolo led in una funzione: + Quali variabili determinano il comportamento del LED? + Sono globali o locali? */ diff --git a/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino new file mode 100644 index 0000000..886dd5e --- /dev/null +++ b/multitasking/BlinkWithoutDelay_3_funzione/BlinkWithoutDelay_3_funzione.ino @@ -0,0 +1,88 @@ +/* Blink without Delay + + Soluzione + + 3. Provare a isolare il codice per accendere ogni singolo led in una funzione: + Quali variabili determinano il comportamento del LED? + Sono globali o locali? + + */ + +// constants won't change. Used here to +// set pin numbers: + +///////////// +// First LED +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 +// 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) + +////////////// +// Second LED +int ledB = 12; //Secondo LED +int ledStateB = LOW; // ledState used to set the LED +long previousMillisB = 0; // will store last time LED was updated +long intervalB = 500; // interval at which to blink (milliseconds) + + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(); + lightLedB(); +} + + +// Funzioni + +void lightLedA () { + if(millis() - previousMillisA > intervalA) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateA == LOW) + ledStateA = HIGH; + else + ledStateA = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +} + +void lightLedB () { + if(millis() - previousMillisB > intervalB) { + // save the last time you blinked the LED + previousMillisB = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateB == LOW) + ledStateB = HIGH; + else + ledStateB = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledB, ledStateB); + } +} + + +/* Domande + 1. Modificare le funzioni in modo che accettino come parametro + l'intervallo di lampeggio. + + */ + + + + + diff --git a/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino new file mode 100644 index 0000000..a6ecbaf --- /dev/null +++ b/multitasking/BlinkWithoutDelay_4_argomento/BlinkWithoutDelay_4_argomento.ino @@ -0,0 +1,83 @@ +/* Blink without Delay + Soluzione + + Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio + + */ + +// First LED +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 + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(1000); + lightLedB(500); +} + +////////////// +// Funzioni + +void lightLedA (int interval) { + // Illumina il ledA secondo un intervallo passato come argomento + + if(millis() - previousMillisA > interval) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + if (ledStateA == LOW) + ledStateA = HIGH; + else + ledStateA = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledA, ledStateA); + } + +} + +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: + if (ledStateB == LOW) + ledStateB = HIGH; + else + ledStateB = LOW; + // set the LED with the ledState of the variable: + digitalWrite(ledB, ledStateB); + } +} + +/* Approfondimenti +- Quali similitudini ci sono tra le due funzioni? +- Come si dovrebbe fare per semplificare il codice + evitando di ripetere larti del codice simile tra loro? +- Distinguere i dati comuni tra le due funzioni che ci servono per + far lampeggiare i LED +- Distinguere i dati che caratterizzano un LED rispetto all'altro +*/ + + + + + + + diff --git a/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino new file mode 100644 index 0000000..cc3d0e0 --- /dev/null +++ b/multitasking/BlinkWithoutDelay_5_cleanup/BlinkWithoutDelay_5_cleanup.ino @@ -0,0 +1,76 @@ +/* Blink without Delay - Pulizia + +Semplificato il ciclo condizionale + */ + +// constants won't change. Used here to +// set pin numbers: + +// First LED +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 + +void setup() { + // set the digital pin as output: + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() +{ + lightLedA(1000); + lightLedB(500); +} + +//////////////// +// Funzioni + +void lightLedA (int interval) { + // Illumina il ledA secondo un intervallo passato come argomento + + if(millis() - previousMillisA > interval) { + // save the last time you blinked the LED + previousMillisA = millis(); + + // if the LED is off turn it on and vice-versa: + ledStateA = !ledStateA ; // Inverti il LED + } + digitalWrite(ledA, ledStateA); +} + +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 + } + digitalWrite(ledB, ledStateB); +} + +/* Approfondimenti + - integrazione tra funzioni e dati + - Rapporto tra global scope e uso di pointers + - Uso di forme di dati strutturate (array, struct) per scambiare dati tra funzioni e programma + */ + + + + + + + + + + + diff --git a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino new file mode 100644 index 0000000..6734b14 --- /dev/null +++ b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino @@ -0,0 +1,56 @@ +/* Blink without Delay + Soluzione + + Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio + */ + +class Lampeggiatore { + // Lampeggia un LED utilizzando millis() + // Variabili + 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 + + // Constructor: come viene instanziato un oggetto facente parte della classe +public: + Lampeggiatore(int pin, long time) + { + ledPin = pin; + pinMode(ledPin, OUTPUT); + ledState = LOW; + previousMillis = 0; + interval = time; + } + +// Una funzione facente parte di una classe prende il nome di "metodo" della stessa: + void Update () { + // Illumina il ledB secondo un intervallo passato come argomento + + if(millis() - previousMillis > interval) { + // save the last time you blinked the LED + previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + ledState = !ledState ; // Inverti il LED + } + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + } + +}; + +// Instanziamo i due led dalla classe +Lampeggiatore ledA(13, 1000); +Lampeggiatore ledB(12, 500); + +void setup() { +} + +void loop() +{ +ledA.Update(); +ledB.Update(); +} + + diff --git a/multitasking/blink_0_soluzione/blink_0_soluzione.ino b/multitasking/blink_0_soluzione/blink_0_soluzione.ino index b1ba27a..265390e 100644 --- a/multitasking/blink_0_soluzione/blink_0_soluzione.ino +++ b/multitasking/blink_0_soluzione/blink_0_soluzione.ino @@ -1,8 +1,6 @@ /* - Blink - Turns on an LED on for one second, then off for one second, repeatedly. - - This example code is in the public domain. + Blink due LED - Soluzione + */ // Pin 13 has an LED connected on most Arduino boards. @@ -39,10 +37,6 @@ void loop() { ; } -/* Domande - 1. Aggiungere un secondo LED e farlo brillare ogni 500ms - mentre il primo brilla ogni 1000ms - */ diff --git a/multitasking/state_3_one_led/state_3_one_led.ino b/multitasking/state_3_one_led/state_3_one_led.ino deleted file mode 100644 index 1b5717b..0000000 --- a/multitasking/state_3_one_led/state_3_one_led.ino +++ /dev/null @@ -1,46 +0,0 @@ -/* Blink con state machine - - Accendere e spegnere un led utilizzando una state machine -*/ - -// These variables store the flash pattern -// and the current state of the LED - -int ledPin = 13; // the number of the LED pin -int ledState = LOW; // ledState used to set the LED -unsigned long previousMillis = 0; // will store last time LED was updated -long interval = 1000; // - -void setup() -{ - // set the digital pin as output: - pinMode(ledPin, OUTPUT); -} - -void loop() -{ - // check to see if it's time to change the state of the LED - unsigned long currentMillis = millis(); - if((ledState == HIGH) && (currentMillis - previousMillis >= interval)) - { - ledState = !ledState ; // Inverti il LED - previousMillis = currentMillis; // Remember the time - digitalWrite(ledPin, ledState); // Update the actual LED - - } - else if((ledState == LOW) && (currentMillis - previousMillis >= interval)) - { - ledState = !ledState ; // Inverti il LED - previousMillis = currentMillis; // Remember the time - digitalWrite(ledPin, ledState); // Update the actual LED - } - - -} - -/* -1. Modificare il codice in modo che si possa precisare il tempo di HIGH - e il tempo di LOW -2. Aggiungere un LED che brilli ogni 500ms - */ - diff --git a/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino b/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino deleted file mode 100644 index 1f696cb..0000000 --- a/multitasking/state_4_one_led_two_timers/state_4_one_led_two_timers.ino +++ /dev/null @@ -1,49 +0,0 @@ -/* Blink con state machine - - Accendere e spegnere un led utilizzando una state machine - specificando sia il tempo accensione che di spegnimento. - -*/ - -// These variables store the flash pattern -// and the current state of the LED - -int ledPin = 13; // the number of the LED pin -int ledState = LOW; // ledState used to set the LED -unsigned long previousMillis = 0; // will store last time LED was updated -long intervalHigh = 1000; // -long intervalLow = 200; // - -void setup() -{ - // set the digital pin as output: - pinMode(ledPin, OUTPUT); -} - -void loop() -{ - // check to see if it's time to change the state of the LED - unsigned long currentMillis = millis(); - if((ledState == HIGH) && (currentMillis - previousMillis >= intervalHigh)) - { - ledState = !ledState ; // Inverti il LED - previousMillis = currentMillis; // Remember the time - digitalWrite(ledPin, ledState); // Update the actual LED - - } - else if((ledState == LOW) && (currentMillis - previousMillis >= intervalLow)) - { - ledState = !ledState ; // Inverti il LED - previousMillis = currentMillis; // Remember the time - digitalWrite(ledPin, ledState); // Update the actual LED - } - - -} - -/* -1. Modificare il codice in modo che si possa precisare il tempo di HIGH - e il tempo di LOW -2. Aggiungere un LED che brilli ogni 500ms - */ - diff --git a/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino b/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino deleted file mode 100644 index 174f875..0000000 --- a/multitasking/state_5_two_led_two_timers/state_5_two_led_two_timers.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* Blink con state machine: due led - - Accendere e spegnere due led utilizzando una state machine - specificando sia il tempo accensione che di spegnimento. - - */ - -// These variables store the flash pattern -// and the current state of the LED - -int ledPinA = 13; // the number of the LED pin -int ledStateA = LOW; // ledState used to set the LED -unsigned long previousMillisA = 0; // will store last time LED was updated -long intervalHighA = 1000; // -long intervalLowA = 200; // - -int ledPinB = 12; // the number of the LED pin -int ledStateB = LOW; // ledState used to set the LED -unsigned long previousMillisB = 0; // will store last time LED was updated -long intervalHighB = 500; // -long intervalLowB = 100; // - -void setup() -{ - // set the digital pin as output: - pinMode(ledPinA, OUTPUT); - pinMode(ledPinB, OUTPUT); -} - -void loop() -{ - // Primo LED - // check to see if it's time to change the state of the LED - unsigned long currentMillis = millis(); - if((ledStateA == HIGH) && (currentMillis - previousMillisA >= intervalHighA)) - { - ledStateA = 1 - ledStateA ; // Inverti il LED - previousMillisA = currentMillis; // Remember the time - digitalWrite(ledPinA, ledStateA); // Update the actual LED - - } - else if((ledStateA == LOW) && (currentMillis - previousMillisA >= intervalLowA)) - { - ledStateA = 1 - ledStateA ; // Inverti il LED - previousMillisA = currentMillis; // Remember the time - digitalWrite(ledPinA, ledStateA); // Update the actual LED - } - -// Secondo LED - - // check to see if it's time to change the state of the LED - unsigned long currentMillis = millis(); - if((ledStateB == HIGH) && (currentMillis - previousMillisB >= intervalHighB)) - { - ledStateB = 1 - ledStateB ; // Inverti il LED - previousMillisB = currentMillis; // Remember the time - digitalWrite(ledPinB, ledStateB); // Update the actual LED - - } - else if((ledStateB == LOW) && (currentMillis - previousMillisB >= intervalLowB)) - { - ledStateB = 1 - ledStateB ; // Inverti il LED - previousMillisB = currentMillis; // Remember the time - digitalWrite(ledPinA, ledStateB); // Update the actual LED - } -} - -/* Domande - 1. E' possibile razzinalizzare il codice utilizzando una funzione? - - - Link: - - Utilizzare codice a oggetti per ottimizzare il codice: - https://learn.adafruit.com/multi-tasking-the-arduino-part-1?view=all#a-classy-solution - */ - -