以下是我在英特爾Galileo Gen2上運行的代碼。我只是想知道爲什麼當物體接近超聲波傳感器時,程序停止並抱怨變量sig「本地變量」sig在分配之前引用「?距離小於4cm時伽利略和超聲波錯誤
import mraa
import time
trig = mraa.Gpio(0)
echo = mraa.Gpio(1)
trig.dir(mraa.DIR_OUT)
echo.dir(mraa.DIR_IN)
def distance(measure='cm'):
trig.write(0)
time.sleep(0.2)
trig.write(1)
time.sleep(0.00001)
trig.write(0)
while echo.read() == 0:
nosig = time.time()
while echo.read() == 1:
sig = time.time()
# et = Elapsed Time
et = sig - nosig
if measure == 'cm':
distance = et * 17150
elif measure == 'in':
distance = et/0.000148
else:
print('improper choice of measurement!!')
distance = None
return distance
while True:
print(distance('cm'))
非常感謝您的理解:) – Jedani
如果回答您的問題,請將其標記爲如此。謝謝 :) – deets