Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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