-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.ino
175 lines (141 loc) · 5.63 KB
/
Main.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
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h>
#endif
// Traffic Light System (mimicing and controlling 4 lanes light) and radio receiver
// ( Speed, receving pin , Transmitting pin , push to talk)
RH_ASK driver(2000, 2, 4, 5);
// Traffic light pins allotment
int r1 = 52;
int y1 = 50;
int g1 = 48;
int r2 = 42;
int y2 = 40;
int g2 = 38;
int r3 = 32;
int y3 = 30;
int g3 = 28;
int r4 = 24;
int y4 = 22;
int g4 = 23;
int eachlaneTime = 2000; // time for green light to remain active
int lightGap = 1000; // Between red and yellow , yellow and green
void setup() {
pinMode (r1, OUTPUT);
pinMode (y1, OUTPUT);
pinMode (g1, OUTPUT);
pinMode (r2, OUTPUT);
pinMode (y2, OUTPUT);
pinMode (g2, OUTPUT);
pinMode (r3, OUTPUT);
pinMode (y3, OUTPUT);
pinMode (g3, OUTPUT);
pinMode (r4, OUTPUT);
pinMode (y4, OUTPUT);
pinMode (g4, OUTPUT);
#ifdef RH_HAVE_SERIAL
Serial.begin(9600);
#endif
if (!driver.init())
#ifdef RH_HAVE_SERIAL
Serial.println("init failed");
#else
;
#endif
}
void loop() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
// Message with a good checksum received, dump it.
//driver.printBuffer("Got:", buf, buflen);
String rev;
for (int i = 0; i < buflen;i++) {
rev += (char)buf[i];
}
while (rev.equals("Invalid Input"))
{
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r1,0);delay(lightGap);digitalWrite(y1,1);delay(lightGap);
digitalWrite(y1,0);digitalWrite(g1,1);delay(eachlaneTime);
String check = Receiver();
if (check != "Invalid Input") {break;}
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r2,0);delay(lightGap);digitalWrite(y2,1);delay(lightGap);
digitalWrite(y2,0);digitalWrite(g2,1);delay(eachlaneTime);
check = Receiver();
if (check != "Invalid Input") {break;}
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);delay(100);
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r3,0);delay(lightGap);digitalWrite(y3,1);delay(lightGap);
digitalWrite(y3,0);digitalWrite(g3,1);delay(eachlaneTime);
check = Receiver();
if (check != "Invalid Input") {break;}
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,0);delay(lightGap);digitalWrite(y4,1);delay(lightGap);
digitalWrite(y4,0);digitalWrite(g4,1);delay(eachlaneTime);
check = Receiver();
if (check != "Invalid Input") {break;}
}
if (rev.equals("East"))
{
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r1,0);digitalWrite(y1,0);digitalWrite(g1,1);
}
else if (rev == "South")
{
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);
digitalWrite(r2,0);digitalWrite(y2,0);digitalWrite(g2,1);
}
else if (rev == "West")
{
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,0);digitalWrite(y3,0);digitalWrite(g3,1);
digitalWrite(r4,1);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);
}
else if (rev == "North")
{
digitalWrite(r2,1);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,1);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,0);digitalWrite(y4,0);digitalWrite(g4,1);
digitalWrite(r1,1);digitalWrite(y1,0);digitalWrite(g1,0);
}
else
{
digitalWrite(r2,0);digitalWrite(y2,0);digitalWrite(g2,0);
digitalWrite(r3,0);digitalWrite(y3,0);digitalWrite(g3,0);
digitalWrite(r4,0);digitalWrite(y4,0);digitalWrite(g4,0);
digitalWrite(r1,0);digitalWrite(y1,0);digitalWrite(g1,0);
}
Serial.println(rev);
}
}
String Receiver(){
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
// Message with a good checksum received, dump it.
//driver.printBuffer("Got:", buf, buflen);
String rev;
for (int i = 0; i < buflen;i++) {
rev += (char)buf[i];
}
Serial.println(rev);
delay(100);
return rev;
}
}