Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Adafruit_DHT
- import RPi.GPIO as GPIO # Import GPIO library
- import time # Import time library
- GPIO.setmode(GPIO.BCM)
- PIR = 21
- TRIG = 23 #Associate pin 23 to TRIG
- ECHO = 24
- BUZZER = 18
- SAFE_DOOR = 50
- GPIO.setup(PIR, GPIO.IN) #PIR
- GPIO.setup(BUZZER, GPIO.OUT)
- GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out
- GPIO.setup(ECHO,GPIO.IN)
- def checkDistance():
- GPIO.output(TRIG, False) # Set TRIG as LOW
- GPIO.output(TRIG, True) # Set TRIG as HIGH
- time.sleep(0.00001) # Delay of 0.00001 seconds
- GPIO.output(TRIG, False) # Set TRIG as LOW
- while GPIO.input(ECHO) == 0: # Check whether the ECHO is LOW
- pulse_start = time.time() # Saves the last known time of LOW pulse
- while GPIO.input(ECHO) == 1: # Check whether the ECHO is HIGH
- pulse_end = time.time() # Saves the last known time of HIGH pulse
- pulse_duration = pulse_end - pulse_start # Get pulse duration to a variable
- distance = pulse_duration * 17150 # Multiply pulse duration by 17150 to get distance
- distance = round(distance, 2) # Round to two decimal points
- if distance > 2 and distance < 400: # Check whether the distance is within range
- # print("Distance:",distance - 0.5,"cm") #Print distance with 0.5 cm calibration
- return distance - 0.5
- else:
- # print("Out Of Range") #display out of range
- return -1
- def check_PIR():
- if GPIO.input(PIR):
- time.sleep(5) #to avoid multiple detection
- return True
- return False
- def start_buzzer():
- GPIO.output(BUZZER, True)
- def stop_buzzer():
- GPIO.output(BUZZER, False)
- def main():
- while True:
- if check_PIR():
- if checkDistance() < SAFE_DOOR:
- start_buzzer()
- time.sleep(20)
- stop_buzzer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement