我真的很难完成代码的最后一部分。
这里有一些背景。这段代码通过超声波传感器寻找它前面的物体,如果有,它通过 http_get 将其记录到互联网数据库中,如果没有物体,它只是每 2 秒查找一次。
我把所有东西都打蜡了,除非有人把东西放在那里很长时间。看看代码,它会很有意义。(这只是代码的一部分)。
while True:
#Setting the pins and taking the reading.
#In a while loop so that it continually does it.
trig=machine.Pin(5,machine.Pin.OUT)
trig.low()
time.sleep_ms(2)
trig.high()
time.sleep_ms(10)
trig.low()
echo=machine.Pin(4,machine.Pin.IN)
while echo.value() == 0:
pass
t1 = time.ticks_us()
while echo.value() == 1:
pass
t2 = time.ticks_us()
cm = (t2 - t1) / 58.0
print(cm) #cm is the output that I use to determine if the object is there or not.
if cm >= 15: #This means there is nothing there.
time.sleep(1)
elif cm <= 15: #Because the distance is less than 15cm, something is there, and it logs it.
http_get('URL')
time.sleep(5)
所以现在,如您所见,如果有人将对象留在那里不到 5 秒,那只会记录一次(对象的数量至关重要)。需要注意的是,如果有人忘记了那里的对象,或者将它放在那里 10 秒,它会记录两次,这是我们无法做到的。所以,我有点需要这样的东西,但在语法上是正确的。
def checking():
if cm >= 15:
time.sleep(1)
elif cm <= 15:
http_get('URL')
time.sleep(5)
while: cm <= 15:
keep sensing but not logging.
then, if the distance changes to back to more than 15cm,
return back to the top. (because the object would be gone).
我希望这对你们来说足够清楚。
让我知道是否需要在某处进行澄清。