-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion.py
39 lines (34 loc) · 938 Bytes
/
motion.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
38
39
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
buttonPin = 16
sensorPin = 18
lightPin = 12
soundSensor = 25
GPIO.setup(sensorPin, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(lightPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(soundSensor, GPIO.OUT)
try:
while True:
p = GPIO.PWM(soundSensor, 50)
button_state = GPIO.input(buttonPin)
if GPIO.input(sensorPin):
GPIO.output(lightPin, 1)
p.start(50)
p.ChangeFrequency(300)
print("Motion Detected")
time.sleep(0.1)
GPIO.output(lightPin, 0)
elif not button_state:
print("Clicked!")
GPIO.cleanup()
os.system("sudo shutdown -h now")
else:
GPIO.output(lightPin, 0)
GPIO.output(soundSensor, 0)
p.stop()
time.sleep(0.1)
except:
GPIO.cleanup()