I'm trying to get my raspberry pi to detect motion using the IR sensor, and then turn on an LED for 5 seconds while still polling the IR sensor every 0.5 seconds. Here is my code so far, but it waits for the LED to turn off before checking the IR sensor again...
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
PIR_PIN = 18
GPIO.setup(PIR_PIN, GPIO.IN)
LED_PIN = 17
GPIO.setup(LED_PIN, GPIO.OUT)
def light():
GPIO.output(LED_PIN, GPIO.input(PIR_PIN))
time.sleep(5)
GPIO.output(LED_PIN, False)
try:
while True:
if GPIO.input(PIR_PIN):
print("Motion Detected!")
light()
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()