-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection_roadlights_simulation.ino
135 lines (112 loc) · 2.96 KB
/
intersection_roadlights_simulation.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
// left road
const int LEFT_RED_LIGHT = 2;
const int LEFT_YELLOW_LIGHT = 3;
const int LEFT_GREEN_LIGHT = 4;
// right road
const int RIGHT_RED_LIGHT = 13;
const int RIGHT_YELLOW_LIGHT = 12;
const int RIGHT_GREEN_LIGHT = 11;
// top road
const int TOP_RED_LIGHT = 5;
const int TOP_YELLOW_LIGHT = 6;
const int TOP_GREEN_LIGHT = 7;
// bottom road
const int BOTTOM_RED_LIGHT = 10;
const int BOTTOM_YELLOW_LIGHT = 9;
const int BOTTOM_GREEN_LIGHT = 8;
const int DELAY_BTWN_LIGHTS = 1000;
class Road {
private:
int RED_LIGHT;
int YELLOW_LIGHT;
int GREEN_LIGHT;
public:
Road(int red, int yellow, int green) {
RED_LIGHT = red;
YELLOW_LIGHT = yellow;
GREEN_LIGHT = green;
}
void redLights() {
digitalWrite(RED_LIGHT, HIGH);
}
void redToYellow() {
digitalWrite(RED_LIGHT, LOW);
digitalWrite(YELLOW_LIGHT, HIGH);
}
void yellowToGreen() {
digitalWrite(YELLOW_LIGHT, LOW);
digitalWrite(GREEN_LIGHT, HIGH);
}
void greenToYellow() {
digitalWrite(GREEN_LIGHT, LOW);
digitalWrite(YELLOW_LIGHT, HIGH);
}
void yellowToRed() {
digitalWrite(YELLOW_LIGHT, LOW);
digitalWrite(RED_LIGHT, HIGH);
}
};
// create road objects
Road leftRoad(LEFT_RED_LIGHT, LEFT_YELLOW_LIGHT, LEFT_GREEN_LIGHT);
Road rightRoad(RIGHT_RED_LIGHT, RIGHT_YELLOW_LIGHT, RIGHT_GREEN_LIGHT);
Road topRoad(TOP_RED_LIGHT, TOP_YELLOW_LIGHT, TOP_GREEN_LIGHT);
Road bottomRoad(BOTTOM_RED_LIGHT, BOTTOM_YELLOW_LIGHT, BOTTOM_GREEN_LIGHT);
void setup() {
// left road
pinMode(LEFT_RED_LIGHT, OUTPUT);
pinMode(LEFT_YELLOW_LIGHT, OUTPUT);
pinMode(LEFT_GREEN_LIGHT, OUTPUT);
// right road
pinMode(RIGHT_RED_LIGHT, OUTPUT);
pinMode(RIGHT_YELLOW_LIGHT, OUTPUT);
pinMode(RIGHT_GREEN_LIGHT, OUTPUT);
// top road
pinMode(TOP_RED_LIGHT, OUTPUT);
pinMode(TOP_YELLOW_LIGHT, OUTPUT);
pinMode(TOP_GREEN_LIGHT, OUTPUT);
// bottom road
pinMode(BOTTOM_RED_LIGHT, OUTPUT);
pinMode(BOTTOM_YELLOW_LIGHT, OUTPUT);
pinMode(BOTTOM_GREEN_LIGHT, OUTPUT);
}
void leftAndRightRoadCycle() {
// light the left and right road
leftRoad.redToYellow();
rightRoad.redToYellow();
delay(DELAY_BTWN_LIGHTS);
leftRoad.yellowToGreen();
rightRoad.yellowToGreen();
delay(10000);
// turn off
leftRoad.greenToYellow();
rightRoad.greenToYellow();
delay(DELAY_BTWN_LIGHTS);
leftRoad.yellowToRed();
rightRoad.yellowToRed();
}
void topAndBottomRoadCycle() {
// lights the top and bottom road
topRoad.redToYellow();
bottomRoad.redToYellow();
delay(DELAY_BTWN_LIGHTS);
topRoad.yellowToGreen();
bottomRoad.yellowToGreen();
delay(10000);
// turn off
topRoad.greenToYellow();
bottomRoad.greenToYellow();
delay(DELAY_BTWN_LIGHTS);
topRoad.yellowToRed();
bottomRoad.yellowToRed();
}
void loop() {
// start with red lights
leftRoad.redLights();
rightRoad.redLights();
topRoad.redLights();
bottomRoad.redLights();
delay(3000);
leftAndRightRoadCycle();
delay(3000);
topAndBottomRoadCycle();
}