Pergunta

Bem, estou tentando desenvolver um alarme residencial que tire fotos de acordo com o feedback recebido de um módulo PIR (usando Raspberry Pi e seus GPIOs).

O problema é o seguinte.Quando o PIr é acionado, tira 5 fotos, depois vai para uma função que permanece verificando outro acionamento durante os próximos 5 segundos OU até que seja acionado novamente.

Só sai do while caso os 5 segundos tenham passado (time.time() < start + secs) e nenhum movimento seja detectado (Curren_State permanece == 0)

Bloco de código com o qual estou tendo problemas:

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

O PROBLEMA:Quando faço esta condição (sem o OR):

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

O script parece funcionar normalmente:se os 5 segundos tiverem passado, ele apaga.Mas se à condição while eu adicionar (*ou não Current_State==True*) ele simplesmente não atende a primeira condição já que exibo em cada loop a comparação de hora.hora() e início + segundos e vejo que o primeiro é maior que o segundo e ainda continua executando o while.

Ainda estou desenvolvendo o código, mas o código é mais ou menos assim.Caso o código a seguir não esteja bem resumido: 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()
Foi útil?

Solução

Mudar or para and.Opcionalmente, considere simplificar not Current_State==True para Current_State is not True ou apenas not Current_State se Current_State é um booleano.

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

Isso irá repetir até que secs segundos se passaram ou Current_State deixa de ser verdadeiro.O truque é que o while só para quando sua condição é falso. or é falso apenas se ambos condições são falsas, and é falso se qualquer condição é falsa.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top