-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (44 loc) · 846 Bytes
/
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
package main
import (
"machine"
"time"
)
var (
threshold = uint16(40000)
waitTime = time.Minute * 10
fetchRetry = 6
sampleInterval = time.Second * 10
)
func main() {
machine.InitADC()
pr := machine.ADC{machine.ADC0}
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
r := machine.D2
r.Configure(machine.PinConfig{Mode: machine.PinOutput})
for {
v := fetchLight(pr)
switchLight(led, r, v)
time.Sleep(waitTime)
}
}
func fetchLight(pr machine.ADC) uint16 {
s := uint32(0)
for i := 1; i <= fetchRetry; i++ {
v := pr.Get()
s += uint32(v)
if i != fetchRetry {
time.Sleep(sampleInterval)
}
}
return uint16(s / uint32(fetchRetry))
}
func switchLight(led, r machine.Pin, light uint16) {
if light > threshold {
led.High()
r.High()
return
}
led.Low()
r.Low()
}