-
Notifications
You must be signed in to change notification settings - Fork 0
/
pir2.py
37 lines (37 loc) · 1.14 KB
/
pir2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO_PIR = 7
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 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!"
# Record previous state
GPIO.output(27, GPIO.HIGH)
GPIO.output(27, GPIO.LOW)
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
sleep(0.01)
# clean up gpio pins when we exit the script
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()