-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
81 lines (63 loc) · 1.54 KB
/
main.go
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
package main
import (
"machine"
"time"
)
var blink bool
// It is important to set a scheduler for this to work
// tinygo flash -scheduler tasks -target=arduino ch2/traffic-lights-pedestrian/main.go
func main() {
blink = false
outputConfig := machine.PinConfig{Mode: machine.PinOutput}
greenLED := machine.D13
greenLED.Configure(outputConfig)
yellowLED := machine.D12
yellowLED.Configure(outputConfig)
redLED := machine.D11
redLED.Configure(outputConfig)
pedestrianGreen := machine.D4
pedestrianGreen.Configure(outputConfig)
pedestrianRed := machine.D5
pedestrianRed.Configure(outputConfig)
inputConfig := machine.PinConfig{Mode: machine.PinInput}
buttonInput := machine.D2
buttonInput.Configure(inputConfig)
pedestrianRed.High()
pedestrianGreen.Low()
go trafficLights(redLED, greenLED, yellowLED, pedestrianRed, pedestrianGreen)
for {
if buttonInput.Get() {
blink = !blink
}
time.Sleep(50 * time.Millisecond)
}
}
func trafficLights(redLED, greenLED, yellowLED, pedestrianRED, pedestrianGreen machine.Pin) {
for {
if blink {
redLED.Low()
greenLED.Low()
pedestrianGreen.Low()
pedestrianRED.Low()
yellowLED.High()
time.Sleep(350 * time.Millisecond)
yellowLED.Low()
time.Sleep(350 * time.Millisecond)
continue
} else {
pedestrianRED.High()
}
redLED.High()
time.Sleep(time.Second)
yellowLED.High()
time.Sleep(time.Second)
redLED.Low()
yellowLED.Low()
greenLED.High()
time.Sleep(time.Second)
greenLED.Low()
yellowLED.High()
time.Sleep(time.Second)
yellowLED.Low()
}
}