我正在尝试将 Raspberry 的 python 代码转换为 MicroBit MicroPython,以使用 MicroPython 驱动 Grove - Ultrasonic Ranger 模块。
http://wiki.seeedstudio.com/Grove-Ultrasonic_Ranger/
https://github.com/Seeed-Studio/grove.py/blob/master/grove/grove_ultrasonic_ranger.py
我这样做了,语法没问题:
from microbit import *
import time
_TIMEOUT1 = 1000
_TIMEOUT2 = 10000
def _get_distance():
pin0.write_digital(0)
time.sleep_us(2)
pin0.write_digital(1)
time.sleep_us(10)
pin0.write_digital(0)
t0 = time.ticks_us()
count = 0
while count < _TIMEOUT1:
if pin0.read_digital():
display.set_pixel(1, 1, 5)
break
count += 1
if count >= _TIMEOUT1:
return None
t1 = time.ticks_us()
count = 0
while count < _TIMEOUT2:
if not pin0.read_digital():
display.set_pixel(0, 0, 5)
break
count += 1
if count >= _TIMEOUT2:
return None
t2 = time.ticks_us()
dt = int(time.ticks_diff(t1,t0) * 1000000)
# The problem is upside !
if dt > 530:
return None
distance = (time.ticks_diff(t2,t1) * 1000000 / 29 / 2) # cm
return distance
def get_distance():
while True:
dist = _get_distance()
if dist:
return dist
#Appel de la fonction get_distance(void) et affichage sur le display
display.scroll(get_distance())
但我有很大的价值,因为dt
我不知道为什么......谢谢你的帮助!