File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,10 +42,10 @@ def _send_pulse_and_wait(self):
4242 We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
4343 """
4444 self ._trigger .value (0 ) # Stabilize the sensor
45- time . sleep_us (5 )
45+ self . _delay_us (5 )
4646 self ._trigger .value (1 )
4747 # Send a 10us pulse.
48- time . sleep_us (10 )
48+ self . _delay_us (10 )
4949 self ._trigger .value (0 )
5050 try :
5151 pulse_time = machine .time_pulse_us (self .echo , 1 , self .timeout_us )
@@ -74,3 +74,12 @@ def distance(self) -> float:
7474 # 0.034320 cm/us that is 1cm each 29.1us
7575 cms = (pulse_time / 2 ) / 29.1
7676 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