if语句条件中的while循环

2024-04-25 12:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的在努力完成代码的最后一部分。在

这里有一些背景。这个代码通过一个超声波传感器寻找一个在它前面的物体,如果有,它会通过http峈get把它记录到互联网数据库中,如果没有对象,它只会每隔2s查找一次

我把所有的东西都打蜡了,除非有人把东西放在那里很长时间。看看代码,它会有意义的。(这只是代码的一部分)。在

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秒,它将记录两次,这是我们不能拥有的。所以,我需要这样的东西,但语法上是正确的。在

^{pr2}$

我希望这对你们来说足够清楚。在

如果有什么需要澄清的地方,请告诉我。在


Tags: the对象代码echotimeis记录pin