From 90f3116fa09ed583c137d1df423b9913f55522fa Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Thu, 30 Apr 2015 18:08:46 +0200 Subject: [PATCH] gmulti ptr --- .../BlinkWithoutDelay_7_struct.ino | 52 +++++++++++++++++ .../BlinkWithoutDelay_8_struct_pointer.ino | 57 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino create mode 100644 multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino diff --git a/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino b/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino new file mode 100644 index 0000000..621083e --- /dev/null +++ b/multitasking/BlinkWithoutDelay_7_struct/BlinkWithoutDelay_7_struct.ino @@ -0,0 +1,52 @@ +/* Blink without Delay + Soluzione + + Introdotto un argomento per la funzione che nodifica l'intervallo di lampeggio + */ + +struct blinkLed { + // 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 +} +; + +// Instanziamo i due led dalla classe +blinkLed ledA = { + 13 , LOW , 1000, 0 }; +blinkLed ledB = { + 12, LOW, 500, 0}; + +void setup() { + pinMode(ledA.ledPin, OUTPUT); + pinMode(ledB.ledPin, OUTPUT); +} + +void loop() +{ + ledA = lightLed(ledA ); + ledB = lightLed(ledB ); +} + +//////////////// +// Funzioni + +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] + // save the last time you blinked the LED + temp.previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + temp.ledState = !temp.ledState ; // Inverti il LED + } + digitalWrite(temp.ledPin, temp.ledState); + return(temp); // copy by value +} + + + diff --git a/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino b/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino new file mode 100644 index 0000000..635642d --- /dev/null +++ b/multitasking/BlinkWithoutDelay_8_struct_pointer/BlinkWithoutDelay_8_struct_pointer.ino @@ -0,0 +1,57 @@ +/* Blink without Delay + + Struct con pointer + + La funzione lightLed() ora passa un pointer alla posizione di memoria + della struttura dati ledA / ledB, non viene fatta una copia di + tutto lo struct ledA --> temp per poi far tornare (return(temp)) + una struttura aggiornate di temp che vera' nuovamente copiata + sull'originale ledA. + + Viene fatta un pass by reference e non un pass by value, + vengono usate meno risose di calcolo e meno memoria. + */ + +struct blinkLed { + // 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 +}; +// Instanziamo i due led dalla struttur +blinkLed ledA = { + 13 , LOW , 1000, 0 }; +blinkLed ledB = { + 12, LOW, 500, 0}; + +void setup() { + pinMode(ledA.ledPin, OUTPUT); + pinMode(ledB.ledPin, OUTPUT); +} + +void loop() +{ + lightLed(&ledA ); // Viene passato il riferimento (Left value) di ledA + lightLed(&ledB ); +} + +//////////////// +// Funzioni + +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 the LED is off turn it on and vice-versa: + temp->ledState = !temp->ledState ; // Forma contratta, deference operator: temp->ledState == (*temp).ledState + } + digitalWrite(temp->ledPin, temp->ledState); + //return(temp); // copy by value +} + + + -- 2.39.2