-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_1.ino
380 lines (303 loc) · 10.1 KB
/
train_1.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//this is the program for the first IR(the 4 gates exept the car one)
#include<Servo.h>
#include <ESP8266WiFi.h>
//D0 free
int ir1 = D1;
int ir2 = D2;
//D6 free for LCD
int ir4 = D3;
//D7 is servo motor
int ir5 = D4;
int t1 = D6;
int t2 = D7;
//D7 free for LCD
int op = D8; //this sends the digital value 1 to the second mcu
//buzzer A0 free (this can be used as a regular digital pin also)for buzzer
int pos=0;
String apiKey = "GVKY13RMI3LSHAK6"; // Enter your Write API key from ThingSpeak
const char *ssid = "home"; // replace with your wifi ssid and wpa2 key
const char *pass = "12345678";
const char* server = "api.thingspeak.com";
int timeCounter=0;
int valIp;
Servo servo;
WiFiClient client;
void setup()
{
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir4, INPUT);
pinMode(ir5, INPUT);
pinMode(t1, OUTPUT);
pinMode(t2, OUTPUT);
pinMode(op, OUTPUT);
servo.attach(D5);
Serial.begin(9600);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
Serial.println(valIp);
delay(1000);
//condition1 00
digitalWrite(t1,LOW);
digitalWrite(t2,LOW);
Serial.println(LOW);
Serial.println(HIGH);
Serial.println("waiting for a train to come ");
int ReadIr1 = digitalRead(ir1);
int ReadIr2, ReadIr4, ReadIr5;
ReadIr5 = digitalRead(ir5);
if (ReadIr1 == 1 )
{
servo.write(0);
ReadIr2 = digitalRead(ir2);
Serial.println("IR1 is activated :train is coming");
digitalWrite(t1,LOW);
digitalWrite(t2,HIGH);
Serial.println(t1);
Serial.println(t2);
//lcd_write("train is at 1km")
while (ReadIr2 == 0) //wait for train to come from first ir to the 2nd ir
{
digitalWrite(op,HIGH); //car mcu starts working whenever the train is approaching. The red light will glow if car detected.else green light
ReadIr2 = digitalRead(ir2);
Serial.println("waiting for train to reach ir2: train is at around 1 km from the crossing");
//condition1 0 1
delay(500);
if (ReadIr2 == 1)
{
goto A;
}
ReadIr2 = digitalRead(ir2);
timeCounter+=1;
}
A: if ( ReadIr2 == 1 )
{
for(pos=0;pos<=90;pos+=1)
{
servo.write(pos);
//make buzzer high here with appropriate delays
delay(25);
}
Serial.println("train has reached ir2: the train has arrived at 500 mts");
digitalWrite(op,LOW); //car mcu stops working whenever the train is approaching. The red light will glow if car detected.else green light
ReadIr4 = digitalRead(ir4);
while (ReadIr4 == 0)
{
//lcd_write("waiting for train to reach ir4: train is less than 500 mts from the gate")
Serial.println("train is less than 500 mts from the gate");
//condition1 1 0
digitalWrite(t1,HIGH);
digitalWrite(t2,LOW);
Serial.println(t1);
Serial.println(t2);
delay(500);
if (ReadIr4 == 1)
{
goto B;
}
ReadIr4 = digitalRead(ir4);
timeCounter+=1;
}
}
B: if ( ReadIr4 == 1 )
{
//lcd_write(" calm....train has almost left")
Serial.println("train reached ir 4: the train is leaving");
ReadIr5 = digitalRead(ir5);
while (ReadIr5 == 0)
{
//lcd_write("more calm...train has almost left")
Serial.println("waiting for train to reach ir5: train has almost left");
//condition1 1 1
digitalWrite(t1,HIGH);
digitalWrite(t2,HIGH);
Serial.println(t1);
Serial.println(t2);
ReadIr5 = digitalRead(ir5);
if (ReadIr5 == 1)
{
goto C;
}
timeCounter+=1;
delay(500);
}
C: while (ReadIr5 == 1)
{
Serial.println("waiting for train to complete pass the IR5 : train is leaving(crossing the 5th gate)");
ReadIr5 = digitalRead(ir5);
//condition1 0 0
digitalWrite(t1,LOW);
digitalWrite(t2,LOW);
Serial.println(t1);
Serial.println(t2);
if (ReadIr5 == 0)
{
goto D;
}
timeCounter+=1;
delay(500);
}
D: if (ReadIr5 == 0)
{
Serial.println("track now clear: Train has left the craoosing. Crossing is now open again");
for(pos=90;pos>=0;pos-=1)
{
servo.write(pos);
//make buzzer high here with appropriate delays
delay(25);
}
}
}
Serial.print("time taken is : ");
Serial.print(timeCounter);
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(timeCounter);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
timeCounter=0;
}
else if (ReadIr5 == 1 )
{
servo.write(0);
Serial.println("IR5 is activated :train is coming");
//lcd_write("train is at 1km")
ReadIr4 = digitalRead(ir4);
while (ReadIr4 == 0) //wait for train to come from first ir to the 2nd ir
{
digitalWrite(op,HIGH); //car mcu starts working whenever the train is approaching. The red light will glow if car detected.else green light
ReadIr4 = digitalRead(ir4);
Serial.println("waiting for train to reach ir2: train is at around 1 km from the crossing");
//condition1 0 1
digitalWrite(t1,LOW);
digitalWrite(t2,HIGH);
Serial.println(0);
Serial.println(1);
delay(500);
if (ReadIr4 == 1)
{
goto x;
}
timeCounter+=1;
ReadIr4 = digitalRead(ir4);
}
x: if ( ReadIr4 == 1 )
{
for(pos=0;pos<=90;pos+=1)
{
servo.write(pos);
delay(25);
}
Serial.println("train has reached ir4: the train has arrived at 500 mts");
digitalWrite(op,HIGH); //car mcu starts working whenever the train is approaching. The red light will glow if car detected.else green light
ReadIr2 = digitalRead(ir2);
while (ReadIr2 == 0)
{
//lcd_write("waiting for train to reach ir2: train is less than 500 mts from the gate")
Serial.println("train is less than 500 mts from the gate");
//condition1 1 0
digitalWrite(t1,HIGH);
digitalWrite(t2,LOW);
Serial.println(t1);
Serial.println(t2);
delay(500);
if (ReadIr2 == 1)
{
goto y;
}
ReadIr2 = digitalRead(ir2);
timeCounter+=1;
}
}
y : if ( ReadIr2 == 1 )
{
//lcd_write(" calm....train has almost left")
Serial.println("train reached ir 2: the train is leaving");
ReadIr1 = digitalRead(ir1);
while (ReadIr1 == 0)
{
//lcd_write("more calm...train has almost left")
Serial.println("waiting for train to reach ir1: train has almost left");
//condition1 1 1
digitalWrite(t1,HIGH);
digitalWrite(t2,HIGH);
Serial.println(t1);
Serial.println(t2);
ReadIr1 = digitalRead(ir1);
if (ReadIr1 == 1)
{
goto z;
}
delay(500);
timeCounter+=1;
}
z: while (ReadIr1 == 1)
{
Serial.println("waiting for train to complete pass the IR1 : train is leaving(crossing the 5th gate)");
//condition1 0 0
digitalWrite(t1,LOW);
digitalWrite(t2,LOW);
Serial.println(t1);
Serial.println(t2);
ReadIr1 = digitalRead(ir1);
if (ReadIr1 == 0)
{
goto v;
}
delay(500);
timeCounter+=1;
}
v: if (ReadIr1 == 0)
{
Serial.println("track now clear: Train has left the craoosing. Crossing is now open again");
for(pos=90;pos>=0;pos-=1)
{
servo.write(pos);
delay(25);
}
}
}
Serial.print("time taken is : ");
Serial.print(timeCounter);
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(timeCounter);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
timeCounter=0;
}
delay(500);
}