File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -42,10 +42,10 @@ def _send_pulse_and_wait(self):
42
42
We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
43
43
"""
44
44
self ._trigger .value (0 ) # Stabilize the sensor
45
- time . sleep_us (5 )
45
+ self . _delay_us (5 )
46
46
self ._trigger .value (1 )
47
47
# Send a 10us pulse.
48
- time . sleep_us (10 )
48
+ self . _delay_us (10 )
49
49
self ._trigger .value (0 )
50
50
try :
51
51
pulse_time = machine .time_pulse_us (self .echo , 1 , self .timeout_us )
@@ -74,3 +74,12 @@ def distance(self) -> float:
74
74
# 0.034320 cm/us that is 1cm each 29.1us
75
75
cms = (pulse_time / 2 ) / 29.1
76
76
return cms
77
+
78
+ def _delay_us (self , delay :int ):
79
+ """
80
+ Custom implementation of time.sleep_us(), used to get around the bug in MicroPython where time.sleep_us()
81
+ doesn't work properly and causes the IDE to hang when uploading the code
82
+ """
83
+ start = time .ticks_us ()
84
+ while time .ticks_diff (time .ticks_us (), start ) < delay :
85
+ pass
You can’t perform that action at this time.
0 commit comments