]> git.piffa.net Git - sketchbook_andrea/blob - advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino
0d7271e0f9f5fee4acebcdbe86983bc4f55ba604
[sketchbook_andrea] / advanced_projects / state_machine / semaforo_2_2_doppio / semaforo_2_2_doppio.ino
1 /*
2    Incrocio RGB
3
4    Un incrocio costituito da due strade, una principale e una secondaria.
5    La via viene concessa al secondario alla pressione di un bottone,
6    il secondario cambia automaticamente dopo una pausa.
7    Viene utilizzato un oggetto della libreria common per gestire i LED.
8
9    Uno stimolo esterno rappresentato dalla pressione di un bottone
10    causa il passaggio di stato.
11
12    Questo sketch usa due FSM indipendenti che modificano i rispettivi stati.
13
14 - Schema per un led RGB: https://lab.piffa.net/schemi/rgb.jpg
15 - Schema per un bottone: https://www.arduino.cc/en/uploads/Tutorial/inputPullupButton.png
16  */
17
18 #include <common.h>
19 const byte input = 2; // PIN del bottone
20 int pausa = 3000;
21 enum states_available { // Stati della FMS
22     turn_green,    // Dinamico, transizione
23     green,         // Statico
24     wait_button,
25     turn_red,
26     red
27 };
28
29 states_available FSM1  = turn_green; // Semaforo principale
30 states_available FSM2 = turn_red; // Semaforo secondario
31
32
33 RGBLed led_main(11, 10, 9); 
34 RGBLed led_secondary(8, 7, 6); 
35
36 void setup() {
37   pinMode(input, INPUT_PULLUP);
38   Serial.begin(9600);
39   Serial.flush();
40   led_secondary.Red();
41 }
42
43
44 void loop() {
45 switch (FSM1) {
46     // Semaforo principale
47     case turn_green :
48     led_main.Green();
49     FSM1 = green ; // Setta il prossimo state
50     FSM2 = red ;    // Setta il prossimo state
51     break;
52
53     case green:
54     delay(pausa * 2/3);
55     FSM1 = wait_button ;
56     break;
57
58     case wait_button:
59     if (digitalRead(input) == LOW) { 
60     FSM1 = turn_red ; // Il passaggio di stato avviene alla pressione di un bottone
61     };
62     delay(20);
63     break;
64
65     case turn_red :
66     led_main.Yellow();
67     delay(pausa/3);
68     led_main.Red();
69     FSM1 = red ;
70     FSM2 = turn_green; // **** Stimolo al semaforo secondario
71     break;
72
73     case red :
74     //delay(pausa);
75     //main = turn_green ;
76     break;
77 }
78
79 switch (FSM2) {
80     // Semaforo Secondario
81     case turn_green :
82     led_secondary.Green();
83     FSM2 = green ; // Setta il prossimo state
84     break;
85
86     case green:
87     delay(pausa * 2/3);
88     FSM2 = turn_red ; // Niente stimoli
89     break;
90
91     case turn_red :
92     led_secondary.Yellow();
93     delay(pausa/3);
94     FSM1 = turn_green ; // ****
95     FSM2 = red ;
96     led_secondary.Red();
97     break;
98
99     case red :
100     // niente da fare, statico
101     break;
102 }
103 // Debug
104 Serial.print(millis()); 
105 Serial.print(" \t Stato attuale Main: ");
106 Serial.print(FSM1);
107 Serial.print(", secondary: ");
108 Serial.println(FSM2);
109
110 }