0

I have a PIR motion sensor hooked up to an LED strip. When motion is detected, the lights turn on as expected. My problem is trying to get the lights to turn off only if there has not been any movement for a certain amount of time. If, however, there continues to be movement, the lights will stay on.

I tried doing this with time.sleep but it would shut off after the specified time even if movement was still being detected. I have looked at the API documentation here but I have not been able to figure it out.

Here's my code.

import RPi.GPIO as GPIO
import time
from gpiozero import MotionSensor
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
pir = MotionSensor(16)
pinList = [3]

for i in pinList: 
    GPIO.setup(i, GPIO.OUT) 
    GPIO.output(i, GPIO.HIGH)


    try:

##    while True:

    if pir.motion_detected:   
        GPIO.output(3, GPIO.LOW)
        print("On")


time.sleep(7)
        GPIO.output(3, GPIO.HIGH)
##            print("Off")

except KeyboardInterrupt:
2
  • You might get more interest over on raspberrypi.stackexchange.com But what you want is to keep track of the "triggered" state and have a separate routine that resets this state. Commented Sep 24, 2017 at 13:23
  • Also see here about event driven programming for this specific case: stackoverflow.com/q/16143842/1531971 Commented Sep 24, 2017 at 13:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.