1
0
mirror of https://github.com/kidoman/embd synced 2024-11-13 21:18:55 +01:00
embd/samples/us020.go

55 lines
785 B
Go
Raw Normal View History

2014-03-02 08:06:34 +01:00
// +build ignore
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
2014-03-02 20:21:23 +01:00
"github.com/kidoman/embd"
2014-02-10 00:35:41 +01:00
"github.com/kidoman/embd/sensor/us020"
)
func main() {
flag.Parse()
2014-03-02 20:21:23 +01:00
if err := embd.InitGPIO(); err != nil {
panic(err)
}
2014-03-02 20:21:23 +01:00
defer embd.CloseGPIO()
echoPin, err := embd.NewDigitalPin("P9_21")
if err != nil {
panic(err)
}
triggerPin, err := embd.NewDigitalPin("P9_22")
if err != nil {
panic(err)
}
rf := us020.New(echoPin, triggerPin, nil)
defer rf.Close()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, os.Kill)
for {
select {
default:
distance, err := rf.Distance()
if err != nil {
panic(err)
}
fmt.Printf("Distance is %v\n", distance)
time.Sleep(500 * time.Millisecond)
case <-quit:
return
}
}
}