user12358221
user12358221

Reputation:

Turn off PIR Motion sensor

just need a quick advice if anyone can help. I finished my code for my alarm project using kivy for the gui and python for the script. My issue however is with the motionsensor. I can detect if motion is high or low no problem. Is there a way however to stop the motionsensor from detecting as in a low or false state lets say if i enter a disarm pin. Seems once the alarm is activated it cant be turned off. Code can submitted on request but im hoping theres a simple way to achieve this.

    def callback(self):  
        global Activated, Armed

        if GPIO.input(11)==False:
            Activated = False
            Armed = True
        elif GPIO.input(11) == True:   
            Activated = True
            Armed = False
        else:
            pass

        if Activated == True:
                self.info.text = '!!Intruder Alert!!'
                self.info.background_color = [1, 1, 0, 1]
                GPIO.output(16,1)
                GPIO.output(22, 1)
                GPIO.output(18,0)
                #self.txt_display.text = ''
                #time.sleep(3)
                #disarm = int(input('Enter deactivation code'))  
        else:
            pass

    def reset_notice(self):
        self.info.text = 'Alarm disarmed'

    def armAlarm(self):
        global Activated, Armed

        if self.txt_display.text == str(pincode):
            Clock.schedule_once(lambda dt: self.indicated(), 3)
            Armed = True
            Activated = False
            GPIO.output(22, 0)
            GPIO.output(18, 1)
            GPIO.output(16,1)
            time.sleep(0.5)
            GPIO.output(16,0)
        elif self.txt_display.text != str(pincode):
            Armed = False
            Activated = False
            self.txt_display.text = ''
            self.info.text = '**Incorrect Pin**'
            self.info.background_color = [1, 1, 1, 1]
            Clock.schedule_once(lambda dt: self.reset_notice(), 3)
            self.info.background_color = [0, 1, 0, 1]

        if Armed == False:
            Clock.schedule_interval(lambda dt: self.callback(), 0.5)
        else:
            self.reset_alarm()

in kv file when i press a button it runs the armAlarm function, if the alarm is activated the pir is also activated and if an intruder is detected the buzzer goes off, buzzer is 16.

    def disarmAlarm(self):
        global Activated, Armed
        Armed = True
        Activated = False


        if Armed == True:
            if Activated == False:
                if self.txt_display.text == str(pincode):
                    self.info.text = 'Alarm disarmed'
                    self.info.background_color = [0, 1, 0, 1]
                    self.txt_display.text = ''
                    Armed = False
                    Activated = False
                    GPIO.output(16,0)
                    GPIO.output(22, 0)
                    GPIO.output(18, 0)
                    GPIO.setup(11, GPIO.IN)
                    print('Alarm deactivated')

    def disarmAlarm(self):
        global Activated, Armed
        Armed = True
        Activated = True

        if Armed == True:
            if Activated == True:
                if self.txt_display.text == str(pincode):
                    self.info.text = 'Alarm disarmed'
                    self.info.background_color = [0, 1, 0, 1]
                    self.txt_display.text = ''
                    Armed = False
                    Activated = False
                    GPIO.output(16,0)
                    GPIO.output(22, 0)
                    GPIO.output(18, 0)
                    print('Alarm deactivated')  


    def pir_off(self):
        GPIO.input(11) == False      

    def reset_alarm(self):
        global Activated, Armed
        Armed = False
        Activated = False

        if Armed == False:
            if Activated == False:
                   Clock.schedule_interval(lambda dt: self.pir_off(), 0.1)

and its supposed to do this when i deactivate the alarm which it does, theoretically, problem is if i put my hand in front of the pir it still detects movement. I want to stop it from detecting or looking for movement altogether.

Upvotes: 0

Views: 419

Answers (1)

befunkt
befunkt

Reputation: 131

The main issue I'm seeing is you have the PIR sensor unconditionally linked to the Alarm state. You are trying to remedy this by turning off / changing the sensor after the disarm code is entered, or for whatever reason the system goes into Disarmed mode.

  1. The PIR should be running at all times. You want your program to have that raw relay feed from the sensor no matter what happens.
  2. In the lines of code inside your function 'callback', you are setting the state of the alarm activation only based on the current state of the PIR sensor. You need to include the condition of the alarm Armed state before you set Activate to True. e.g.:
 def callback(self):  
        global Activated, Armed

        #we dont care if the PIR goes to False. This only matters for lighting systems and 
        #the like.
        if GPIO.input(11) == False:
            pass

        elif GPIO.input(11) == True  and  Armed == True:   
            Activated = True

The above example receives ALL of the PIR data as raw data, and only the PIR==True state in combination with the Armed==True state will cause the alarm to trigger.

  1. I assume you have callback() on a loop, and the system checks the state of pin 11 every nth? If this is the case, then you probably want to limit the alarm Activated state to only trigger the alarm once per incident, whereas if looped, the above code would retrigger the alarm every cycle when the conditions are right. You want to trigger the alarm only under the following conditions: Armed==True, and PIR transitions from False to True. This is called 'momentary' logic, where we ignore the continuous state of the PIR, and only concern ourselves with when the state changes. That would look something like this:
 def callback(self):  
        global Activated, Armed, PIRRawState

        if GPIO.input(11) == False:
            if PIRRawState == True:
                PIRRawState = False

        elif GPIO.input(11) == True:   
            if PIRRawState == False:
                PIRRawState = True
                if Armed == True:
                    activateAlarm()

def activateAlarm(self):
    self.info.text = '!!Intruder Alert!!'
    self.info.background_color = [1, 1, 0, 1]
    GPIO.output(16,1)
    GPIO.output(22, 1)
    GPIO.output(18,0)
    #self.txt_display.text = ''
    #time.sleep(3)
    #disarm = int(input('Enter deactivation code')) 

There are several other things that I'd recommend you change, but I think that will solve the problem for you. Other suggestions will appear in the comments for this Answer.

Upvotes: 0

Related Questions