Traffic lights w/advanced green light and pedestrian light
A more advanced version than usual.
Components and supplies
Tactile Switch, Top Actuated
5 mm LED: Red
Through Hole Resistor, 10 kohm
Through Hole Resistor, 470 ohm
Through Hole Resistor, 330 ohm
General Purpose Transistor NPN
9V battery (generic)
LED, Orange
5 mm LED: Green
5 mm LED: Yellow
Through Hole Resistor, 47 ohm
Through Hole Resistor, 1 kohm
High Brightness LED, White
Arduino UNO
Tools and machines
Breadboard, 830 Tie Points
A lot of wires
Breadboard, 830 Tie Points
Breadboard, 830 Tie Points
Breadboard, 830 Tie Points
Project description
Code
Master Arduino
arduino
1#include <Wire.h> 2 3//Slave 1 Assignation 4int EastRed = 0; 5int 6 EastYellow = 1; 7int EastGreenUp = 2; 8int EastGreenRight = 3; 9int EastLeftRed 10 = 4; 11int EastLeftYellow = 5; 12int EastLeftGreen = 6; 13int NorthRed = 7; 14int 15 NorthYellow = 8; 16int NorthGreenUp = 9; 17int NorthGreenRight = 10; 18int NorthLeftRed 19 = 11; 20int NorthLeftYellow = 12; 21int NorthLeftGreen = 13; 22int WestOrange 23 = 14; 24int WestWhite = 15; 25int NorthOrange = 16; 26int NorthWhite = 17; 27 28//Slave 29 2 Assignation 30int WestRed = 0; 31int WestYellow = 1; 32int WestGreenUp = 2; 33int 34 WestGreenRight = 3; 35int WestLeftRed = 4; 36int WestLeftYellow = 5; 37int WestLeftGreen 38 = 6; 39int SouthRed = 7; 40int SouthYellow = 8; 41int SouthGreenUp = 9; 42int 43 SouthGreenRight = 10; 44int SouthLeftRed = 11; 45int SouthLeftYellow = 12; 46int 47 SouthLeftGreen = 13; 48int EastOrange = 14; 49int EastWhite = 15; 50int SouthOrange 51 = 16; 52int SouthWhite = 17; 53 54//Time Assignations 55int PedestrianCrossingTime 56 = 6000; 57int NorthSouthGreenTime = 15000; 58int EastWestGreenTime = 15000; 59int 60 AdvanceGreenTime = 10000; 61int YellowTime = 3000; 62int InitialFlashingTime = 63 5000; 64int PedestrianFlashingTime = 4000; 65int BufferTime = 1000; 66int LongFlashTime 67 = 500; 68int ShorthFlashTime = 250; 69 70//Other Assignations 71int QtyInitialFlash 72 = InitialFlashingTime / LongFlashTime; 73int QtyPedestrianFlash = PedestrianFlashingTime 74 / ShorthFlashTime; 75unsigned long ActionTime; 76int PedestrianButton = A0; 77int 78 ButtonState; 79int PedestrianCall = 0; 80int i; 81byte Slave1Values[] = {0, 0, 82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 83byte Slave2Values[] = {0, 0, 84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 85 86void setup() { 87 88 Wire.begin(); 89 90 delay(50); 91 //Initial orange permanent lights (pedestrian) on four sides and 92 red flashing lights on four sides 93 Slave1Values[WestOrange] = 1; 94 Slave1Values[NorthOrange] 95 = 1; 96 Slave2Values[EastOrange] = 1; 97 Slave2Values[SouthOrange] = 1; 98 99 for (i = 0; i < QtyInitialFlash / 2; i++) { 100 Slave1Values[EastRed] = 1; 101 102 Slave1Values[EastLeftRed] = 1; 103 Slave1Values[NorthRed] = 1; 104 Slave1Values[NorthLeftRed] 105 = 1; 106 Slave2Values[WestRed] = 1; 107 Slave2Values[WestLeftRed] = 1; 108 109 Slave2Values[SouthRed] = 1; 110 Slave2Values[SouthLeftRed] = 1; 111 SlavesActions(); 112 113 delay(LongFlashTime); 114 Slave1Values[EastRed] = 0; 115 Slave1Values[EastLeftRed] 116 = 0; 117 Slave1Values[NorthRed] = 0; 118 Slave1Values[NorthLeftRed] = 0; 119 120 Slave2Values[WestRed] = 0; 121 Slave2Values[WestLeftRed] = 0; 122 Slave2Values[SouthRed] 123 = 0; 124 Slave2Values[SouthLeftRed] = 0; 125 SlavesActions(); 126 delay(LongFlashTime); 127 128 } 129 130 //Initial red on East-West, green on North-South 131 Slave1Values[EastRed] 132 = 1; 133 Slave2Values[WestRed] = 1; 134 Slave1Values[EastLeftRed] = 1; 135 Slave2Values[WestLeftRed] 136 = 1; 137 Slave1Values[NorthLeftRed] = 1; 138 Slave2Values[SouthLeftRed] = 1; 139 140 Slave1Values[NorthGreenUp] = 1; 141 Slave1Values[NorthGreenRight] = 1; 142 Slave2Values[SouthGreenUp] 143 = 1; 144 Slave2Values[SouthGreenRight] = 1; 145 SlavesActions(); 146 147} 148 149void 150 SlavesActions() { 151 152 Wire.beginTransmission(1); 153 Wire.write((byte*)Slave1Values, 154 18); 155 Wire.endTransmission(); 156 Wire.beginTransmission(2); 157 Wire.write((byte*)Slave2Values, 158 18); 159 Wire.endTransmission(); 160 161} 162 163void loop() { 164 165 //End of 166 Cycle,Green lights North-South 167 ActionTime = millis(); 168 do { 169 PedestrianVerification(); 170 171 } while (millis() - ActionTime < NorthSouthGreenTime); 172 173 //Yellow lights 174 cycle for North 175 Slave1Values[NorthGreenUp] = 0; 176 Slave1Values[NorthGreenRight] 177 = 0; 178 Slave1Values[NorthYellow] = 1; 179 SlavesActions(); 180 ActionTime = 181 millis(); 182 do { 183 PedestrianVerification(); 184 } while (millis() - ActionTime 185 < YellowTime); 186 187 //Buffer cycle before advance green light South 188 Slave1Values[NorthYellow] 189 = 0; 190 Slave1Values[NorthRed] = 1; 191 SlavesActions(); 192 ActionTime = millis(); 193 194 do { 195 PedestrianVerification(); 196 } while (millis() - ActionTime < BufferTime); 197 198 199 //Advance Green light cycle for South 200 Slave2Values[SouthLeftRed] = 201 0; 202 Slave2Values[SouthLeftGreen] = 1; 203 SlavesActions(); 204 ActionTime 205 = millis(); 206 do { 207 PedestrianVerification(); 208 } while (millis() - 209 ActionTime < AdvanceGreenTime); 210 211 //Yellow light cycle for South 212 Slave2Values[SouthLeftGreen] 213 = 0; 214 Slave2Values[SouthGreenUp] = 0; 215 Slave2Values[SouthGreenRight] = 0; 216 217 Slave2Values[SouthLeftYellow] = 1; 218 Slave2Values[SouthYellow] = 1; 219 SlavesActions(); 220 221 ActionTime = millis(); 222 do { 223 PedestrianVerification(); 224 } while 225 (millis() - ActionTime < YellowTime); 226 227 //Buffer Cycle before Green lights 228 East-West 229 Slave2Values[SouthLeftYellow] = 0; 230 Slave2Values[SouthYellow] 231 = 0; 232 Slave2Values[SouthLeftRed] = 1; 233 Slave2Values[SouthRed] = 1; 234 SlavesActions(); 235 236 ActionTime = millis(); 237 do { 238 PedestrianVerification(); 239 } while (millis() 240 - ActionTime < BufferTime); 241 242 //Pedestrian Cycle Verification 243 if (PedestrianCall 244 == 1) { 245 PedestrianCycle(); 246 } 247 248 //Advance green light cycle 249 for East 250 Slave1Values[EastLeftRed] = 0; 251 Slave1Values[EastRed] = 0; 252 253 Slave1Values[EastLeftGreen] = 1; 254 Slave1Values[EastGreenUp] = 1; 255 Slave1Values[EastGreenRight] 256 = 1; 257 SlavesActions(); 258 ActionTime = millis(); 259 do { 260 PedestrianVerification(); 261 262 } while (millis() - ActionTime < AdvanceGreenTime); 263 264 //Yellow light 265 cycle for Left East 266 Slave1Values[EastLeftGreen] = 0; 267 Slave1Values[EastLeftYellow] 268 = 1; 269 SlavesActions(); 270 ActionTime = millis(); 271 do { 272 PedestrianVerification(); 273 274 } while (millis() - ActionTime < YellowTime); 275 276 //Buffer cycle before 277 green lights East-West 278 Slave1Values[EastLeftYellow] = 0; 279 Slave1Values[EastLeftRed] 280 = 1; 281 SlavesActions(); 282 ActionTime = millis(); 283 do { 284 PedestrianVerification(); 285 286 } while (millis() - ActionTime < BufferTime); 287 288 //Green lights cycle 289 for East-West 290 Slave2Values[WestRed] = 0; 291 Slave2Values[WestGreenUp] = 1; 292 293 Slave2Values[WestGreenRight] = 1; 294 SlavesActions(); 295 ActionTime = millis(); 296 297 do { 298 PedestrianVerification(); 299 } while (millis() - ActionTime < EastWestGreenTime); 300 301 302 //Yellow light cycle for East 303 Slave1Values[EastGreenUp] = 0; 304 Slave1Values[EastGreenRight] 305 = 0; 306 Slave1Values[EastYellow] = 1; 307 SlavesActions(); 308 ActionTime = 309 millis(); 310 do { 311 PedestrianVerification(); 312 } while (millis() - ActionTime 313 < YellowTime); 314 315 //Buffer cycle before advance green light West 316 Slave1Values[EastYellow] 317 = 0; 318 Slave1Values[EastRed] = 1; 319 SlavesActions(); 320 ActionTime = millis(); 321 322 do { 323 PedestrianVerification(); 324 } while (millis() - ActionTime < BufferTime); 325 326 327 //Advance Green light cycle for West 328 Slave2Values[WestLeftRed] = 0; 329 330 Slave2Values[WestLeftGreen] = 1; 331 SlavesActions(); 332 ActionTime = millis(); 333 334 do { 335 PedestrianVerification(); 336 } while (millis() - ActionTime < AdvanceGreenTime); 337 338 339 //Yellow light cycle for West 340 Slave2Values[WestLeftGreen] = 0; 341 342 Slave2Values[WestGreenUp] = 0; 343 Slave2Values[WestGreenRight] = 0; 344 Slave2Values[WestLeftYellow] 345 = 1; 346 Slave2Values[WestYellow] = 1; 347 SlavesActions(); 348 ActionTime = 349 millis(); 350 do { 351 PedestrianVerification(); 352 } while (millis() - ActionTime 353 < YellowTime); 354 355 //Buffer Cycle before Green lights North-South 356 Slave2Values[WestLeftYellow] 357 = 0; 358 Slave2Values[WestYellow] = 0; 359 Slave2Values[WestLeftRed] = 1; 360 361 Slave2Values[WestRed] = 1; 362 SlavesActions(); 363 ActionTime = millis(); 364 365 do { 366 PedestrianVerification(); 367 } while (millis() - ActionTime < BufferTime); 368 369 370 //Pedestrian Cycle Verification 371 if (PedestrianCall == 1) { 372 PedestrianCycle(); 373 374 } 375 376 //Advance Green light cycle for North 377 Slave1Values[NorthLeftRed] 378 = 0; 379 Slave1Values[NorthRed] = 0; 380 Slave1Values[NorthLeftGreen] = 1; 381 382 Slave1Values[NorthGreenUp] = 1; 383 Slave1Values[NorthGreenRight] = 1; 384 SlavesActions(); 385 386 ActionTime = millis(); 387 do { 388 PedestrianVerification(); 389 } while 390 (millis() - ActionTime < AdvanceGreenTime); 391 392 //Yellow light cycle for 393 left North 394 Slave1Values[NorthLeftGreen] = 0; 395 Slave1Values[NorthLeftYellow] 396 = 1; 397 SlavesActions(); 398 ActionTime = millis(); 399 do { 400 PedestrianVerification(); 401 402 } while (millis() - ActionTime < YellowTime); 403 404 //Buffer Cycle before 405 Green lights North-South 406 Slave1Values[NorthLeftYellow] = 0; 407 Slave1Values[NorthLeftRed] 408 = 1; 409 SlavesActions(); 410 ActionTime = millis(); 411 do { 412 PedestrianVerification(); 413 414 } while (millis() - ActionTime < BufferTime); 415 416 //Start of Cycle,Green 417 lights North-South 418 Slave2Values[SouthGreenUp] = 1; 419 Slave2Values[SouthGreenRight] 420 = 1; 421 Slave2Values[SouthRed] = 0; 422 SlavesActions(); 423 424} 425 426void 427 PedestrianVerification() { 428 429 delay(25); 430 ButtonState = digitalRead(PedestrianButton); 431 432 if (ButtonState == HIGH) { 433 PedestrianCall = 1; 434 } 435 436} 437 438void 439 PedestrianCycle() { 440 441 //White lights cycle 442 Slave1Values[WestOrange] 443 = 0; 444 Slave1Values[NorthOrange] = 0; 445 Slave2Values[EastOrange] = 0; 446 447 Slave2Values[SouthOrange] = 0; 448 Slave1Values[WestWhite] = 1; 449 Slave1Values[NorthWhite] 450 = 1; 451 Slave2Values[EastWhite] = 1; 452 Slave2Values[SouthWhite] = 1; 453 SlavesActions(); 454 455 delay(PedestrianCrossingTime); 456 Slave1Values[WestWhite] = 0; 457 Slave1Values[NorthWhite] 458 = 0; 459 Slave2Values[EastWhite] = 0; 460 Slave2Values[SouthWhite] = 0; 461 SlavesActions(); 462 463 for (i = 0; i < QtyPedestrianFlash / 2; i++) { 464 Slave1Values[WestOrange] 465 = 1; 466 Slave1Values[NorthOrange] = 1; 467 Slave2Values[EastOrange] = 1; 468 469 Slave2Values[SouthOrange] = 1; 470 SlavesActions(); 471 delay(ShorthFlashTime); 472 473 Slave1Values[WestOrange] = 0; 474 Slave1Values[NorthOrange] = 0; 475 Slave2Values[EastOrange] 476 = 0; 477 Slave2Values[SouthOrange] = 0; 478 SlavesActions(); 479 delay(ShorthFlashTime); 480 481 } 482 Slave1Values[WestOrange] = 1; 483 Slave1Values[NorthOrange] = 1; 484 Slave2Values[EastOrange] 485 = 1; 486 Slave2Values[SouthOrange] = 1; 487 SlavesActions(); 488 delay(BufferTime); 489 490 PedestrianCall = 0; 491 492}
Slave Arduino #1
arduino
1#include <Wire.h> 2 3int i = 0; 4int Event = 0; 5byte SlaveValues[18]; 6 7void 8 setup() { 9 10 for (i = 0; i <= 13; i++) { 11 pinMode(i, OUTPUT); 12 13 } 14 pinMode(A0, OUTPUT); 15 pinMode(A1, OUTPUT); 16 pinMode(A2, OUTPUT); 17 18 pinMode(A3, OUTPUT); 19 Wire.begin(1); 20 Wire.onReceive(receiveEvent); 21 22 23} 24 25void receiveEvent(int howMany) { 26 27 for (i = 0; i < howMany; 28 i++) 29 { 30 SlaveValues[i] = Wire.read(); 31 } 32 Event = 1; 33 34} 35 36void 37 loop() { 38 39 if (Event == 1) { 40 41 if (SlaveValues[0] == 0) { 42 digitalWrite(0, 43 LOW); 44 } 45 else { 46 digitalWrite(0, HIGH); 47 } 48 if 49 (SlaveValues[1] == 0) { 50 digitalWrite(1, LOW); 51 } 52 else { 53 54 digitalWrite(1, HIGH); 55 } 56 if (SlaveValues[2] == 0) { 57 digitalWrite(2, 58 LOW); 59 } 60 else { 61 digitalWrite(2, HIGH); 62 } 63 if 64 (SlaveValues[3] == 0) { 65 digitalWrite(3, LOW); 66 } 67 else { 68 69 digitalWrite(3, HIGH); 70 } 71 if (SlaveValues[4] == 0) { 72 digitalWrite(4, 73 LOW); 74 } 75 else { 76 digitalWrite(4, HIGH); 77 } 78 if 79 (SlaveValues[5] == 0) { 80 digitalWrite(5, LOW); 81 } 82 else { 83 84 digitalWrite(5, HIGH); 85 } 86 if (SlaveValues[6] == 0) { 87 digitalWrite(6, 88 LOW); 89 } 90 else { 91 digitalWrite(6, HIGH); 92 } 93 if 94 (SlaveValues[7] == 0) { 95 digitalWrite(7, LOW); 96 } 97 else { 98 99 digitalWrite(7, HIGH); 100 } 101 if (SlaveValues[8] == 0) { 102 digitalWrite(8, 103 LOW); 104 } 105 else { 106 digitalWrite(8, HIGH); 107 } 108 if 109 (SlaveValues[9] == 0) { 110 digitalWrite(9, LOW); 111 } 112 else { 113 114 digitalWrite(9, HIGH); 115 } 116 if (SlaveValues[10] == 0) { 117 digitalWrite(10, 118 LOW); 119 } 120 else { 121 digitalWrite(10, HIGH); 122 } 123 if 124 (SlaveValues[11] == 0) { 125 digitalWrite(11, LOW); 126 } 127 else { 128 129 digitalWrite(11, HIGH); 130 } 131 if (SlaveValues[12] == 0) { 132 digitalWrite(12, 133 LOW); 134 } 135 else { 136 digitalWrite(12, HIGH); 137 } 138 if 139 (SlaveValues[13] == 0) { 140 digitalWrite(13, LOW); 141 } 142 else { 143 144 digitalWrite(13, HIGH); 145 } 146 if (SlaveValues[14] == 0) { 147 digitalWrite(A0, 148 LOW); 149 } 150 else { 151 digitalWrite(A0, HIGH); 152 } 153 if 154 (SlaveValues[15] == 0) { 155 digitalWrite(A1, LOW); 156 } 157 else { 158 159 digitalWrite(A1, HIGH); 160 } 161 if (SlaveValues[16] == 0) { 162 digitalWrite(A2, 163 LOW); 164 } 165 else { 166 digitalWrite(A2, HIGH); 167 } 168 if 169 (SlaveValues[17] == 0) { 170 digitalWrite(A3, LOW); 171 } 172 else { 173 174 digitalWrite(A3, HIGH); 175 } 176 Event = 0; 177 } 178 179}
Slave Arduino #2
arduino
1#include <Wire.h> 2 3int i = 0; 4int Event = 0; 5byte SlaveValues[18]; 6 7void 8 setup() { 9 10 for (i = 0; i <= 13; i++) { 11 pinMode(i, OUTPUT); 12 13 } 14 pinMode(A0, OUTPUT); 15 pinMode(A1, OUTPUT); 16 pinMode(A2, OUTPUT); 17 18 pinMode(A3, OUTPUT); 19 Wire.begin(2); 20 Wire.onReceive(receiveEvent); 21 22 23} 24 25void receiveEvent(int howMany) { 26 27 for (i = 0; i < howMany; 28 i++) 29 { 30 SlaveValues[i] = Wire.read(); 31 } 32 Event = 1; 33 34} 35 36void 37 loop() { 38 39 if (Event == 1) { 40 41 if (SlaveValues[0] == 0) { 42 digitalWrite(0, 43 LOW); 44 } 45 else { 46 digitalWrite(0, HIGH); 47 } 48 if 49 (SlaveValues[1] == 0) { 50 digitalWrite(1, LOW); 51 } 52 else { 53 54 digitalWrite(1, HIGH); 55 } 56 if (SlaveValues[2] == 0) { 57 digitalWrite(2, 58 LOW); 59 } 60 else { 61 digitalWrite(2, HIGH); 62 } 63 if 64 (SlaveValues[3] == 0) { 65 digitalWrite(3, LOW); 66 } 67 else { 68 69 digitalWrite(3, HIGH); 70 } 71 if (SlaveValues[4] == 0) { 72 digitalWrite(4, 73 LOW); 74 } 75 else { 76 digitalWrite(4, HIGH); 77 } 78 if 79 (SlaveValues[5] == 0) { 80 digitalWrite(5, LOW); 81 } 82 else { 83 84 digitalWrite(5, HIGH); 85 } 86 if (SlaveValues[6] == 0) { 87 digitalWrite(6, 88 LOW); 89 } 90 else { 91 digitalWrite(6, HIGH); 92 } 93 if 94 (SlaveValues[7] == 0) { 95 digitalWrite(7, LOW); 96 } 97 else { 98 99 digitalWrite(7, HIGH); 100 } 101 if (SlaveValues[8] == 0) { 102 digitalWrite(8, 103 LOW); 104 } 105 else { 106 digitalWrite(8, HIGH); 107 } 108 if 109 (SlaveValues[9] == 0) { 110 digitalWrite(9, LOW); 111 } 112 else { 113 114 digitalWrite(9, HIGH); 115 } 116 if (SlaveValues[10] == 0) { 117 digitalWrite(10, 118 LOW); 119 } 120 else { 121 digitalWrite(10, HIGH); 122 } 123 if 124 (SlaveValues[11] == 0) { 125 digitalWrite(11, LOW); 126 } 127 else { 128 129 digitalWrite(11, HIGH); 130 } 131 if (SlaveValues[12] == 0) { 132 digitalWrite(12, 133 LOW); 134 } 135 else { 136 digitalWrite(12, HIGH); 137 } 138 if 139 (SlaveValues[13] == 0) { 140 digitalWrite(13, LOW); 141 } 142 else { 143 144 digitalWrite(13, HIGH); 145 } 146 if (SlaveValues[14] == 0) { 147 digitalWrite(A0, 148 LOW); 149 } 150 else { 151 digitalWrite(A0, HIGH); 152 } 153 if 154 (SlaveValues[15] == 0) { 155 digitalWrite(A1, LOW); 156 } 157 else { 158 159 digitalWrite(A1, HIGH); 160 } 161 if (SlaveValues[16] == 0) { 162 digitalWrite(A2, 163 LOW); 164 } 165 else { 166 digitalWrite(A2, HIGH); 167 } 168 if 169 (SlaveValues[17] == 0) { 170 digitalWrite(A3, LOW); 171 } 172 else { 173 174 digitalWrite(A3, HIGH); 175 } 176 Event = 0; 177 } 178 179}
Slave Arduino #2
arduino
1#include <Wire.h> 2 3int i = 0; 4int Event = 0; 5byte SlaveValues[18]; 6 7void setup() { 8 9 for (i = 0; i <= 13; i++) { 10 pinMode(i, OUTPUT); 11 } 12 pinMode(A0, OUTPUT); 13 pinMode(A1, OUTPUT); 14 pinMode(A2, OUTPUT); 15 pinMode(A3, OUTPUT); 16 Wire.begin(2); 17 Wire.onReceive(receiveEvent); 18 19} 20 21void receiveEvent(int howMany) { 22 23 for (i = 0; i < howMany; i++) 24 { 25 SlaveValues[i] = Wire.read(); 26 } 27 Event = 1; 28 29} 30 31void loop() { 32 33 if (Event == 1) { 34 35 if (SlaveValues[0] == 0) { 36 digitalWrite(0, LOW); 37 } 38 else { 39 digitalWrite(0, HIGH); 40 } 41 if (SlaveValues[1] == 0) { 42 digitalWrite(1, LOW); 43 } 44 else { 45 digitalWrite(1, HIGH); 46 } 47 if (SlaveValues[2] == 0) { 48 digitalWrite(2, LOW); 49 } 50 else { 51 digitalWrite(2, HIGH); 52 } 53 if (SlaveValues[3] == 0) { 54 digitalWrite(3, LOW); 55 } 56 else { 57 digitalWrite(3, HIGH); 58 } 59 if (SlaveValues[4] == 0) { 60 digitalWrite(4, LOW); 61 } 62 else { 63 digitalWrite(4, HIGH); 64 } 65 if (SlaveValues[5] == 0) { 66 digitalWrite(5, LOW); 67 } 68 else { 69 digitalWrite(5, HIGH); 70 } 71 if (SlaveValues[6] == 0) { 72 digitalWrite(6, LOW); 73 } 74 else { 75 digitalWrite(6, HIGH); 76 } 77 if (SlaveValues[7] == 0) { 78 digitalWrite(7, LOW); 79 } 80 else { 81 digitalWrite(7, HIGH); 82 } 83 if (SlaveValues[8] == 0) { 84 digitalWrite(8, LOW); 85 } 86 else { 87 digitalWrite(8, HIGH); 88 } 89 if (SlaveValues[9] == 0) { 90 digitalWrite(9, LOW); 91 } 92 else { 93 digitalWrite(9, HIGH); 94 } 95 if (SlaveValues[10] == 0) { 96 digitalWrite(10, LOW); 97 } 98 else { 99 digitalWrite(10, HIGH); 100 } 101 if (SlaveValues[11] == 0) { 102 digitalWrite(11, LOW); 103 } 104 else { 105 digitalWrite(11, HIGH); 106 } 107 if (SlaveValues[12] == 0) { 108 digitalWrite(12, LOW); 109 } 110 else { 111 digitalWrite(12, HIGH); 112 } 113 if (SlaveValues[13] == 0) { 114 digitalWrite(13, LOW); 115 } 116 else { 117 digitalWrite(13, HIGH); 118 } 119 if (SlaveValues[14] == 0) { 120 digitalWrite(A0, LOW); 121 } 122 else { 123 digitalWrite(A0, HIGH); 124 } 125 if (SlaveValues[15] == 0) { 126 digitalWrite(A1, LOW); 127 } 128 else { 129 digitalWrite(A1, HIGH); 130 } 131 if (SlaveValues[16] == 0) { 132 digitalWrite(A2, LOW); 133 } 134 else { 135 digitalWrite(A2, HIGH); 136 } 137 if (SlaveValues[17] == 0) { 138 digitalWrite(A3, LOW); 139 } 140 else { 141 digitalWrite(A3, HIGH); 142 } 143 Event = 0; 144 } 145 146}
Master Arduino
arduino
1#include <Wire.h> 2 3//Slave 1 Assignation 4int EastRed = 0; 5int EastYellow = 1; 6int EastGreenUp = 2; 7int EastGreenRight = 3; 8int EastLeftRed = 4; 9int EastLeftYellow = 5; 10int EastLeftGreen = 6; 11int NorthRed = 7; 12int NorthYellow = 8; 13int NorthGreenUp = 9; 14int NorthGreenRight = 10; 15int NorthLeftRed = 11; 16int NorthLeftYellow = 12; 17int NorthLeftGreen = 13; 18int WestOrange = 14; 19int WestWhite = 15; 20int NorthOrange = 16; 21int NorthWhite = 17; 22 23//Slave 2 Assignation 24int WestRed = 0; 25int WestYellow = 1; 26int WestGreenUp = 2; 27int WestGreenRight = 3; 28int WestLeftRed = 4; 29int WestLeftYellow = 5; 30int WestLeftGreen = 6; 31int SouthRed = 7; 32int SouthYellow = 8; 33int SouthGreenUp = 9; 34int SouthGreenRight = 10; 35int SouthLeftRed = 11; 36int SouthLeftYellow = 12; 37int SouthLeftGreen = 13; 38int EastOrange = 14; 39int EastWhite = 15; 40int SouthOrange = 16; 41int SouthWhite = 17; 42 43//Time Assignations 44int PedestrianCrossingTime = 6000; 45int NorthSouthGreenTime = 15000; 46int EastWestGreenTime = 15000; 47int AdvanceGreenTime = 10000; 48int YellowTime = 3000; 49int InitialFlashingTime = 5000; 50int PedestrianFlashingTime = 4000; 51int BufferTime = 1000; 52int LongFlashTime = 500; 53int ShorthFlashTime = 250; 54 55//Other Assignations 56int QtyInitialFlash = InitialFlashingTime / LongFlashTime; 57int QtyPedestrianFlash = PedestrianFlashingTime / ShorthFlashTime; 58unsigned long ActionTime; 59int PedestrianButton = A0; 60int ButtonState; 61int PedestrianCall = 0; 62int i; 63byte Slave1Values[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 64byte Slave2Values[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 65 66void setup() { 67 68 Wire.begin(); 69 delay(50); 70 //Initial orange permanent lights (pedestrian) on four sides and red flashing lights on four sides 71 Slave1Values[WestOrange] = 1; 72 Slave1Values[NorthOrange] = 1; 73 Slave2Values[EastOrange] = 1; 74 Slave2Values[SouthOrange] = 1; 75 for (i = 0; i < QtyInitialFlash / 2; i++) { 76 Slave1Values[EastRed] = 1; 77 Slave1Values[EastLeftRed] = 1; 78 Slave1Values[NorthRed] = 1; 79 Slave1Values[NorthLeftRed] = 1; 80 Slave2Values[WestRed] = 1; 81 Slave2Values[WestLeftRed] = 1; 82 Slave2Values[SouthRed] = 1; 83 Slave2Values[SouthLeftRed] = 1; 84 SlavesActions(); 85 delay(LongFlashTime); 86 Slave1Values[EastRed] = 0; 87 Slave1Values[EastLeftRed] = 0; 88 Slave1Values[NorthRed] = 0; 89 Slave1Values[NorthLeftRed] = 0; 90 Slave2Values[WestRed] = 0; 91 Slave2Values[WestLeftRed] = 0; 92 Slave2Values[SouthRed] = 0; 93 Slave2Values[SouthLeftRed] = 0; 94 SlavesActions(); 95 delay(LongFlashTime); 96 } 97 98 //Initial red on East-West, green on North-South 99 Slave1Values[EastRed] = 1; 100 Slave2Values[WestRed] = 1; 101 Slave1Values[EastLeftRed] = 1; 102 Slave2Values[WestLeftRed] = 1; 103 Slave1Values[NorthLeftRed] = 1; 104 Slave2Values[SouthLeftRed] = 1; 105 Slave1Values[NorthGreenUp] = 1; 106 Slave1Values[NorthGreenRight] = 1; 107 Slave2Values[SouthGreenUp] = 1; 108 Slave2Values[SouthGreenRight] = 1; 109 SlavesActions(); 110 111} 112 113void SlavesActions() { 114 115 Wire.beginTransmission(1); 116 Wire.write((byte*)Slave1Values, 18); 117 Wire.endTransmission(); 118 Wire.beginTransmission(2); 119 Wire.write((byte*)Slave2Values, 18); 120 Wire.endTransmission(); 121 122} 123 124void loop() { 125 126 //End of Cycle,Green lights North-South 127 ActionTime = millis(); 128 do { 129 PedestrianVerification(); 130 } while (millis() - ActionTime < NorthSouthGreenTime); 131 132 //Yellow lights cycle for North 133 Slave1Values[NorthGreenUp] = 0; 134 Slave1Values[NorthGreenRight] = 0; 135 Slave1Values[NorthYellow] = 1; 136 SlavesActions(); 137 ActionTime = millis(); 138 do { 139 PedestrianVerification(); 140 } while (millis() - ActionTime < YellowTime); 141 142 //Buffer cycle before advance green light South 143 Slave1Values[NorthYellow] = 0; 144 Slave1Values[NorthRed] = 1; 145 SlavesActions(); 146 ActionTime = millis(); 147 do { 148 PedestrianVerification(); 149 } while (millis() - ActionTime < BufferTime); 150 151 //Advance Green light cycle for South 152 Slave2Values[SouthLeftRed] = 0; 153 Slave2Values[SouthLeftGreen] = 1; 154 SlavesActions(); 155 ActionTime = millis(); 156 do { 157 PedestrianVerification(); 158 } while (millis() - ActionTime < AdvanceGreenTime); 159 160 //Yellow light cycle for South 161 Slave2Values[SouthLeftGreen] = 0; 162 Slave2Values[SouthGreenUp] = 0; 163 Slave2Values[SouthGreenRight] = 0; 164 Slave2Values[SouthLeftYellow] = 1; 165 Slave2Values[SouthYellow] = 1; 166 SlavesActions(); 167 ActionTime = millis(); 168 do { 169 PedestrianVerification(); 170 } while (millis() - ActionTime < YellowTime); 171 172 //Buffer Cycle before Green lights East-West 173 Slave2Values[SouthLeftYellow] = 0; 174 Slave2Values[SouthYellow] = 0; 175 Slave2Values[SouthLeftRed] = 1; 176 Slave2Values[SouthRed] = 1; 177 SlavesActions(); 178 ActionTime = millis(); 179 do { 180 PedestrianVerification(); 181 } while (millis() - ActionTime < BufferTime); 182 183 //Pedestrian Cycle Verification 184 if (PedestrianCall == 1) { 185 PedestrianCycle(); 186 } 187 188 //Advance green light cycle for East 189 Slave1Values[EastLeftRed] = 0; 190 Slave1Values[EastRed] = 0; 191 Slave1Values[EastLeftGreen] = 1; 192 Slave1Values[EastGreenUp] = 1; 193 Slave1Values[EastGreenRight] = 1; 194 SlavesActions(); 195 ActionTime = millis(); 196 do { 197 PedestrianVerification(); 198 } while (millis() - ActionTime < AdvanceGreenTime); 199 200 //Yellow light cycle for Left East 201 Slave1Values[EastLeftGreen] = 0; 202 Slave1Values[EastLeftYellow] = 1; 203 SlavesActions(); 204 ActionTime = millis(); 205 do { 206 PedestrianVerification(); 207 } while (millis() - ActionTime < YellowTime); 208 209 //Buffer cycle before green lights East-West 210 Slave1Values[EastLeftYellow] = 0; 211 Slave1Values[EastLeftRed] = 1; 212 SlavesActions(); 213 ActionTime = millis(); 214 do { 215 PedestrianVerification(); 216 } while (millis() - ActionTime < BufferTime); 217 218 //Green lights cycle for East-West 219 Slave2Values[WestRed] = 0; 220 Slave2Values[WestGreenUp] = 1; 221 Slave2Values[WestGreenRight] = 1; 222 SlavesActions(); 223 ActionTime = millis(); 224 do { 225 PedestrianVerification(); 226 } while (millis() - ActionTime < EastWestGreenTime); 227 228 //Yellow light cycle for East 229 Slave1Values[EastGreenUp] = 0; 230 Slave1Values[EastGreenRight] = 0; 231 Slave1Values[EastYellow] = 1; 232 SlavesActions(); 233 ActionTime = millis(); 234 do { 235 PedestrianVerification(); 236 } while (millis() - ActionTime < YellowTime); 237 238 //Buffer cycle before advance green light West 239 Slave1Values[EastYellow] = 0; 240 Slave1Values[EastRed] = 1; 241 SlavesActions(); 242 ActionTime = millis(); 243 do { 244 PedestrianVerification(); 245 } while (millis() - ActionTime < BufferTime); 246 247 //Advance Green light cycle for West 248 Slave2Values[WestLeftRed] = 0; 249 Slave2Values[WestLeftGreen] = 1; 250 SlavesActions(); 251 ActionTime = millis(); 252 do { 253 PedestrianVerification(); 254 } while (millis() - ActionTime < AdvanceGreenTime); 255 256 //Yellow light cycle for West 257 Slave2Values[WestLeftGreen] = 0; 258 Slave2Values[WestGreenUp] = 0; 259 Slave2Values[WestGreenRight] = 0; 260 Slave2Values[WestLeftYellow] = 1; 261 Slave2Values[WestYellow] = 1; 262 SlavesActions(); 263 ActionTime = millis(); 264 do { 265 PedestrianVerification(); 266 } while (millis() - ActionTime < YellowTime); 267 268 //Buffer Cycle before Green lights North-South 269 Slave2Values[WestLeftYellow] = 0; 270 Slave2Values[WestYellow] = 0; 271 Slave2Values[WestLeftRed] = 1; 272 Slave2Values[WestRed] = 1; 273 SlavesActions(); 274 ActionTime = millis(); 275 do { 276 PedestrianVerification(); 277 } while (millis() - ActionTime < BufferTime); 278 279 //Pedestrian Cycle Verification 280 if (PedestrianCall == 1) { 281 PedestrianCycle(); 282 } 283 284 //Advance Green light cycle for North 285 Slave1Values[NorthLeftRed] = 0; 286 Slave1Values[NorthRed] = 0; 287 Slave1Values[NorthLeftGreen] = 1; 288 Slave1Values[NorthGreenUp] = 1; 289 Slave1Values[NorthGreenRight] = 1; 290 SlavesActions(); 291 ActionTime = millis(); 292 do { 293 PedestrianVerification(); 294 } while (millis() - ActionTime < AdvanceGreenTime); 295 296 //Yellow light cycle for left North 297 Slave1Values[NorthLeftGreen] = 0; 298 Slave1Values[NorthLeftYellow] = 1; 299 SlavesActions(); 300 ActionTime = millis(); 301 do { 302 PedestrianVerification(); 303 } while (millis() - ActionTime < YellowTime); 304 305 //Buffer Cycle before Green lights North-South 306 Slave1Values[NorthLeftYellow] = 0; 307 Slave1Values[NorthLeftRed] = 1; 308 SlavesActions(); 309 ActionTime = millis(); 310 do { 311 PedestrianVerification(); 312 } while (millis() - ActionTime < BufferTime); 313 314 //Start of Cycle,Green lights North-South 315 Slave2Values[SouthGreenUp] = 1; 316 Slave2Values[SouthGreenRight] = 1; 317 Slave2Values[SouthRed] = 0; 318 SlavesActions(); 319 320} 321 322void PedestrianVerification() { 323 324 delay(25); 325 ButtonState = digitalRead(PedestrianButton); 326 if (ButtonState == HIGH) { 327 PedestrianCall = 1; 328 } 329 330} 331 332void PedestrianCycle() { 333 334 //White lights cycle 335 Slave1Values[WestOrange] = 0; 336 Slave1Values[NorthOrange] = 0; 337 Slave2Values[EastOrange] = 0; 338 Slave2Values[SouthOrange] = 0; 339 Slave1Values[WestWhite] = 1; 340 Slave1Values[NorthWhite] = 1; 341 Slave2Values[EastWhite] = 1; 342 Slave2Values[SouthWhite] = 1; 343 SlavesActions(); 344 delay(PedestrianCrossingTime); 345 Slave1Values[WestWhite] = 0; 346 Slave1Values[NorthWhite] = 0; 347 Slave2Values[EastWhite] = 0; 348 Slave2Values[SouthWhite] = 0; 349 SlavesActions(); 350 for (i = 0; i < QtyPedestrianFlash / 2; i++) { 351 Slave1Values[WestOrange] = 1; 352 Slave1Values[NorthOrange] = 1; 353 Slave2Values[EastOrange] = 1; 354 Slave2Values[SouthOrange] = 1; 355 SlavesActions(); 356 delay(ShorthFlashTime); 357 Slave1Values[WestOrange] = 0; 358 Slave1Values[NorthOrange] = 0; 359 Slave2Values[EastOrange] = 0; 360 Slave2Values[SouthOrange] = 0; 361 SlavesActions(); 362 delay(ShorthFlashTime); 363 } 364 Slave1Values[WestOrange] = 1; 365 Slave1Values[NorthOrange] = 1; 366 Slave2Values[EastOrange] = 1; 367 Slave2Values[SouthOrange] = 1; 368 SlavesActions(); 369 delay(BufferTime); 370 PedestrianCall = 0; 371 372}
Slave Arduino #1
arduino
1#include <Wire.h> 2 3int i = 0; 4int Event = 0; 5byte SlaveValues[18]; 6 7void setup() { 8 9 for (i = 0; i <= 13; i++) { 10 pinMode(i, OUTPUT); 11 } 12 pinMode(A0, OUTPUT); 13 pinMode(A1, OUTPUT); 14 pinMode(A2, OUTPUT); 15 pinMode(A3, OUTPUT); 16 Wire.begin(1); 17 Wire.onReceive(receiveEvent); 18 19} 20 21void receiveEvent(int howMany) { 22 23 for (i = 0; i < howMany; i++) 24 { 25 SlaveValues[i] = Wire.read(); 26 } 27 Event = 1; 28 29} 30 31void loop() { 32 33 if (Event == 1) { 34 35 if (SlaveValues[0] == 0) { 36 digitalWrite(0, LOW); 37 } 38 else { 39 digitalWrite(0, HIGH); 40 } 41 if (SlaveValues[1] == 0) { 42 digitalWrite(1, LOW); 43 } 44 else { 45 digitalWrite(1, HIGH); 46 } 47 if (SlaveValues[2] == 0) { 48 digitalWrite(2, LOW); 49 } 50 else { 51 digitalWrite(2, HIGH); 52 } 53 if (SlaveValues[3] == 0) { 54 digitalWrite(3, LOW); 55 } 56 else { 57 digitalWrite(3, HIGH); 58 } 59 if (SlaveValues[4] == 0) { 60 digitalWrite(4, LOW); 61 } 62 else { 63 digitalWrite(4, HIGH); 64 } 65 if (SlaveValues[5] == 0) { 66 digitalWrite(5, LOW); 67 } 68 else { 69 digitalWrite(5, HIGH); 70 } 71 if (SlaveValues[6] == 0) { 72 digitalWrite(6, LOW); 73 } 74 else { 75 digitalWrite(6, HIGH); 76 } 77 if (SlaveValues[7] == 0) { 78 digitalWrite(7, LOW); 79 } 80 else { 81 digitalWrite(7, HIGH); 82 } 83 if (SlaveValues[8] == 0) { 84 digitalWrite(8, LOW); 85 } 86 else { 87 digitalWrite(8, HIGH); 88 } 89 if (SlaveValues[9] == 0) { 90 digitalWrite(9, LOW); 91 } 92 else { 93 digitalWrite(9, HIGH); 94 } 95 if (SlaveValues[10] == 0) { 96 digitalWrite(10, LOW); 97 } 98 else { 99 digitalWrite(10, HIGH); 100 } 101 if (SlaveValues[11] == 0) { 102 digitalWrite(11, LOW); 103 } 104 else { 105 digitalWrite(11, HIGH); 106 } 107 if (SlaveValues[12] == 0) { 108 digitalWrite(12, LOW); 109 } 110 else { 111 digitalWrite(12, HIGH); 112 } 113 if (SlaveValues[13] == 0) { 114 digitalWrite(13, LOW); 115 } 116 else { 117 digitalWrite(13, HIGH); 118 } 119 if (SlaveValues[14] == 0) { 120 digitalWrite(A0, LOW); 121 } 122 else { 123 digitalWrite(A0, HIGH); 124 } 125 if (SlaveValues[15] == 0) { 126 digitalWrite(A1, LOW); 127 } 128 else { 129 digitalWrite(A1, HIGH); 130 } 131 if (SlaveValues[16] == 0) { 132 digitalWrite(A2, LOW); 133 } 134 else { 135 digitalWrite(A2, HIGH); 136 } 137 if (SlaveValues[17] == 0) { 138 digitalWrite(A3, LOW); 139 } 140 else { 141 digitalWrite(A3, HIGH); 142 } 143 Event = 0; 144 } 145 146}
Downloadable files
traffic_light_2_0_P182JTEWZ8.png
traffic_light_2_0_P182JTEWZ8.png

Comments
Only logged in users can leave comments