-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.v2.c
312 lines (272 loc) · 6.66 KB
/
main.v2.c
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
/* CAUTION : QUICK & DIRTY ! */
/* This Arduino program allow to control a traffic light (or three light) thanks to a bluetooth app on your mobile */
/* It contains many possibilities (mods) to enjoy your traffic light */
/* I used it with a 4 chanels relay and an Arduino Uno + A Nexus 4 */
/* Author : Quentin HESS */
/* Date of creation : 2015.10.20 */
/* Comments : Should be ehanced */
#include <SoftwareSerial.h>
/* CONFIGURATION */
// define pin heads
const int red = 10;
const int orange = 9;
const int green = 8;
const int btrx = 6;
const int bttx = 7;
/* mode variables
10 is normal (red, green then orange and etc) 100 -> 010 -> 001
20 is outage (orange blinking) 010 -> 000
30 is go_carefully (green blinking) 100 -> 000
40 is trains_stop (red blinking) 001 -> 000
5X is manual (green (52) or red (50))
- 50 : XXX -> 001
- 52 : XXX -> 100
6X is full_manual (green (62), orange (61), red (60))
- 60 : 001
- 61 : 010
- 62 : 100
- 63 : 011
- 64 : 101
- 65 : 110
- 66 : 111
7X delays (reduce/rise green: 70/71, orange: 72/73, red: 74/75, blink: 76/77))
*/
int newmod = 10; // functionnal default mod
/* Delays */
long delay_green = 10000; // delay for green
long delay_red = 10000; // delay for red
long delay_orange = 3000; // delay for orange
int delay_blink = 600; // delay for blinking
/* relays_conf */
const bool down = HIGH;
const bool up = LOW;
/* END CONFIGURATION */
int current_light = 99; // all is down by default
long lastchange = 0; // time variables in ms, don't touch it
int mod = 99; // technical default mod
bool binblink = 0; // just for blinking
long current_delay = delay_red;
int ins;
// Create custom serial pin instance
SoftwareSerial mySerial(bttx, btrx); // RX, TX
/* Here we are ! */
void setup() {
// Initialize Serial Connection for debug
Serial.begin(9600); // open serial connection
Serial.println("Start of TL Arduino");
// Initialize BT Connection for instructions
mySerial.begin(9600);
mySerial.println("Open BT connection");
// Initialize relay pins
pinMode(red, OUTPUT); // set pin on output
pinMode(orange, OUTPUT); // set pin on output
pinMode(green, OUTPUT); // set pin on output
// Initialize relays
digitalWrite(green, down);
digitalWrite(orange, down);
digitalWrite(red, down);
// Load the default mode
switchmod(newmod);
}
void loop() {
// call function off the current mod
Serial.print(current_light);
Serial.print(",");
Serial.print(digitalRead(red));
Serial.print(",");
Serial.print(digitalRead(orange));
Serial.print(",");
Serial.print(digitalRead(green));
Serial.print("\n");
if (mySerial.available()) {
ins=(int) mySerial.read();
Serial.print("Receive : ");
Serial.print(ins);
Serial.print(" EOC");
Serial.print("\n");
if(ins == 10 || ins == 20 || ins == 30 || ins == 40 || ins == 50 || ins == 52 || ins == 60 || ins == 61 || ins == 62 || ins == 63 || ins == 64 || ins == 65 || ins == 66) {
newmod = ins;
}
else if(ins == 70 && delay_green >= 2000) {
delay_green -= 1000;
}
else if(ins == 71 && delay_green < 1000000) {
delay_green += 1000;
}
else if(ins == 72 && delay_orange >= 2000) {
delay_orange -= 1000;
}
else if(ins == 73 && delay_orange < 1000000) {
delay_orange += 1000;
}
else if(ins == 74 && delay_red >= 2000) {
delay_red -= 1000;
}
else if(ins == 75 && delay_red < 1000000) {
delay_red += 1000;
}
else if(ins == 76 && delay_blink >= 500) {
delay_blink -= 200;
}
else if(ins == 77 && delay_blink < 10000) {
delay_blink += 200;
}
}
switchmod(newmod);
switch (mod) {
case 10:
normal();
break;
case 30:
go_carefully();
break;
case 40:
trains_stop();
break;
case 50:
manual(red);
break;
case 52:
manual(green);
break;
case 60:
full_manual(red);
break;
case 61:
full_manual(orange);
break;
case 62:
full_manual(green);
break;
case 63:
switchon(orange);
switchon(red);
break;
case 63:
switchon(green);
switchon(red);
break;
case 63:
switchon(green);
switchon(orange);
break;
case 63:
switchon(green);
switchon(orange);
switchon(red);
break;
default:
outage();
break;
}
delay(100);
}
/* function for initializing each mod */
void switchmod(int newmod) {
if(newmod != mod) {
mod = newmod;
Serial.print("Switch to mod : ");
Serial.print(mod);
Serial.print("\n");
switch(newmod) {
case 10:
changeto(red);
break;
case 20:
case 30:
case 40:
case 60:
case 61:
case 62:
case 63:
case 64:
case 65:
case 66:
shutdownall();
break;
case 50:
case 52:
current_delay = 0;
break;
}
}
}
/* list off mods */
void normal() {
if(millis() > (lastchange + current_delay)) {
lastchange = millis();
current_delay = nextlight();
}
/*Serial.println(lastchange);
Serial.println(current_delay);
Serial.println("===");*/
}
void go_carefully() {
blinklight(green);
}
void trains_stop() {
blinklight(red);
}
void outage() {
blinklight(orange);
}
void manual(int manual_light) {
if(current_light != manual_light) {
if(millis() > (lastchange + current_delay)) {
lastchange = millis();
current_delay = nextlight();
}
}
}
void full_manual(int fmanual_light) {
changeto(fmanual_light);
}
/* go to the next light */
int nextlight() {
long delayreturn = 0;
switch (current_light) {
case red:
changeto(green);
delayreturn = delay_green;
break;
case orange:
changeto(red);
delayreturn = delay_red;
break;
case green:
changeto(orange);
delayreturn = delay_orange;
break;
}
return delayreturn;
}
/* blink a light */
void blinklight(int light) {
if(binblink) {
current_light = light;
}
else {
current_light = 99;
}
if(millis() > (lastchange + delay_blink)) {
lastchange = millis();
binblink = !binblink;
digitalWrite(light, binblink);
}
}
void shutdownall() {
if(Serial.print(digitalRead(red))) { digitalWrite(red, down); }
if(Serial.print(digitalRead(green))) { digitalWrite(green, down); }
if(Serial.print(digitalRead(orange))) { digitalWrite(orange, down); }
current_light = 99;
}
void switchon(int light) {
digitalWrite(light, up);
}
void changeto(int light) {
if(light != current_light) {
shutdownall();
switchon(light);
current_light=light;
}
}