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:
if pir.motion_detected:
GPIO.output(3, GPIO.LOW)
print("On")
time.sleep(7)
GPIO.output(3, GPIO.HIGH)
except KeyboardInterrupt:
GPIO.cleanup()