mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-03 14:37:46 +00:00
c765ef3970
This PR adds support for controlling servos. For example, on the Arduino Uno it should be able to control up to 6 servos jitter-free when using all available PWM pins. I haven't added support for setting a position in degrees, mainly because this varies by servo and it's probably necessary to configure the bounds in some way. Therefore, I added just SetMicroseconds. This makes the API possible to use and leaves the possibility of adding a SetPosition in the future.
44 lines
693 B
Go
44 lines
693 B
Go
package main
|
|
|
|
import (
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/servo"
|
|
)
|
|
|
|
// Configuration for the Arduino Uno.
|
|
// Please change the PWM and pin if you want to try this example on a different
|
|
// board.
|
|
var (
|
|
pwm = machine.Timer1
|
|
pin = machine.D9
|
|
)
|
|
|
|
func main() {
|
|
s, err := servo.New(pwm, pin)
|
|
if err != nil {
|
|
for {
|
|
println("could not configure servo")
|
|
time.Sleep(time.Second)
|
|
}
|
|
return
|
|
}
|
|
|
|
println("setting to 0°")
|
|
s.SetMicroseconds(1000)
|
|
time.Sleep(3 * time.Second)
|
|
|
|
println("setting to 45°")
|
|
s.SetMicroseconds(1500)
|
|
time.Sleep(3 * time.Second)
|
|
|
|
println("setting to 90°")
|
|
s.SetMicroseconds(2000)
|
|
time.Sleep(3 * time.Second)
|
|
|
|
for {
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|