@@ -90,28 +90,30 @@ See Also:
90
90
<http://circuitpython.readthedocs.io/en/latest/docs/pyboard/tutorial/repl.html> `_ (the guide linked was written
91
91
for the pyboard, but it still works), then input the following:
92
92
93
- ::
93
+ .. code-block :: python
94
94
95
95
import board
96
96
dir (board)
97
97
98
98
Without a Context Manager
99
99
-------------------------
100
100
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.
103
102
104
- ::
103
+ .. code-block :: python
105
104
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 )
115
117
116
118
117
119
With a Context Manager
@@ -121,13 +123,14 @@ In the example below, we use a context manager (the `with <https://docs.python.o
121
123
instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for
122
124
us.
123
125
124
- ::
126
+ .. code-block :: python
125
127
128
+ import board
126
129
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:
128
131
try :
129
132
while True :
130
- print(sonar.dist_cm() )
133
+ print (sonar.distance )
131
134
sleep(2 )
132
135
except KeyboardInterrupt :
133
136
pass
0 commit comments