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:
Evan Lalo
  • 1,209
  • 1
  • 14
  • 34
  • You might get more interest over on https://raspberrypi.stackexchange.com/ But what you want is to keep track of the "triggered" state and have a separate routine that resets this state. –  Sep 24 '17 at 13:23
  • Also see here about event driven programming for this specific case: https://stackoverflow.com/q/16143842/1531971 –  Sep 24 '17 at 13:25

0 Answers0