Skip to content

Commit

Permalink
Merge pull request #21 from FoamyGuy/dannystaple-readme-patch
Browse files Browse the repository at this point in the history
Fix readme example to use same code from simpletest
  • Loading branch information
FoamyGuy committed Aug 20, 2020
2 parents 04a299f + b600484 commit ce9d1bb
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,30 @@ See Also:
<http://circuitpython.readthedocs.io/en/latest/docs/pyboard/tutorial/repl.html>`_ (the guide linked was written
for the pyboard, but it still works), then input the following:

::
.. code-block:: python
import board
dir(board)
Without a Context Manager
-------------------------

In the example below, we create the `HCSR04` object directly, get the distance every 2 seconds, then
de-initialize the device.
In the example below, we create the `HCSR04` object directly, get the distance every 2 seconds.

::
.. code-block:: python
from adafruit_hcsr04 import HCSR04
sonar = HCSR04(trig, echo)
try:
while True:
print(sonar.dist_cm())
sleep(2)
except KeyboardInterrupt:
pass
sonar.deinit()
import time
import board
import adafruit_hcsr04
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D5, echo_pin=board.D6)
while True:
try:
print((sonar.distance,))
except RuntimeError:
print("Retrying!")
time.sleep(2)
With a Context Manager
Expand All @@ -121,13 +123,14 @@ In the example below, we use a context manager (the `with <https://docs.python.o
instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for
us.

::
.. code-block:: python
import board
from adafruit_hcsr04 import HCSR04
with HCSR04(trig, echo) as sonar:
with HCSR04(trigger_pin=board.D5, echo_pin=board.D6) as sonar:
try:
while True:
print(sonar.dist_cm())
print(sonar.distance)
sleep(2)
except KeyboardInterrupt:
pass
Expand Down

0 comments on commit ce9d1bb

Please sign in to comment.