I'm using a motion sensor to turn on an LED. My hurdle right now is to have the led stay on for an interval, if more movement is detected I want to reset a timer to zero. I almost have this figured out by using 2 scripts, the first one detects motion and calls the second which turns the LED high for an interval.
What happens now is the sensor detects movement, triggers the led script which executes to completion and the led goes dim, even though I'm constantly moving, a second or so passes and the LED lights up again and the process repeats.
How can I write this so when movement is detected the interval is reset and the LED stays HIGH without interruption?
Code: Select all
#!/usr/bin/python import RPi.GPIO as GPIO import time # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO to use on Pi GPIO_PIR = 18 print "PIR Module Test (CTRL-C to exit)" GPIO.setup(GPIO_PIR,GPIO.IN) # Echo Current_State = 0 Previous_State = 0 try: print "Waiting for PIR to settle ..." # Loop until PIR output is 0 while GPIO.input(GPIO_PIR)==1: Current_State = 0 print " Ready" # Loop until users quits with CTRL-C while True : # Read PIR state Current_State = GPIO.input(GPIO_PIR) if Current_State==1 and Previous_State==0: # PIR is triggered print " Motion detected!" execfile('led_on.py') # Record previous state Previous_State=1 elif Current_State==0 and Previous_State==1: # PIR has returned to ready state print " Ready" Previous_State=0 # Wait for 10 milliseconds time.sleep(0.01) except KeyboardInterrupt: print " Quit" # Reset GPIO settings GPIO.cleanup()
Code: Select all
import RPi.GPIO as GPIO import time # Use BCM GPIO references # instead of physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO to use on Pi GPIO_LED = 17 GPIO.setup(GPIO_LED, GPIO.OUT) GPIO.setwarnings(False) GPIO.output(GPIO_LED, True) time.sleep(5) GPIO.output(GPIO_LED, False)