Domanda

Bene, sto cercando di sviluppare un allarme domestico che scatta foto in base al feedback ricevuto da un modulo PIR (usando Raspberry PI e i suoi GPiOS).

Il problema è il seguente. Quando il PIR viene attivato, prende 5 immagini, quindi passa a una funzione che rimane controllando un altro trigger durante i prossimi 5 secondi o fino a quando non viene nuovamente attivato.

Si esaurisce solo nel caso in cui i 5 secondi siano passati (Time.time ()

Blocco di codice che sto avendo problemi con:

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

Il problema: quando faccio questa condizione (senza o):

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

Lo script sembra fungere dal normale: se i 5 secondi sono passati si spegne. Ma se al momento della condizione aggiungo (* o non corrente_State== true *) non partecipano solo alla prima condizione da quando viene visualizzato in ogni ciclo il confronto di time.time () e < EM> START + SECS E vedo che il primo è più grande del secondo e continua a eseguire l'esecuzione del tempo.

Quindi sto ancora sviluppando il codice, ma il codice è, più o meno qualcosa del genere. Nel caso in cui il codice seguente non sia ben riassunto: 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()
.

È stato utile?

Soluzione

Cambia or a and.Facoltativamente, prendi in considerazione la possibilità di semplificare il not Current_State==True a Current_State is not True o solo not Current_State se Current_State è un booleano.

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

che passerà fino a quando non sono passati i secondi di secs, o Current_State Stops è vero.Il trucco è che mentre si arresta solo quando è la condizione è false .or è falso solo se entrambe le condizioni sono false, and è falsa se condizione è falsa.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top