-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.ino
71 lines (58 loc) · 1.19 KB
/
TrafficLight.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
const int greenPin = 2;
const int redPin = 8;
const int yellowPin = 4;
const int buttonPin = 9;
const int maxDelay = 8;
const int minDelay = 2;
const bool buttonPressed = false;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(greenPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
}
void loop() {
if(digitalRead(buttonPin) == HIGH){
competition();
}
else{
demo();
}
// put your main code here, to run repeatedly:
}
void lightBulb(int bulb){
digitalWrite(bulb, HIGH);
}
void extinguishBulb(int bulb){
digitalWrite(bulb, LOW);
}
void timeBulb(int bulb, int time){
lightBulb(bulb);
delay(time);
extinguishBulb(bulb);
}
void demo() {
while(true){
}
}
void competition(){
while(true){
lightBulb(redPin);
delay(1);
int buttonState = digitalRead(buttonPin);
while(buttonState == LOW){
}
while(buttonState == HIGH){
}
extinguishBulb(redPin);
lightBulb(yellowPin);
delay(random(minDelay,maxDelay));
extinguishBulb(yellowPin);
lightBulb(greenPin);
while(buttonState == LOW){
}
while(buttonState == HIGH){
}
}
}