From 22da2a0f8203616aedbb746d4b4dbb4f7718ae58 Mon Sep 17 00:00:00 2001 From: eaman Date: Wed, 25 Jan 2017 22:06:17 +0100 Subject: [PATCH] Sequenza con millis --- libraries/common/common.cpp | 74 ++++++++++++++++++- libraries/common/common.h | 34 +++++++++ .../common/examples/sequenza/sequenza.ino | 31 ++++++++ .../loop_array_millis_obj.ino | 52 ++++++++++--- 4 files changed, 179 insertions(+), 12 deletions(-) create mode 100644 libraries/common/examples/sequenza/sequenza.ino diff --git a/libraries/common/common.cpp b/libraries/common/common.cpp index 3fd82db..cbf8fdd 100644 --- a/libraries/common/common.cpp +++ b/libraries/common/common.cpp @@ -83,9 +83,6 @@ Lampeggiatore::Lampeggiatore(int pin) interval = 500; }; - - - // Una funzione facente parte di una classe prende il nome di "metodo" della stessa: void Lampeggiatore::Invert() { // Inverte il lampeggio @@ -184,6 +181,77 @@ void Pwm::UD(long speed ) { } +///////////////////////////////////// +// Sequenza +// Constructor +Sequenza::Sequenza (byte passed[], byte dim) { + ledPins = passed ; + size = dim ; + for (int thisPin = 0; thisPin < size; thisPin++) { + pinMode(ledPins[thisPin], OUTPUT); + } + previousMillis = millis(); + digitalWrite(ledPins[0], HIGH); +} + +void Sequenza::Update(long value) { + // Incrementa dal primo all'ultimo valore dell'array + interval = value; + if (millis() - previousMillis >= interval) { + previousMillis = millis(); + + if ( i < size - 1 ) { + // Spegni precedente led + digitalWrite(ledPins[i], LOW); + + // Accendi successivo led + digitalWrite(ledPins[++i], HIGH); + } + + else if (i == size - 1 ) { + // Ultimo caso + i = 0; + previousMillis = millis(); + digitalWrite(ledPins[i], HIGH); + digitalWrite(ledPins[ size - 1 ], LOW); + } + } +} + + +void Sequenza::Reverse(long value) { + interval = value; + if (millis() - previousMillis >= interval) { + previousMillis = millis(); + + if (i == 0) { // Entry point, ultimo LED + digitalWrite(ledPins[size -1],HIGH); + digitalWrite(ledPins[0],LOW); + i = size -1 ; + } + else { + digitalWrite(ledPins[i],LOW); + digitalWrite(ledPins[--i],HIGH); + } + } +} + +void Sequenza::UD(long value) { + interval = value; + if (millis() - previousMillis >= interval) { + previousMillis = millis(); + // Spegni precedente led + digitalWrite(ledPins[i], LOW); + i = i + inc ; + // Accendi successivo led + digitalWrite(ledPins[i], HIGH); + + if (i == 0 || i == size -1) { + inc = -inc ; + } + } +} + ////////////////// // Funzioni diff --git a/libraries/common/common.h b/libraries/common/common.h index c5ad5d5..f5a763c 100644 --- a/libraries/common/common.h +++ b/libraries/common/common.h @@ -100,6 +100,40 @@ public: byte brightness ; // luminostia' iniziale }; + + +//////////////////////////// +class Sequenza { + // Lampeggia LED in sequenza utilizzando millis() + unsigned long previousMillis ; + byte i = 0; + byte *ledPins; + byte size; + int inc = 1; + long interval; + + public: + Sequenza (byte passed[], byte dim) ; // Array contentente i PINS, dimensioni array + void Update(long value) ; // Accende in sequenza + void Reverse(long value) ; // Invertita + void UD(long value) ; // Up & Down +}; + + + + + + + + + + + + + + + + ////////////////////// // Funzioni diff --git a/libraries/common/examples/sequenza/sequenza.ino b/libraries/common/examples/sequenza/sequenza.ino new file mode 100644 index 0000000..bb9b46b --- /dev/null +++ b/libraries/common/examples/sequenza/sequenza.ino @@ -0,0 +1,31 @@ +/* + For Loop with millis() + +Blink di un array di led in sucessione, +utilizzando millis() per non blocking. +L'array puo' contenere un numero arbitrario di led +(l'ordine in cui compaiono e' l'ordine in cui brillano). + +OOP version. + + Schemi: + - http://lab.piffa.net/schemi/8_led_single_res_bb.png + - http://lab.piffa.net/schemi/8_led_single_res_schem.png + + http://www.arduino.cc/en/Tutorial/ForLoop + */ +#include +void setup() { +}; + +byte pins[] = { // PIN dei LED che compongono la sequenza + 2, 3, 4, 5, 6, 7 +}; +Sequenza seq = Sequenza(pins,sizeof(pins)); // Array dei PINs, quanti elementi compongono l'array + +void loop() { + seq.Update(200); // Passa al segmento suciessivo ogni 200ms +}; + + + diff --git a/multitasking/millis/loop_array_millis_obj/loop_array_millis_obj.ino b/multitasking/millis/loop_array_millis_obj/loop_array_millis_obj.ino index 10301ca..a103c1b 100644 --- a/multitasking/millis/loop_array_millis_obj/loop_array_millis_obj.ino +++ b/multitasking/millis/loop_array_millis_obj/loop_array_millis_obj.ino @@ -22,12 +22,13 @@ class Sequenza { byte i = 0; byte *ledPins; byte size; + int inc = 1; long interval; - + public: Sequenza (byte passed[], byte dim) { - ledPins = passed ; - size = dim ; + ledPins = passed ; + size = dim ; for (int thisPin = 0; thisPin < size; thisPin++) { pinMode(ledPins[thisPin], OUTPUT); } @@ -57,24 +58,57 @@ class Sequenza { } } } + + + void Reverse(long value) { + interval = value; + if (millis() - previousMillis >= interval) { + previousMillis = millis(); + + if (i == 0) { // Entry point, ultimo LED + digitalWrite(ledPins[size - 1], HIGH); + digitalWrite(ledPins[0], LOW); + i = size - 1 ; + } + else { + digitalWrite(ledPins[i], LOW); + digitalWrite(ledPins[--i], HIGH); + } + } + } + + void UD(long value) { + interval = value; + if (millis() - previousMillis >= interval) { + previousMillis = millis(); + // Spegni precedente led + digitalWrite(ledPins[i], LOW); + i = i + inc ; + // Accendi successivo led + digitalWrite(ledPins[i], HIGH); + + if (i == 0 || i == size - 1) { + inc = -inc ; + } + } + } + }; void setup() { - Serial.begin(9600); - //Serial.print(seq.ledPins); }; -byte pins[] = { +byte pins[] = { 2, 3, 4, 5, 6, 7 -}; +}; -Sequenza seq = Sequenza(pins,sizeof(pins)); +Sequenza seq = Sequenza(pins, sizeof(pins)); void loop() { - seq.Update(200); + seq.UD(200); }; -- 2.39.2