2017-03-09 32 views
0

私のコードの最後の部分を完了するのは本当に苦労しています。whileループ内のループ条件

ここに背景があります。このコードは、超音波センサーを介してその前にあるオブジェクトを探します。存在する場合、http_get経由でインターネットデータベースに記録します。オブジェクトがなければ、2秒ごとに探し続けます。

誰かが長い間オブジェクトを残している場合を除いて、私はすべてワックスを塗りました。コードを見て、それは理にかなっています。 (これはコードの一部です)。誰かが一度だけログに記録する5秒未満、のためにそこにオブジェクトを離れた場合だから今、あなたが見ることができるよう

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) 

、(オブジェクトの数が非常に重要です)。注意点は、誰かがそのオブジェクトを忘れてしまったり、10秒間そこに残したりすると、2回ログに記録されます。だから、私はこのようなものが必要だが、構文的に正しい。

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). 

私はこれがあなたにとって十分明確であることを望みます。

どこかの説明が必要な場合は教えてください。そんなに感謝 -

答えて

-1

使用フラグは、距離が働く以上15か

flag_reset = 0 

while True: 

    (your_code) 

    if cm >15: 

     time.sleep(1) 
     flag_reset = 0 

    elif cm <=15 and flag_reset == 0: 

     do_something() 
     flag_reset = 1 
+0

に行ったことを確認します。なぜ私は説明しなくていいのか分かりません。 – LukeVenter

+0

オブジェクトが到着するたびに何が実行されるかを考えてみて、数秒間残ってからもう一度離れてください。 – nekomatic

+0

@nekomatic。編集していただきありがとうございます。 –