-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaled_traffic.ino
187 lines (174 loc) · 6.04 KB
/
scaled_traffic.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
#include <IRremote.h>
//#define PIN_DETECT 2
IRsend irsend;
double sensor[4][3];
double a = 0, b = 0, c = 0, d= 0, maxi;
void setup()
{
// pinMode(PIN_DETECT, INPUT);
//initialize pins 22 - 33 as input
for(int i=18; i<29; i++){
pinMode(i, INPUT);
}
//initialize pins 34 - 45 as output
for(int i=34; i<46; i++){
pinMode(i, OUTPUT);
}
Serial.begin(115200);
Serial.println("READY");
// attachInterrupt(digitalPinToInterrupt(PIN_DETECT), checkIRBeamBreak, RISING);
/*
for(int i=2; i<4; i++){
attachInterrupt(digitalPinToInterrupt(i), checkIRBeamBreak, RISING);
}
*/
for(int i=18; i<22; i++){
attachInterrupt(digitalPinToInterrupt(i), checkIRBeamBreak, RISING);
}
// Note : the IR emitter is on PIN #3 on the Uno and on PIN #9 on the MEGA
irsend.enableIROut(38);
IREmitterOn();
}
// Use this function instead of delay() as delay() does not work in interrupt callbacks
void pause(int ms) {
// We need a loop as the largest value for delayMicroseconds that will produce an accurate delay is 16383
for (int i = 0; i < ms; i++) {
delayMicroseconds(1000);
}
}
void IREmitterOff() {
irsend.space(0);
pause(60); // 60 ms is OK for my TSOP but should be tuned
}
void IREmitterOn() {
irsend.mark(0);
pause(10);
}
void switchOffOnIREmitter() {
IREmitterOff();
IREmitterOn();
}
void checkIRBeamBreak() {
// int val = digitalRead(PIN_DETECT);
for (int i=18; i<29; i++){
for(int k=0; k<4; k++){
for(int j=0; j<3; j++){
sensor[k][j]= digitalRead(i);
if (sensor[k][j] == LOW){
return;
}
detachInterrupt(digitalPinToInterrupt(i));
switchOffOnIREmitter();
if(k==0 && sensor[k][j] == HIGH){
a++;
} else if (k==1 && sensor[k][j] == HIGH){
b++;
} else if (k==2 && sensor[k][j] == HIGH){
c++;
} else if (k==3 && sensor[k][j] == HIGH){
d++;
}
}
}
}
double maxi2, maxi3, maxi1, maxi4;
maxi2 = fmax(a,b);
maxi3 = fmax(maxi2,c);
maxi = fmax(maxi3,d);
if(maxi == a){
maxi4 = fmax(b, c);
maxi1 = fmax(maxi4, d);
if(maxi1 == b){
Serial.println("Arm 1 has highest traffic density followed by Arm 2");
digitalWrite(36, HIGH);
digitalWrite(38, HIGH);
digitalWrite(40, HIGH);
digitalWrite(43, HIGH);
} else if(maxi1 == c){
digitalWrite(36, HIGH);
digitalWrite(37, HIGH);
digitalWrite(41, HIGH);
digitalWrite(43, HIGH);
} else if (maxi1 == d){
digitalWrite(36, HIGH);
digitalWrite(37, HIGH);
digitalWrite(40, HIGH);
digitalWrite(44, HIGH);
}
} else if (maxi == b){
maxi4 = fmax(a, c);
maxi1 = fmax(maxi4, d);
if(maxi1 == a){
digitalWrite(35, HIGH);
digitalWrite(39, HIGH);
digitalWrite(40, HIGH);
digitalWrite(43, HIGH);
} else if (maxi1 == c){
digitalWrite(34, HIGH);
digitalWrite(39, HIGH);
digitalWrite(41, HIGH);
digitalWrite(43, HIGH);
} else if (maxi1 == d){
digitalWrite(34, HIGH);
digitalWrite(39, HIGH);
digitalWrite(40, HIGH);
digitalWrite(44, HIGH);
}
} else if (maxi == c){
maxi4 = fmax(b, a);
maxi1 = fmax(maxi4, d);
if (maxi1 == a){
digitalWrite(35, HIGH);
digitalWrite(37, HIGH);
digitalWrite(42, HIGH);
digitalWrite(43, HIGH);
} else if (maxi1 == b){
digitalWrite(34, HIGH);
digitalWrite(38, HIGH);
digitalWrite(42, HIGH);
digitalWrite(43, HIGH);
} else if (maxi == d){
digitalWrite(34, HIGH);
digitalWrite(37, HIGH);
digitalWrite(42, HIGH);
digitalWrite(44, HIGH);
}
} else if (maxi == d){
maxi4 = fmax(b, c);
maxi1 = fmax(maxi4, a);
if(maxi1 == a){
digitalWrite(35, HIGH);
digitalWrite(37, HIGH);
digitalWrite(40, HIGH);
digitalWrite(45, HIGH);
} else if (maxi1 == b){
digitalWrite(34, HIGH);
digitalWrite(38, HIGH);
digitalWrite(40, HIGH);
digitalWrite(45, HIGH);
} else if (maxi1 == c){
digitalWrite(34, HIGH);
digitalWrite(37, HIGH);
digitalWrite(41, HIGH);
digitalWrite(45, HIGH);
}
}
// LOW : no beam break
// HIGH : beam break
/*
if (val == LOW)
return;
detachInterrupt(digitalPinToInterrupt(PIN_DETECT));
switchOffOnIREmitter();
if (digitalRead(PIN_DETECT) == HIGH) {
Serial.println("IR BEAM BREAK !!!!");
}
*/
for(int i =18; i<29; i++){
attachInterrupt(digitalPinToInterrupt(i), checkIRBeamBreak, RISING);
}
}
void loop() {
checkIRBeamBreak();
delay(10000);
}