Skip to content

Commit ce9d1bb

Browse files
authored
Merge pull request #21 from FoamyGuy/dannystaple-readme-patch
Fix readme example to use same code from simpletest
2 parents 04a299f + b600484 commit ce9d1bb

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

README.rst

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,30 @@ See Also:
9090
<http://circuitpython.readthedocs.io/en/latest/docs/pyboard/tutorial/repl.html>`_ (the guide linked was written
9191
for the pyboard, but it still works), then input the following:
9292

93-
::
93+
.. code-block:: python
9494
9595
import board
9696
dir(board)
9797
9898
Without a Context Manager
9999
-------------------------
100100

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

104-
::
103+
.. code-block:: python
105104
106-
from adafruit_hcsr04 import HCSR04
107-
sonar = HCSR04(trig, echo)
108-
try:
109-
while True:
110-
print(sonar.dist_cm())
111-
sleep(2)
112-
except KeyboardInterrupt:
113-
pass
114-
sonar.deinit()
105+
import time
106+
import board
107+
import adafruit_hcsr04
108+
109+
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D5, echo_pin=board.D6)
110+
111+
while True:
112+
try:
113+
print((sonar.distance,))
114+
except RuntimeError:
115+
print("Retrying!")
116+
time.sleep(2)
115117
116118
117119
With a Context Manager
@@ -121,13 +123,14 @@ In the example below, we use a context manager (the `with <https://docs.python.o
121123
instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for
122124
us.
123125

124-
::
126+
.. code-block:: python
125127
128+
import board
126129
from adafruit_hcsr04 import HCSR04
127-
with HCSR04(trig, echo) as sonar:
130+
with HCSR04(trigger_pin=board.D5, echo_pin=board.D6) as sonar:
128131
try:
129132
while True:
130-
print(sonar.dist_cm())
133+
print(sonar.distance)
131134
sleep(2)
132135
except KeyboardInterrupt:
133136
pass

0 commit comments

Comments
 (0)