-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriggerScriptMovement.py
More file actions
38 lines (32 loc) · 1.13 KB
/
triggerScriptMovement.py
File metadata and controls
38 lines (32 loc) · 1.13 KB
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
38
# .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
# script to trigger procedure starting from a detected movement
# Upon detection, the useful procedure is called and the led lights up.
# .-..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#input pin
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# LED ouput yellow
GPIO.setup(16, GPIO.OUT)
#Callback function to run when motion de
def motionSensor(channel):
GPIO.output(16, GPIO.LOW)
if GPIO.input(21):
global counter
counter += 1
print('Motion Detected')
# here it is possible to call procedures
# the LED remains ON for the whole procedure
GPIO.output(16, GPIO.HIGH)
sleep(5) # simulation of execution time of function
# add event listener on pin 21
# bouncetime == minimum time between two callbacks in milliseconds
GPIO.add_event_detect(21, GPIO.BOTH, callback=motionSensor, bouncetime=300)
counter = 0
try:
while True:
sleep(1)
finally:
GPIO.cleanup()
print ('All cleaned up')