-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduino_Multiblock_Original.ino
245 lines (223 loc) · 8.39 KB
/
Arduino_Multiblock_Original.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
int Sensor1 = A0; //Sensor 1 is setup on pin A0
int Sensor2 = A1; //Sensor 2 is setup on pin A1
int Green1 = 2; // Green Signal 1 is digital pin 2
int Yellow1 = 3;// Yellow Signal 1 is digital pin 3
int Red1 = 4;// Red Signal 1 is digital pin 4
int Green2 = 5;// Green Signal 2 is digital pin 5
int Yellow2 = 6;// Yellow Signal 1 is digital pin 6
int Red2 = 7;//Red Signal 2 is digital pin 7
int blockASend=8;// The transmit pin for the previous block in the sequence
int blockBSend=9;// The transmit pin for the next block in the sequence
int blockARec=A2;// The recieve pin for the previous block in the sequence
int blockBRec=A3;// The recieve pin for the next block in the sequence
void setup() {
Serial.begin(9600);
//Establish all of our pins at inputs or outputs
pinMode(Sensor1, INPUT);
pinMode(Sensor2, INPUT);
pinMode(Green1, OUTPUT);
pinMode(Yellow1, OUTPUT);
pinMode(Red1, OUTPUT);
pinMode(Green2, OUTPUT);
pinMode(Yellow2, OUTPUT);
pinMode(Red2, OUTPUT);
pinMode(blockASend, OUTPUT);
pinMode(blockBSend, OUTPUT);
pinMode(blockARec, INPUT);
pinMode(blockBRec, INPUT);
}
enum SIGNALSTATES
{
ST_GG,
ST_GY,
ST_YG,
ST_YY,
ST_RR1,
ST_RR2,
ST_RR3,
}; //Sets up different signal states for the entire program
SIGNALSTATES signalState = ST_GG; //Sets default state to Green and Green
void loop(){
int valA1 = analogRead(Sensor1);
int valA2 = analogRead(Sensor2);
int valA3 = analogRead(blockARec);
int valA4 = analogRead(blockBRec);
// Sets up the values of the different signals we will be reading
Serial.println(valA1);
Serial.println(valA2);
Serial.println(valA3);
Serial.println(valA4);
// Sets up the different values to be read on the serial monitor in the Arduino IDE
delay(500);
switch(signalState)
{
case ST_GG:
signalgg(valA1, valA2, valA3, valA4);
break;
case ST_GY:
signalgy(valA1, valA2, valA3, valA4);
break;
case ST_YG:
signalyg(valA1, valA2, valA3, valA4);
break;
case ST_YY:
signalyy(valA1, valA2, valA3, valA4);
break;
case ST_RR1:
signalrr1(valA1, valA2, valA3, valA4);
break;
case ST_RR2:
signalrr2(valA1, valA2, valA3, valA4);
break;
case ST_RR3:
signalrr3(valA1, valA2, valA3, valA4);
break;
//this sets up our different loops within the main loop for our different signal states
}
}
void signalgg(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, LOW); //Sets Green1 to ON
digitalWrite(Yellow1, HIGH);
digitalWrite(Red1, HIGH);
digitalWrite(Green2, LOW); //Sets Green2 to ON
digitalWrite(Yellow2, HIGH);
digitalWrite(Red2, HIGH);
analogWrite(blockASend, 0); //this will not transmit a signal to Block A
analogWrite(blockBSend, 0); //this will not transmit a signal to Block B
delay(500);
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4>300)){
signalState = ST_GY;//block not occupied but Block B is
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4<300)){
signalState = ST_YG;//block not occupied but Block A is
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4>300)){
signalState = ST_YY;//block not occupied but Block B and Block A are
}
if ((valA1 < 500) && (valA2 >500)){
signalState = ST_RR1;// block is occupied entered from Sensor1
}
if ((valA1 > 500) && (valA2 <500)){
signalState = ST_RR2;// block is occupied entered from Sensor2
}
}
void signalgy(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, LOW); //Sets Green1 to ON
digitalWrite(Yellow1, HIGH);
digitalWrite(Red1, HIGH);
digitalWrite(Green2, HIGH);
digitalWrite(Yellow2, LOW);//Sets Yellow2 to ON
digitalWrite(Red2, HIGH);
analogWrite(blockASend, 0); //this will not transmit a signal to Block A
analogWrite(blockBSend, 0); //this will not transmit a signal to Block B
delay(500);
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4<300)){
signalState = ST_GG;//block not occupied No adjacent blocks occupied
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4<300)){
signalState = ST_YG;//block not occupied but Block A is
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4>300)){
signalState = ST_YY;//block not occupied but Block B and Block A are
}
if ((valA1 < 500) && (valA2 >500)){
signalState = ST_RR1;// block is occupied entered from Sensor1
}
if ((valA1 > 500) && (valA2 <500)){
signalState = ST_RR2;// block is occupied entered from Sensor2
}
}
void signalyg(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, HIGH);
digitalWrite(Yellow1, LOW);//Sets Yellow1 to ON
digitalWrite(Red1, HIGH);
digitalWrite(Green2, LOW);//Sets Green2 to ON
digitalWrite(Yellow2, HIGH);
digitalWrite(Red2, HIGH);
analogWrite(blockASend, 0); //this will not transmit a signal to Block A
analogWrite(blockBSend, 0); //this will not transmit a signal to Block B
delay(500);
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4<300)){
signalState = ST_GG;//block not occupied No adjacent blocks occupied
}
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4>300)){
signalState = ST_GY;//block not occupied but Block B is
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4>300)){
signalState = ST_YY;//block not occupied but Block B and Block A are
}
if ((valA1 < 500) && (valA2 >500)){
signalState = ST_RR1;// block is occupied entered from Sensor1
}
if ((valA1 > 500) && (valA2 <500)){
signalState = ST_RR2;// block is occupied entered from Sensor2
}
}
void signalyy(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, HIGH);
digitalWrite(Yellow1, LOW);//Sets Yellow1 to ON
digitalWrite(Red1, HIGH);
digitalWrite(Green2, HIGH);
digitalWrite(Yellow2, LOW);//Sets Yellow2 to ON
digitalWrite(Red2, HIGH);
analogWrite(blockASend, 0); //this will not transmit a signal to Block A
analogWrite(blockBSend, 0); //this will not transmit a signal to Block B
delay(500);
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4<300)){
signalState = ST_GG;//block not occupied No adjacent blocks occupied
}
if ((valA1 > 500) && (valA2 >500) && (valA3<300) && (valA4>300)){
signalState = ST_GY;//block not occupied but Block B is
}
if ((valA1 > 500) && (valA2 >500) && (valA3>300) && (valA4<300)){
signalState = ST_YG;//block not occupied but Block B and Block A are
}
if ((valA1 < 500) && (valA2 >500)){
signalState = ST_RR1;// block is occupied entered from Sensor1
}
if ((valA1 > 500) && (valA2 <500)){
signalState = ST_RR2;// block is occupied entered from Sensor2
}
}
void signalrr1(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, HIGH);
digitalWrite(Yellow1, HIGH);
digitalWrite(Red1, LOW);//Sets Red1 to ON
digitalWrite(Green2, HIGH);
digitalWrite(Yellow2, HIGH);
digitalWrite(Red2, LOW);//Sets Red2 to ON
analogWrite(blockASend, 1023); //this will transmit a signal to Block A
analogWrite(blockBSend, 1023); //this will transmit a signal to Block B
delay(500);
if ((valA1>500) && (valA2<500)){
signalState = ST_RR3; //switched to transition Red signal state
}
}
void signalrr2(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, HIGH);
digitalWrite(Yellow1, HIGH);
digitalWrite(Red1, LOW);//Sets Red1 to ON
digitalWrite(Green2, HIGH);
digitalWrite(Yellow2, HIGH);
digitalWrite(Red2, LOW);//Sets Red2 to ON
analogWrite(blockASend, 1023); //this will transmit a signal to Block A
analogWrite(blockBSend, 1023); //this will transmit a signal to Block B
delay(500);
if ((valA1<500) && (valA2>500)){
signalState = ST_RR3; //switched to transition Red signal state
}
}
void signalrr3(int valA1, int valA2, int valA3, int valA4){
digitalWrite(Green1, HIGH);
digitalWrite(Yellow1, HIGH);
digitalWrite(Red1, LOW);//Sets Red1 to ON
digitalWrite(Green2, HIGH);
digitalWrite(Yellow2, HIGH);
digitalWrite(Red2, LOW);//Sets Red2 to ON
analogWrite(blockASend, 1023); //this will transmit a signal to Block A
analogWrite(blockBSend, 1023); //this will transmit a signal to Block B
delay(500);
if ((valA1>500) && (valA2>500)){
signalState = ST_GG; //switched to Green unoccupied signal
}
}