我想请你帮个忙。我试图将距离传感器连接到我的微型钻头上,但当我使用"time_pulse_us“命令时,它总是给出-2或-1。我阅读了文档,我理解这些数字的含义,但我认为命令有问题,或者我可能用错了方法。
在这方面,我编写了一个简单的代码片段来测试该命令。你能告诉我它出了什么问题吗?
from microbit import * //to import microbit modules
from machine import * //to import the time_pulse_us command
while True:
pin1.write_digital(0)
time = time_pulse_us(pin2, 1) //to begin the timing
pin1.write_digital(1) //this pin is connected to an LED
sleep(1000)
value = pin2.read_digital() //gives 1, as this pin is reading the voltage from the led
pin1.write_digital(0) //this will make the time_pulse command to end timing
display.scroll(time) //it should display the duration of the pulse.
//Displays -2 instead.
display.scroll(value) //gives 1, as expected

为什么这不起作用?
发布于 2021-01-12 21:00:34
time_pulse_us()按顺序运行,而不是在后台运行,因此在调用时,它将等待1秒,直到引脚达到1,但它不会这样做,因此在程序进入下一个命令write_digital(1)之前,time将被设置为-2。
https://stackoverflow.com/questions/65677802
复制相似问题