mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +01:00
set the pin modes appropriately
This commit is contained in:
parent
1355a9d959
commit
65b32143cf
@ -2,6 +2,7 @@
|
|||||||
package us020
|
package us020
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -29,6 +30,8 @@ type us020 struct {
|
|||||||
|
|
||||||
initialized bool
|
initialized bool
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
|
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new US020 interface. The bus variable controls
|
// New creates a new US020 interface. The bus variable controls
|
||||||
@ -55,11 +58,18 @@ func (d *us020) setup() (err error) {
|
|||||||
d.echoPin = rpio.Pin(d.echoPinNumber) // ECHO port on the US020
|
d.echoPin = rpio.Pin(d.echoPinNumber) // ECHO port on the US020
|
||||||
d.triggerPin = rpio.Pin(d.triggerPinNumber) // TRIGGER port on the US020
|
d.triggerPin = rpio.Pin(d.triggerPinNumber) // TRIGGER port on the US020
|
||||||
|
|
||||||
|
d.echoPin.Input()
|
||||||
|
d.triggerPin.Output()
|
||||||
|
|
||||||
temp, err := bmp085.Temperature()
|
temp, err := bmp085.Temperature()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.speedSound = 340
|
d.speedSound = 340
|
||||||
} else {
|
} else {
|
||||||
d.speedSound = 331.4 + 0.606*temp
|
d.speedSound = 331.4 + 0.606*temp
|
||||||
|
|
||||||
|
if d.debug {
|
||||||
|
log.Printf("read a temperature of %v, so speed of sound = %v", temp, d.speedSound)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d.initialized = true
|
d.initialized = true
|
||||||
@ -73,17 +83,29 @@ func (d *us020) Distance() (distance float64, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.debug {
|
||||||
|
log.Print("trigetting pulse")
|
||||||
|
}
|
||||||
|
|
||||||
// Generate a TRIGGER pulse
|
// Generate a TRIGGER pulse
|
||||||
d.triggerPin.High()
|
d.triggerPin.High()
|
||||||
time.Sleep(pulseDelay)
|
time.Sleep(pulseDelay)
|
||||||
d.triggerPin.Low()
|
d.triggerPin.Low()
|
||||||
|
|
||||||
|
if d.debug {
|
||||||
|
log.Print("waiting for echo to go high")
|
||||||
|
}
|
||||||
|
|
||||||
// Wait until ECHO goes high
|
// Wait until ECHO goes high
|
||||||
for d.echoPin.Read() == rpio.Low {
|
for d.echoPin.Read() == rpio.Low {
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Now() // Record time when ECHO goes high
|
startTime := time.Now() // Record time when ECHO goes high
|
||||||
|
|
||||||
|
if d.debug {
|
||||||
|
log.Print("waiting for echo to go low")
|
||||||
|
}
|
||||||
|
|
||||||
// Wait until ECHO goes low
|
// Wait until ECHO goes low
|
||||||
for d.echoPin.Read() == rpio.High {
|
for d.echoPin.Read() == rpio.High {
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user