From: Andrea Manni Date: Wed, 29 Apr 2015 21:10:54 +0000 (+0200) Subject: multitask X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=b819e0775a4fdd866954d5365812294eb7bb5c32 multitask --- 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 new file mode 100644 index 0000000..7b812ee --- /dev/null +++ b/multitasking/BlinkWithoutDelay_2_1_led_funzione/BlinkWithoutDelay_2_1_led_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 +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 new file mode 100644 index 0000000..7685471 --- /dev/null +++ b/multitasking/BlinkWithoutDelay_2_2_led_funzione_argomento/BlinkWithoutDelay_2_2_led_funzione_argomento.ino @@ -0,0 +1,92 @@ +/* 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/programming/functions/scope_1/scope_1.ino b/programming/functions/scope_1/scope_1.ino index ae93e8f..9662283 100644 --- a/programming/functions/scope_1/scope_1.ino +++ b/programming/functions/scope_1/scope_1.ino @@ -1,3 +1,14 @@ +/* + Scope + + Variabili globali e locali rispetto a una funzione + Le variabili dichiarate al di fuori di funzioni + sono globali e accessibili a tutti, + + Le variabili dichiarate all'interno di una funzione + sono locali e accessibili solo a questa, + */ + boolean sposato = 1 ; void setup(){ @@ -13,14 +24,26 @@ Serial.println(inVacanza(sposato)); Serial.print("Tornato a casa sposato = "); Serial.println(sposato); + +// Global +Serial.print("In vacanza global mode sposato = "); +Serial.println(inVacanzaG()); + +Serial.print("Stato di sposato = "); +Serial.println(sposato); + +// Termine programma Serial.flush(); exit(0); } // Funzioni -boolean inVacanza(boolean sposato){ - sposato = 0 ; +boolean inVacanza(boolean sposato){ // variabile locale + sposato = 0 ; + return sposato; +} +boolean inVacanzaG(){ + sposato = 0 ; // Variabile globale return sposato; } - diff --git a/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino b/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino index 978b97d..2f28259 100644 --- a/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino +++ b/serial/debug_2_preprocessor_advanced/debug_2_preprocessor_advanced.ino @@ -22,7 +22,10 @@ int breve = 200; // Variabile richiambile nel corso dell'esecuzione #define DEBUG_PRINT(x) Serial.print (x) #define DEBUG_PRINTDEC(x) Serial.print (x, DEC) #define DEBUG_PRINTLN(x) Serial.println (x) -#else +#else +/* A volte il codice di debug e' complicato e deve sostituire parte del codice +notmalmente usato. In questo modo potete specificare parte di codice +da eseguire in modalita' non-debug differente da quello di debug */ #define DEBUG_PRINT(x) #define DEBUG_PRINTDEC(x) #define DEBUG_PRINTLN(x)