/
pwm.go
54 lines (43 loc) · 898 Bytes
/
pwm.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"
)
// This example assumes that an RGB LED is connected to pins 3, 5 and 6 on an Arduino.
// Change the values below to use different pins.
const (
redPin = 3
greenPin = 5
bluePin = 6
)
// cycleColor is just a placeholder until math/rand or some equivalent is working.
func cycleColor(color uint8) uint8 {
if color < 10 {
return color + 1
} else if color < 200 {
return color + 10
} else {
return 0
}
}
func main() {
machine.InitPWM()
red := machine.PWM{redPin}
red.Configure()
green := machine.PWM{greenPin}
green.Configure()
blue := machine.PWM{bluePin}
blue.Configure()
var rc uint8
var gc uint8 = 20
var bc uint8 = 30
for {
rc = cycleColor(rc)
gc = cycleColor(gc)
bc = cycleColor(bc)
red.Set(uint16(rc) << 8)
green.Set(uint16(gc) << 8)
blue.Set(uint16(bc) << 8)
time.Sleep(time.Millisecond * 500)
}
}