From: Andrea Manni Date: Mon, 6 Feb 2017 17:30:29 +0000 (+0100) Subject: Millis corrections, no PWM X-Git-Url: http://git.piffa.net/web?a=commitdiff_plain;h=f71e1e30b8bd0a89b32f7c96e698e23b6fee4479;p=sketchbook_andrea Millis corrections, no PWM --- diff --git a/advanced_projects/state_machine/semaforo_3_millis/semaforo_3_millis.ino b/advanced_projects/state_machine/semaforo_3_millis/semaforo_3_millis.ino index af151a7..50eb1ae 100644 --- a/advanced_projects/state_machine/semaforo_3_millis/semaforo_3_millis.ino +++ b/advanced_projects/state_machine/semaforo_3_millis/semaforo_3_millis.ino @@ -46,7 +46,7 @@ switch (state) { case green: led.Green(); - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { state = wait_button ; timer = millis(); } @@ -66,7 +66,7 @@ switch (state) { case yellow : led.Yellow(); - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { state = turn_red ; timer = millis(); } @@ -78,7 +78,7 @@ switch (state) { case red : led.Red(); - if (millis() > timer + pausa) { + if (millis() - timer >= pausa) { state = turn_green ; timer = millis(); } diff --git a/advanced_projects/state_machine/semaforo_4_doppio/semaforo_4_doppio.ino b/advanced_projects/state_machine/semaforo_4_doppio/semaforo_4_doppio.ino index 3866ad2..1ad650b 100644 --- a/advanced_projects/state_machine/semaforo_4_doppio/semaforo_4_doppio.ino +++ b/advanced_projects/state_machine/semaforo_4_doppio/semaforo_4_doppio.ino @@ -52,7 +52,7 @@ switch (state) { break; case green: - if (millis() > timer + pausa * 2/3) { + if (millis() - timer => pausa * 2/3) { state = wait_button ; timer = millis(); } @@ -73,7 +73,7 @@ switch (state) { break; case yellow : - if (millis() > timer + pausa / 3) { + if (millis() - timer >= pausa / 3) { state = turn_red ; timer = millis(); } @@ -86,7 +86,7 @@ switch (state) { break; case red : - if (millis() > timer + pausa /3) { + if (millis() - timer >= pausa /3) { state = turn_sec_yellow ; timer = millis(); } @@ -98,7 +98,7 @@ switch (state) { break; case sec_yellow : - if (millis() > timer + pausa / 3) { + if (millis() - timer >= pausa / 3) { state = turn_green ; timer = millis(); } diff --git a/advanced_projects/state_machine/semaforo_5_doppia_fsm/semaforo_5_doppia_fsm.ino b/advanced_projects/state_machine/semaforo_5_doppia_fsm/semaforo_5_doppia_fsm.ino index 619b85e..602af74 100644 --- a/advanced_projects/state_machine/semaforo_5_doppia_fsm/semaforo_5_doppia_fsm.ino +++ b/advanced_projects/state_machine/semaforo_5_doppia_fsm/semaforo_5_doppia_fsm.ino @@ -49,7 +49,7 @@ switch (FSM1) { break; case green: - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { FSM1 = wait_button ; timer = millis(); } @@ -70,7 +70,7 @@ switch (FSM1) { break; case yellow : - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { FSM1 = turn_red ; timer = millis(); } @@ -100,7 +100,7 @@ switch (FSM2) { break; case green: - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { FSM2 = turn_yellow; timer = millis(); } @@ -112,7 +112,7 @@ switch (FSM2) { break; case yellow : - if (millis() > timer + pausa / 3) { + if (millis() - timer >= pausa / 3) { FSM2 = turn_red ; timer = millis(); } diff --git a/advanced_projects/state_machine/semaforo_rgb_stimolo_millis/semaforo_rgb_stimolo_millis.ino b/advanced_projects/state_machine/semaforo_rgb_stimolo_millis/semaforo_rgb_stimolo_millis.ino index af151a7..50eb1ae 100644 --- a/advanced_projects/state_machine/semaforo_rgb_stimolo_millis/semaforo_rgb_stimolo_millis.ino +++ b/advanced_projects/state_machine/semaforo_rgb_stimolo_millis/semaforo_rgb_stimolo_millis.ino @@ -46,7 +46,7 @@ switch (state) { case green: led.Green(); - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { state = wait_button ; timer = millis(); } @@ -66,7 +66,7 @@ switch (state) { case yellow : led.Yellow(); - if (millis() > timer + pausa * 2/3) { + if (millis() - timer >= pausa * 2/3) { state = turn_red ; timer = millis(); } @@ -78,7 +78,7 @@ switch (state) { case red : led.Red(); - if (millis() > timer + pausa) { + if (millis() - timer >= pausa) { state = turn_green ; timer = millis(); } diff --git a/multitasking/millis/loop_array_millis/loop_array_millis.ino b/multitasking/millis/loop_array_millis/loop_array_millis.ino index 36e5125..4ba4d58 100644 --- a/multitasking/millis/loop_array_millis/loop_array_millis.ino +++ b/multitasking/millis/loop_array_millis/loop_array_millis.ino @@ -32,7 +32,7 @@ void setup() { void loop() { if (millis() - previousMillis >= interval) { - previousMillis = millis(); + previousMillis += interval ; if ( i < sizeof(ledPins) - 1 ) { // Spegni precedente led @@ -45,7 +45,7 @@ void loop() { else if (i == sizeof(ledPins) - 1 ) { // Ultimo caso i = 0; - previousMillis = millis(); + previousMillis += interval ; digitalWrite(ledPins[i], HIGH); digitalWrite(ledPins[ sizeof(ledPins) - 1 ], LOW); } 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 a103c1b..e2e783d 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 @@ -39,7 +39,7 @@ class Sequenza { void Update(long value) { interval = value; if (millis() - previousMillis >= interval) { - previousMillis = millis(); + previousMillis += interval; if ( i < size - 1 ) { // Spegni precedente led @@ -52,7 +52,7 @@ class Sequenza { else if (i == size - 1 ) { // Ultimo caso i = 0; - previousMillis = millis(); + previousMillis += interval; digitalWrite(ledPins[i], HIGH); digitalWrite(ledPins[ size - 1 ], LOW); } @@ -63,7 +63,7 @@ class Sequenza { void Reverse(long value) { interval = value; if (millis() - previousMillis >= interval) { - previousMillis = millis(); + previousMillis += interval; if (i == 0) { // Entry point, ultimo LED digitalWrite(ledPins[size - 1], HIGH); @@ -80,7 +80,7 @@ class Sequenza { void UD(long value) { interval = value; if (millis() - previousMillis >= interval) { - previousMillis = millis(); + previousMillis += interval; // Spegni precedente led digitalWrite(ledPins[i], LOW); i = i + inc ; @@ -96,7 +96,6 @@ class Sequenza { }; - void setup() { }; @@ -110,6 +109,3 @@ Sequenza seq = Sequenza(pins, sizeof(pins)); void loop() { seq.UD(200); }; - - - diff --git a/multitasking/millis/zapper/blink/blink.ino b/multitasking/millis/zapper/blink/blink.ino new file mode 100644 index 0000000..9d52db1 --- /dev/null +++ b/multitasking/millis/zapper/blink/blink.ino @@ -0,0 +1,82 @@ +/* + Blink v1 + + Accensione e spegnimanto di un ledA utilizzando variabili + per impostare la velocita' del lampeggio. + + */ + +// Pin 13 ha un led collegato di default +int ledA = 6; +int ledB = 7; + +void setup() { + // Inizializziamo il PIN 13 come OUTPUT + pinMode(ledA, OUTPUT); + pinMode(ledB, OUTPUT); +} + +void loop() { + // 1st step + digitalWrite(ledA, HIGH); + delay(150); + digitalWrite(ledA, LOW); + delay(50); + digitalWrite(ledB, HIGH); + delay(150); + digitalWrite(ledB, LOW); + delay(50); + + // 2st step + digitalWrite(ledA, HIGH); + delay(150); + digitalWrite(ledA, LOW); + delay(50); + + + // 3st step + delay(150); + digitalWrite(ledA, LOW); + digitalWrite(ledB, LOW); + delay(50); + digitalWrite(ledA, HIGH); + digitalWrite(ledB, HIGH); + delay(350); + digitalWrite(ledA, LOW); + digitalWrite(ledB, LOW); + delay(50); + + + // Reverse + // 1st step + digitalWrite(ledB, HIGH); + delay(150); + digitalWrite(ledB, LOW); + delay(50); + digitalWrite(ledA, HIGH); + delay(150); + digitalWrite(ledA, LOW); + delay(50); + + // 2st step + digitalWrite(ledB, HIGH); + delay(150); + digitalWrite(ledB, LOW); + delay(50); + + + // 3st step + delay(150); + digitalWrite(ledB, LOW); + digitalWrite(ledA, LOW); + delay(50); + digitalWrite(ledB, HIGH); + digitalWrite(ledA, HIGH); + delay(350); + digitalWrite(ledB, LOW); + digitalWrite(ledA, LOW); + delay(50); + + +} +