mmabey / circuitpython_hcsr04 Goto Github PK
View Code? Open in Web Editor NEWCircuitPython library for controlling HC-SR04 ultrasonic range sensors
License: MIT License
CircuitPython library for controlling HC-SR04 ultrasonic range sensors
License: MIT License
If no echo is received the call to dist_cm() will hang. See the discussion in this Forum post
https://forums.adafruit.com/viewtopic.php?f=52&t=131188&start=15#p654534
One simple fix is to add a timeout as a keyword argument. Then use time.monotonic() to return a -1 if a timeout occurs.
I have tested this and it seems to be working well. My sensor was often hanging during the first sample.
This will also prevent it hanging if the sensor is not present.
I'll be happy to submit a PR if you think this is the right way to address this.
Here are some snippets of the changes I made
def __init__(self, trig_pin, echo_pin,timeout=2):
...
self.timeout=timeout
...
def _dist_two_wire(self):
self.echo.clear() # Discard any previous pulse values
self.trig.value = 1 # Set trig high
time.sleep(0.00001) # 10 micro seconds 10/1000/1000
self.trig.value = 0 # Set trig low
timeout=time.monotonic()
self.echo.resume()
while len(self.echo) == 0:
# Wait for a pulse
if(time.monotonic()-timeout)>self.timeout:
self.echo.pause()
return -1
self.echo.pause()
if self.echo[0] == 65535:
return -1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.