-
Notifications
You must be signed in to change notification settings - Fork 1
/
traffic_pole1.ino
177 lines (165 loc) · 3.34 KB
/
traffic_pole1.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
#include <Arduino.h>
#include <SoftwareSerial.h>
void send_density(void);
void light_command(char xd);
SoftwareSerial neighbour(8,7);
int obPin1 = A5; //Input pins
int obPin2 = A4;
int obPin3 = A3;
int obPin4 = A2;
int ledRed = 13; //Output pins
int ledYel = 12;
int ledGre = 11;
int ob1 = 0; //obstacle status
int ob2 = 0;
int ob3 = 0;
int ob4 = 0;
int yellowset=0;
char s=0;
void setup()
{
pinMode(obPin1, INPUT);
pinMode(obPin2, INPUT);
pinMode(obPin3, INPUT);
pinMode(obPin4, INPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledYel, OUTPUT);
pinMode(ledGre, OUTPUT);
Serial.begin(9600);
neighbour.begin(9600);
digitalWrite(ledRed, HIGH);
delay(500);
digitalWrite(ledRed, LOW);
digitalWrite(ledYel, HIGH);
delay(500);
digitalWrite(ledYel, LOW);
digitalWrite(ledGre, HIGH);
delay(500);
digitalWrite(ledGre,LOW);
}
int obcount=0;
uint8_t br=0;
uint8_t bg=0;
uint8_t rstate = 0 ;
uint8_t gstate = 0;
char rec=0;
void loop()
{
ob1 = digitalRead(obPin1);
ob2 = digitalRead(obPin2);
ob3 = digitalRead(obPin3);
ob4 = digitalRead(obPin4);
obcount=0;
if(ob1==1 || ob2==1 || ob3==1 || ob4==1)
{
//obcount=0;
if(ob1==1) obcount++;
if(ob2==1) obcount++;
if(ob3==1) obcount++;
if(ob4==1) obcount++;
}
if(neighbour.available()>0)
{
while(neighbour.available()>0)
{
char c = neighbour.read();
Serial.write(c);
}
}
if(br==1)
{
rstate ^= 1;
digitalWrite(ledRed,rstate);
delay(200);
//Blinking Red
}
if(bg==1)
{
gstate ^= 1;
digitalWrite(ledGre,gstate);
delay(200);
//Blinking Red
}
}
void serialEvent()
{
while(Serial.available()>0)
{
char data = Serial.read();
/*if(data=='?')
{
send_density();
neighbour.print('?');
}*/
if(data=='1')
{
delay(10);
data=Serial.read();
//Serial.print("Read command: ");
//Serial.print(data);
//Serial.println();
light_command(data);
}
else
{
neighbour.print(data);
}
}
}
void light_command(char xd)
{
char cr = xd;
//Serial.print("light command recieved: ");
//Serial.print(cr);
if(cr=='?')
{
send_density();
}
if(cr=='$' || cr=='R')
{
digitalWrite(ledGre,LOW);
//digitalWrite(ledYel,LOW);
digitalWrite(ledRed,HIGH);
digitalWrite(ledYel,LOW);
//s='r';
//Serial.println("Red went on ");
br=0;
bg=0;
}
if(cr=='%' || cr=='G')
{
br=0;
bg=0;
digitalWrite(ledRed,LOW);
//digitalWrite(ledYel,LOW);
digitalWrite(ledGre,HIGH);
digitalWrite(ledYel,LOW);
//s='g';
}
if(cr=='^' || cr=='Y')
{
digitalWrite(ledYel,HIGH);
}
if(cr==']')
{
digitalWrite(ledYel,LOW);
}
if(cr=='.' || bg==1) //Blinking green
{
digitalWrite(ledRed,LOW);
bg=1;
br=0;
}
if(cr=='+'|| br ==1)
{
digitalWrite(ledGre,LOW);
br=1;
bg=0; //Blinking Red
}
}
void send_density(void)
{
Serial.print('A');
Serial.print(obcount);
Serial.println();
}