簡體   English   中英

而條件不符合python

[英]while conditions not meeting in python

好吧,我正在嘗試開發一個家庭鬧鍾,根據從PIR模塊收到的反饋(使用覆盆子pi及其GPIO)拍攝照片。

問題如下。 當PIr被觸發時,需要5個圖片,然后轉到一個函數,該函數在接下來的5秒內仍然會檢查另一個觸發器,或者再次觸發它。

如果已經過了5秒(time.time()<start + secs)並且沒有檢測到任何移動(Curren_State保持== 0),它只會暫停

我遇到問題的代碼塊:

#secs is received as a parameter (let's say the integer 5)

while time.time() < start + secs or not Current_State==True:
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1:
        takePics(3)

問題:當我犯這種情況時(沒有OR):

while time.time() < start + secs:
     #CODE

該腳本似乎正常:如果5秒鍾過去就會消失。 但是如果我添加的條件(*或者不是Current_State == True *)它只是沒有參加第一個條件,因為我在每個循環中顯示time.time()start + secs的比較 ,我看到了第一個比第二個大,並且仍然保持執行時間。

所以我仍在開發代碼,但代碼或多或少都是這樣的。 如果以下代碼沒有很好地總結: http//pastebin.com/0xP4Le1U

# Import required Python libraries

# Here I define GPIO stuff

# Photo dimensions and rotation

# global variables
Current_State=0
Previous_State=0


def takePics(nPics):
    #Here I take pics

def reCheck:
    global Current_State, alert

    alert=0
    start = time.time()
    Current_State = 0

    while time.time() < start + secs or not Current_State==True:
        Current_State = GPIO.input(GPIO_PIR)
        if Current_State==1:
            takePics(3)

            #If there's no movement, this alert remains =0 
            #and will exit the "while" from which it was called 
            alert=1

#Here I have more functions like sendEmail and so on

def main():
    #main code

    while True:

         #SOME CODE

         if Current_State==1 and Previous_State==0:
            print "----> Motion detected!"

            Previous_State = 1
            alert=1

            #sendMail()
            switchLightON() # Switch on the light using the relay
            takePics(5)

            while alert==1:
                reCheck(4) # we check again in case movement was detected in reCheck

if __name__ == "__main__":
    main()

改變or and 可選地,考慮簡化not Current_State==True如果Current_State是布爾值,則not Current_State==TrueCurrent_State is not True或者not Current_State

while time.time() < start + secs and Current_State is not True:
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1:
        takePics(3)

這將循環,直到secs秒已經過去,或者Current_State停止為真。 訣竅在於,只有在條件為時停止。 or是假只有如果兩個條件都為假, and是假如果任一條件為假。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM