Last active
December 16, 2015 00:59
-
-
Save TheRealEdDawson/5351700 to your computer and use it in GitHub Desktop.
Program to take the inputs of both passive infrared (PIR) and photocell sensors and combine that data to "detect things moving in the dark", & turn on a light.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO, time, os | |
DEBUG = 1 | |
GPIO.setmode(GPIO.BCM) | |
# Setting up port to use on Blue LED | |
BLUE_LED = 25 | |
GPIO.setup(BLUE_LED, GPIO.OUT) | |
# Define GPIO port to use for PIR | |
GPIO_PIR = 23 | |
print "PIR Module Test (Ctrl-C to exit)" | |
# Set pin as input | |
GPIO.setup(GPIO_PIR,GPIO.IN) | |
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 user 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!" | |
# Light up LED | |
GPIO.output(BLUE_LED, True) | |
# Record previous state | |
Previous_State=1 | |
elif Current_State==0 and Previous_State==1: | |
# PIT has returned to ready state | |
print " Ready" | |
# Turn LED off | |
GPIO.output(BLUE_LED, False) | |
Previous_State=0 | |
# Wait for 10 milliseconds | |
time.sleep(0.01) | |
Except KeyboardInterrupt: | |
print " Quit" | |
# Reset GPIO settings | |
GPIO.cleanup() | |
try: | |
while True: | |
lightlevel = RCtime(18) | |
print lightlevel | |
if lightlevel > 1000: | |
GPIO.output(BLUE_LED, True) | |
else: | |
GPIO.output(BLUE_LED, False) | |
except KeyboardInterrupt: | |
GPIO.cleanup() | |
print "Clean program exit." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment