From 75928e346c7e5631544b0bea01e2d4ae9a5d749e Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 13 Mar 2017 16:25:24 +0100 Subject: [PATCH] FSM --- RGB_LED/rgb_4_array/rgb_4_array.ino | 2 +- RGB_LED/rgb_7_obj/rgb_7_obj.ino | 8 +---- .../semaforo_2_2_doppio.ino | 10 +++--- .../semaforo_4_doppio_single_FSM.ino | 35 ++++++++----------- .../semaforo_5_doppia_fsm.ino | 9 ++--- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/RGB_LED/rgb_4_array/rgb_4_array.ino b/RGB_LED/rgb_4_array/rgb_4_array.ino index 39060d4..552fa8e 100644 --- a/RGB_LED/rgb_4_array/rgb_4_array.ino +++ b/RGB_LED/rgb_4_array/rgb_4_array.ino @@ -8,7 +8,7 @@ */ byte pin[3] = {11, 10, 9}; // 2v a 20ma: che resistenza dovro usare? -byte color[3] = {255, 255, 255}; +byte color[3] = {0, 255, 255}; diff --git a/RGB_LED/rgb_7_obj/rgb_7_obj.ino b/RGB_LED/rgb_7_obj/rgb_7_obj.ino index e63ace4..5a3dcf8 100644 --- a/RGB_LED/rgb_7_obj/rgb_7_obj.ino +++ b/RGB_LED/rgb_7_obj/rgb_7_obj.ino @@ -15,11 +15,6 @@ class RGBLed { byte bluePin ; public: - // Public properties - byte redValue ; - byte greenValue ; - byte blueValue ; - // Constructor: come viene instanziato un oggetto facente parte della classe RGBLed(byte pinR, byte pinG, byte pinB) { @@ -28,7 +23,7 @@ class RGBLed { greenPin = pinG ; bluePin = pinB ; - // Equvalente del Setup() per inizializzare i PIN + // Equivalente del Setup() per inizializzare i PIN pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(greenPin, OUTPUT); @@ -68,7 +63,6 @@ void loop() { delay(1000); led.SetColor(255, 0, 255) ; // Mettiamo il LED in Green delay(1000); - } /* Domande diff --git a/advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino b/advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino index e561361..0d7271e 100644 --- a/advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino +++ b/advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino @@ -26,18 +26,20 @@ enum states_available { // Stati della FMS red }; -states_available FSM1 ; // Semaforo principale -states_available FSM2 ; // Semaforo secondario +states_available FSM1 = turn_green; // Semaforo principale +states_available FSM2 = turn_red; // Semaforo secondario +RGBLed led_main(11, 10, 9); +RGBLed led_secondary(8, 7, 6); + void setup() { pinMode(input, INPUT_PULLUP); Serial.begin(9600); Serial.flush(); + led_secondary.Red(); } -RGBLed led_main(11, 10, 9); -RGBLed led_secondary(8, 7, 6); void loop() { switch (FSM1) { diff --git a/advanced_projects/state_machine/semaforo_4_doppio_single_FSM/semaforo_4_doppio_single_FSM.ino b/advanced_projects/state_machine/semaforo_4_doppio_single_FSM/semaforo_4_doppio_single_FSM.ino index 0ab3b51..de798b3 100644 --- a/advanced_projects/state_machine/semaforo_4_doppio_single_FSM/semaforo_4_doppio_single_FSM.ino +++ b/advanced_projects/state_machine/semaforo_4_doppio_single_FSM/semaforo_4_doppio_single_FSM.ino @@ -20,11 +20,11 @@ const byte input = 2; // PIN del bottone int pausa = 3000; long timer ; +boolean wait = 0; // Memoria bottone + enum states_available { // Stati della FMS turn_green, // Dinamico, transizione green, // Statico - wait_button, // Evento - Stimolo - turn_yellow, // Dinamico, transizione yellow, // Statico turn_red, // Dinamico, transizione turn_sec_yellow,// Yellow per semaforo secondario @@ -32,7 +32,7 @@ enum states_available { // Stati della FMS red // Statico }; -states_available state ; +states_available state ; void setup() { @@ -54,29 +54,22 @@ switch (state) { break; case green: - if (millis() - timer => pausa * 2/3) { - state = wait_button ; - timer += pausa * 2/3 ; - } - break; + led.Green(); + if (wait && (millis() - timer >= pausa * 2/3)) { + state = yellow; + timer = millis(); + } - case wait_button: - if (digitalRead(input) == LOW) { - delay(20); // Debouncing, si potrebbe fare con millis() - state = turn_yellow ; // Il passaggio di stato avviene alla pressione di un bottone - timer = millis(); - }; - - break; - - case turn_yellow : - led.Yellow(); - state = yellow ; - break; + if (digitalRead(input) == LOW) { + wait = 1; + } + break; case yellow : + led.Yellow(); if (millis() - timer >= pausa / 3) { state = turn_red ; + wait = 0; timer += pausa / 3; } break; 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 db72e6a..d7e9146 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 @@ -76,9 +76,9 @@ switch (FSM1) { break; case yellow : - if (millis() - timer >= pausa * 2/3) { + if (millis() - timer >= pausa * 1/3) { FSM1 = turn_red ; - timer += pausa * 2/3 ; + timer += pausa * 1/3 ; } break; @@ -113,20 +113,21 @@ switch (FSM2) { break; case turn_yellow : - led2.Yellow(); + led2.Blue(); FSM2 = yellow ; break; case yellow : if (millis() - timer >= pausa / 3) { FSM2 = turn_red ; - timer += pausa * 2/3; + //timer += pausa * 2/3; } break; case turn_red : FSM2 = red ; FSM1 = turn_green; + timer = millis(); break; case red : -- 2.39.2