]> git.piffa.net Git - sketchbook_andrea/blob - advanced_projects/state_machine/semaforo_2_2_doppio/semaforo_2_2_doppio.ino
e5613619bd906cfb308341161e3b026fc29493da
[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  ; // Semaforo principale
30 states_available FSM2 ; // Semaforo secondario
31
32
33 void setup() {
34   pinMode(input, INPUT_PULLUP);
35   Serial.begin(9600);
36   Serial.flush();
37 }
38
39 RGBLed led_main(11, 10, 9); 
40 RGBLed led_secondary(8, 7, 6); 
41
42 void loop() {
43 switch (FSM1) {
44     // Semaforo principale
45     case turn_green :
46     led_main.Green();
47     FSM1 = green ; // Setta il prossimo state
48     FSM2 = red ;    // Setta il prossimo state
49     break;
50
51     case green:
52     delay(pausa * 2/3);
53     FSM1 = wait_button ;
54     break;
55
56     case wait_button:
57     if (digitalRead(input) == LOW) { 
58     FSM1 = turn_red ; // Il passaggio di stato avviene alla pressione di un bottone
59     };
60     delay(20);
61     break;
62
63     case turn_red :
64     led_main.Yellow();
65     delay(pausa/3);
66     led_main.Red();
67     FSM1 = red ;
68     FSM2 = turn_green; // **** Stimolo al semaforo secondario
69     break;
70
71     case red :
72     //delay(pausa);
73     //main = turn_green ;
74     break;
75 }
76
77 switch (FSM2) {
78     // Semaforo Secondario
79     case turn_green :
80     led_secondary.Green();
81     FSM2 = green ; // Setta il prossimo state
82     break;
83
84     case green:
85     delay(pausa * 2/3);
86     FSM2 = turn_red ; // Niente stimoli
87     break;
88
89     case turn_red :
90     led_secondary.Yellow();
91     delay(pausa/3);
92     FSM1 = turn_green ; // ****
93     FSM2 = red ;
94     led_secondary.Red();
95     break;
96
97     case red :
98     // niente da fare, statico
99     break;
100 }
101 // Debug
102 Serial.print(millis()); 
103 Serial.print(" \t Stato attuale Main: ");
104 Serial.print(FSM1);
105 Serial.print(", secondary: ");
106 Serial.println(FSM2);
107
108 }