Skip to content

Commit ad6afff

Browse files
jerryneedellmmabey
authored andcommitted
add timeout_sec as keyword parameter (#2)
* add timeout_sec as keyword parameter * Bumped version number, PEP8 spacing Fixes #1
1 parent 353ceeb commit ad6afff

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1
1+
0.2

hcsr04.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@
2424
2525
The HC-SR04 functions by sending an ultrasonic signal, which is reflected by
2626
many materials, and then sensing when the signal returns to the sensor. Knowing
27-
that sound travels through air at 343.2 meters per second
27+
that sound travels through dry air at `343.2 meters per second (at 20 °C)
28+
<https://en.wikipedia.org/wiki/Speed_of_sound>`_, it's pretty straightforward
29+
to calculate how far away the object is by timing how long the signal took to
30+
go round-trip and do some simple arithmetic, which is handled for you by this
31+
library.
2832
2933
.. warning::
3034
3135
The HC-SR04 uses 5V logic, so you will have to use a `level shifter
3236
<https://www.adafruit.com/product/2653?q=level%20shifter&>`_ between it
3337
and your CircuitPython board (which uses 3.3V logic).
3438
35-
* Author(s): Mike Mabey
39+
* Authors:
40+
41+
- Mike Mabey
42+
- Jerry Needell - modified to add timeout while waiting for echo (2/26/2018)
3643
"""
3744
import board
3845
from digitalio import DigitalInOut, DriveMode
3946
from pulseio import PulseIn
40-
from time import sleep
47+
import time
4148

4249

4350
class HCSR04:
@@ -55,20 +62,24 @@ class HCSR04:
5562
except KeyboardInterrupt:
5663
pass
5764
"""
58-
def __init__(self, trig_pin, echo_pin):
65+
def __init__(self, trig_pin, echo_pin, timeout_sec=.1):
5966
"""
6067
:param trig_pin: The pin on the microcontroller that's connected to the
6168
``Trig`` pin on the HC-SR04.
6269
:type trig_pin: str or microcontroller.Pin
6370
:param echo_pin: The pin on the microcontroller that's connected to the
6471
``Echo`` pin on the HC-SR04.
6572
:type echo_pin: str or microcontroller.Pin
73+
:param float timeout_sec: Max seconds to wait for a response from the
74+
sensor before assuming it isn't going to answer. Should *not* be
75+
set to less than 0.05 seconds!
6676
"""
6777
if isinstance(trig_pin, str):
6878
trig_pin = getattr(board, trig_pin)
6979
if isinstance(echo_pin, str):
7080
echo_pin = getattr(board, echo_pin)
7181
self.dist_cm = self._dist_two_wire
82+
self.timeout_sec = timeout_sec
7283

7384
self.trig = DigitalInOut(trig_pin)
7485
self.trig.switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL)
@@ -117,13 +128,15 @@ def dist_cm(self):
117128
def _dist_two_wire(self):
118129
self.echo.clear() # Discard any previous pulse values
119130
self.trig.value = 1 # Set trig high
120-
sleep(0.00001) # 10 micro seconds 10/1000/1000
131+
time.sleep(0.00001) # 10 micro seconds 10/1000/1000
121132
self.trig.value = 0 # Set trig low
122-
133+
timeout = time.monotonic()
123134
self.echo.resume()
124135
while len(self.echo) == 0:
125136
# Wait for a pulse
126-
pass
137+
if (time.monotonic() - timeout) > self.timeout_sec:
138+
self.echo.pause()
139+
return -1
127140
self.echo.pause()
128141
if self.echo[0] == 65535:
129142
return -1
@@ -151,6 +164,6 @@ def test(trig, echo, delay=2):
151164
try:
152165
while True:
153166
print(sonar.dist_cm())
154-
sleep(delay)
167+
time.sleep(delay)
155168
except KeyboardInterrupt:
156169
pass

0 commit comments

Comments
 (0)