1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-24 13:48:49 +08:00
hybridgroup.gobot/examples/raspi_grove_pi_ultrasonic.go
Thomas Kohler 865e724af0
Build(v2): revert move to v2 subfolder (#932)
* revert move to v2 subfolder
* fix CI and adjust CHANGELOG
2023-05-29 19:23:28 +02:00

45 lines
671 B
Go

//go:build example
// +build example
//
// Do not build by default.
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/raspi"
)
const (
ultrasonicPin = "4"
delayMillisec = 10
)
func main() {
r := raspi.NewAdaptor()
gp := i2c.NewGrovePiDriver(r)
work := func() {
gobot.Every(1*time.Second, func() {
if val, err := gp.UltrasonicRead(ultrasonicPin, delayMillisec); err != nil {
fmt.Println(err)
} else {
fmt.Println("Distance [cm]", val)
}
})
}
robot := gobot.NewRobot("ultrasonicBot",
[]gobot.Connection{r},
[]gobot.Device{gp},
work,
)
robot.Start()
}