Giter Club home page Giter Club logo

circuitpython_hcsr04's People

Contributors

jerryneedell avatar mmabey avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

circuitpython_hcsr04's Issues

dist_cm hangs if no echo received

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.