Skip to content

Commit b178b09

Browse files
committed
Lesson with David 03: More buttons and state machine!
1 parent e6cbf1f commit b178b09

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

traffic-lights/traffic-lights.ino

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#define PIN_RED 2
22
#define PIN_YELLOW 3
33
#define PIN_GREEN 4
4+
#define PIN_MODE_MANUAL 11
45
#define PIN_MODE_YELLOW 12
56

67
#define COLOR_RED 0
@@ -11,13 +12,14 @@
1112
#define DELAY_YELLOW 1500
1213
#define DELAY_GREEN 5000
1314
#define DELAY_BLINK 250
14-
#define DELAY_BUTTON_PRESS 250
15+
#define DELAY_BUTTON_PRESS 500
1516

1617
#define MODE_AUTO 0
1718
#define MODE_YELLOW 1
19+
#define MODE_MANUAL 2
1820

19-
unsigned long lastColorSwitchTime = millis();
20-
unsigned long lastModeSwitchTime = millis();
21+
unsigned long lastColorChangeTime = millis();
22+
unsigned long lastButtonPressTime = millis();
2123

2224
unsigned int mode = MODE_AUTO;
2325
unsigned int color = COLOR_RED;
@@ -29,6 +31,7 @@ void setup() {
2931
pinMode(PIN_YELLOW, OUTPUT);
3032
pinMode(PIN_GREEN, OUTPUT);
3133
pinMode(PIN_MODE_YELLOW, INPUT_PULLUP);
34+
pinMode(PIN_MODE_MANUAL, INPUT_PULLUP);
3235
}
3336

3437
void setRedLight(bool on) {
@@ -91,8 +94,12 @@ bool yellowButtonPressed() {
9194
return digitalRead(PIN_MODE_YELLOW) == LOW;
9295
}
9396

97+
bool blueButtonPressed() {
98+
return digitalRead(PIN_MODE_MANUAL) == LOW;
99+
}
100+
94101
void loop() {
95-
if (millis() - lastModeSwitchTime > DELAY_BUTTON_PRESS) {
102+
if (millis() - lastButtonPressTime > DELAY_BUTTON_PRESS) {
96103
if (yellowButtonPressed()) {
97104
if (mode != MODE_YELLOW) {
98105
mode = MODE_YELLOW;
@@ -102,24 +109,39 @@ void loop() {
102109
color = COLOR_RED;
103110
}
104111
on = true;
105-
lastColorSwitchTime = millis();
106-
lastModeSwitchTime = millis();
112+
lastColorChangeTime = millis();
113+
lastButtonPressTime = millis();
114+
}
115+
if (blueButtonPressed()) {
116+
if (mode != MODE_MANUAL) {
117+
mode = MODE_MANUAL;
118+
color = COLOR_RED;
119+
} else {
120+
if (color == COLOR_RED) {
121+
color = COLOR_GREEN;
122+
} else {
123+
color = COLOR_RED;
124+
}
125+
}
126+
on = true;
127+
lastColorChangeTime = millis();
128+
lastButtonPressTime = millis();
107129
}
108130
}
109131

110132
switch (mode) {
111133
case MODE_AUTO:
112134
switch (color) {
113135
case COLOR_RED:
114-
if (millis() - lastColorSwitchTime > DELAY_RED) {
115-
lastColorSwitchTime = millis();
136+
if (millis() - lastColorChangeTime > DELAY_RED) {
137+
lastColorChangeTime = millis();
116138
color = COLOR_YELLOW;
117139
prevColor = COLOR_RED;
118140
}
119141
break;
120142
case COLOR_YELLOW:
121-
if (millis() - lastColorSwitchTime > DELAY_YELLOW) {
122-
lastColorSwitchTime = millis();
143+
if (millis() - lastColorChangeTime > DELAY_YELLOW) {
144+
lastColorChangeTime = millis();
123145
if (prevColor == COLOR_GREEN) {
124146
color = COLOR_RED;
125147
} else {
@@ -129,8 +151,8 @@ void loop() {
129151
}
130152
break;
131153
case COLOR_GREEN:
132-
if (millis() - lastColorSwitchTime > DELAY_GREEN) {
133-
lastColorSwitchTime = millis();
154+
if (millis() - lastColorChangeTime > DELAY_GREEN) {
155+
lastColorChangeTime = millis();
134156
color = COLOR_YELLOW;
135157
prevColor = COLOR_GREEN;
136158
}
@@ -141,15 +163,17 @@ void loop() {
141163
}
142164
break;
143165
case MODE_YELLOW:
144-
if (millis() - lastColorSwitchTime > DELAY_BLINK) {
145-
lastColorSwitchTime = millis();
166+
if (millis() - lastColorChangeTime > DELAY_BLINK) {
167+
lastColorChangeTime = millis();
146168
if (on) {
147169
on = false;
148170
} else {
149171
on = true;
150172
}
151173
}
152174
break;
175+
case MODE_MANUAL:
176+
break;
153177
}
154178

155179
setLight(color, on);

0 commit comments

Comments
 (0)