-
Notifications
You must be signed in to change notification settings - Fork 1
/
lights.ino
71 lines (62 loc) · 1.86 KB
/
lights.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
void setup() {
// put your setup code here, to run once:
pinMode(A1, OUTPUT);//red
pinMode(A2, OUTPUT);//orange
pinMode(A3, OUTPUT);//green
}
static unsigned long blinkingOrangeLastTime = 0;
static unsigned long orangeLightPhaseLastTime = 0;
static unsigned long greenOrRedPhaseLastTime = 0;
int phase = 0;
void loop() {
//handle millis rollover
unsigned long currentMillis = millis();
//start with blinking orange light for 10 seconds
if(phase == 0 && (unsigned long)(currentMillis - blinkingOrangeLastTime) >= 1000) {
blinkingOrangeLastTime += 1000;
digitalWrite(A2, !digitalRead(A2));
//stop blinking after 10 seconds
if(millis()/1000 > 10) {
digitalWrite(A2, LOW);
digitalWrite(A1, HIGH);
phase = 1;//change phase
}
}
//red and green light states
if((unsigned long)(currentMillis - greenOrRedPhaseLastTime) >= 2000) {
greenOrRedPhaseLastTime += 2000;
switch(phase)
{
case 2://green light - go ahead
digitalWrite(A1, LOW);//turn off red
digitalWrite(A2, LOW);//turn off green
digitalWrite(A3, HIGH);//turn on green
phase = 3;
break;
case 4://red light - stop
digitalWrite(A1, HIGH);//turn on red
digitalWrite(A2, LOW);//turn off green
digitalWrite(A3, LOW);//turn off green
phase = 1;
break;
}
}
if((unsigned long)(currentMillis - orangeLightPhaseLastTime) >= 10000) {
orangeLightPhaseLastTime += 10000;
switch(phase)
{
case 1://red and yellow - prepare to start
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, LOW);
phase = 2;
break;
case 3://yellow only - prepare to stop
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
digitalWrite(A3, LOW);
phase = 4;
break;
}
}
}