Skip to content

Commit c1e9eb7

Browse files
authored
Merge pull request #8 from kattni/check-update
Update failed read check.
2 parents 0420430 + ede42c4 commit c1e9eb7

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

adafruit_us100.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
__version__ = "0.0.0-auto.0"
4141
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
4242

43+
import time
44+
4345

4446
class US100:
4547
"""Control a US-100 ultrasonic range sensor."""
@@ -51,34 +53,54 @@ def __init__(self, uart):
5153
def distance(self):
5254
"""Return the distance measured by the sensor in cm.
5355
This is the function that will be called most often in user code.
54-
If no signal is received, we'll throw a RuntimeError exception. This means
55-
either the sensor was moving too fast to be pointing in the right
56+
If no signal is received, return ``None``. This can happen when the
57+
object in front of the sensor is too close, the wiring is incorrect or the
58+
sensor is not found. If the signal received is not 2 bytes, return ``None``.
59+
This means either the sensor was moving too fast to be pointing in the right
5660
direction to pick up the ultrasonic signal when it bounced back (less
5761
likely), or the object off of which the signal bounced is too far away
58-
for the sensor to handle. In my experience, the sensor can detect
62+
for the sensor to handle. In my experience, the sensor can not detect
5963
objects over 460 cm away.
6064
:return: Distance in centimeters.
6165
:rtype: float
6266
"""
63-
self._uart.write(bytes([0x55]))
64-
data = self._uart.read(2) # 2 bytes return for distance
65-
if not data:
66-
raise RuntimeError("Sensor not found. Check your wiring!")
67+
for _ in range(2): # Attempt to read twice.
68+
self._uart.write(bytes([0x55]))
69+
time.sleep(0.1)
70+
data = self._uart.read(2) # 2 byte return for distance.
71+
if data: # If there is a reading, exit the loop.
72+
break
73+
time.sleep(0.1) # You need to wait between readings, so delay is included.
74+
else:
75+
# Loop exited normally, so read failed twice.
76+
# This can happen when the object in front of the sensor is too close, if the wiring
77+
# is incorrect or the sensor is not found.
78+
return None
6779

6880
if len(data) != 2:
69-
raise RuntimeError("Did not receive distance response")
81+
return None
82+
7083
dist = (data[1] + (data[0] << 8)) / 10
7184
return dist
7285

7386
@property
7487
def temperature(self):
7588
"""Return the on-chip temperature, in Celsius"""
76-
self._uart.write(bytes([0x50]))
77-
data = self._uart.read(1) # 1 byte return for temp
78-
if not data:
79-
raise RuntimeError("Sensor not found. Check your wiring!")
89+
for _ in range(2): # Attempt to read twice.
90+
self._uart.write(bytes([0x50]))
91+
time.sleep(0.1)
92+
data = self._uart.read(1) # 1 byte return for temp
93+
if data: # If there is a reading, exit the loop.
94+
break
95+
time.sleep(0.1) # You need to wait between readings, so delay is included.
96+
else:
97+
# Loop exited normally, so read failed twice.
98+
# This can happen when the object in front of the sensor is too close, if the wiring
99+
# is incorrect or the sensor is not found.
100+
return None
80101

81102
if len(data) != 1:
82-
raise RuntimeError("Did not receive temperature response")
103+
return None
104+
83105
temp = data[0] - 45
84106
return temp

0 commit comments

Comments
 (0)