Documentation
¶
Overview ¶
Package hcsr04 is a driver allowing to use a HC-SR04 ultrasound distance sensor
Example ¶
package main
import (
"fmt"
"os"
"github.com/raspberrypi-go-drivers/hcsr04"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
fmt.Println(h.MeasureDistance())
}
Output:
Example (Monitor) ¶
package main
import (
"fmt"
"os"
"time"
"github.com/raspberrypi-go-drivers/hcsr04"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
if err := h.StartDistanceMonitor(); err != nil {
log.WithField("error", err).Error("impossible to start distance monitor")
os.Exit(1)
} else {
defer h.StopDistanceMonitor()
}
for {
fmt.Println(h.GetDistance())
time.Sleep(hcsr04.MonitorUpdate)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
const ( // MonitorUpdate is the time between each monitor update MonitorUpdate time.Duration = 100 * time.Millisecond )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HCSR04 ¶
type HCSR04 struct {
Measure float32 // The last measure
// contains filtered or unexported fields
}
HCSR04 instance
func NewHCSR04 ¶
NewHCSR04 creates a new HCSR04 instance
Example ¶
package main
import (
"fmt"
"os"
"github.com/raspberrypi-go-drivers/hcsr04"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
fmt.Println(h.MeasureDistance())
}
Output:
func (*HCSR04) GetDistance ¶
GetDistance returns the last distance measured Contrary to MeasureDistance, GetDistance does not trigger a distance measurement
Example ¶
package main
import (
"fmt"
"os"
"time"
"github.com/raspberrypi-go-drivers/hcsr04"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
if err := h.StartDistanceMonitor(); err != nil {
log.WithField("error", err).Error("impossible to start distance monitor")
os.Exit(1)
} else {
defer h.StopDistanceMonitor()
}
for {
fmt.Println(h.GetDistance())
time.Sleep(hcsr04.MonitorUpdate)
}
}
Output:
func (*HCSR04) MeasureDistance ¶
MeasureDistance measure the distance in front of sensor in meters and returns the measure MeasureDistance triggers a distance measurement by the sensor
! MeasureDistance is not design to work in a fast loop For this specific usage, use StartDistanceMonitor associated with GetDistance Instead
Example ¶
package main
import (
"fmt"
"os"
"github.com/raspberrypi-go-drivers/hcsr04"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
fmt.Println(h.MeasureDistance())
}
Output:
func (*HCSR04) StartDistanceMonitor ¶
StartDistanceMonitor starts a process which will keep Measure updated
func (*HCSR04) StopDistanceMonitor ¶
func (hcsr04 *HCSR04) StopDistanceMonitor()
StopDistanceMonitor stop the monitor process
Example ¶
package main
import (
"fmt"
"os"
"time"
"github.com/raspberrypi-go-drivers/hcsr04"
log "github.com/sirupsen/logrus"
"github.com/stianeikeland/go-rpio/v4"
)
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
h := hcsr04.NewHCSR04(17, 27)
if err := h.StartDistanceMonitor(); err != nil {
log.WithField("error", err).Error("impossible to start distance monitor")
os.Exit(1)
} else {
defer h.StopDistanceMonitor()
}
for {
fmt.Println(h.GetDistance())
time.Sleep(hcsr04.MonitorUpdate)
}
}
Output: