Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import Adafruit_DHT
  2. import RPi.GPIO as GPIO  # Import GPIO library
  3. import time  # Import time library
  4.  
  5. GPIO.setmode(GPIO.BCM)
  6.  
  7. PIR = 21
  8. TRIG = 23                                  #Associate pin 23 to TRIG
  9. ECHO = 24
  10. BUZZER = 18
  11.  
  12. SAFE_DOOR = 50
  13.  
  14. GPIO.setup(PIR, GPIO.IN) #PIR
  15.  
  16. GPIO.setup(BUZZER, GPIO.OUT)
  17.  
  18. GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
  19. GPIO.setup(ECHO,GPIO.IN)
  20.  
  21.  
  22. def checkDistance():
  23.     GPIO.output(TRIG, False)  # Set TRIG as LOW
  24.     GPIO.output(TRIG, True)  # Set TRIG as HIGH
  25.     time.sleep(0.00001)  # Delay of 0.00001 seconds
  26.     GPIO.output(TRIG, False)  # Set TRIG as LOW
  27.  
  28.     while GPIO.input(ECHO) == 0:  # Check whether the ECHO is LOW
  29.         pulse_start = time.time()  # Saves the last known time of LOW pulse
  30.  
  31.     while GPIO.input(ECHO) == 1:  # Check whether the ECHO is HIGH
  32.         pulse_end = time.time()  # Saves the last known time of HIGH pulse
  33.  
  34.     pulse_duration = pulse_end - pulse_start  # Get pulse duration to a variable
  35.  
  36.     distance = pulse_duration * 17150  # Multiply pulse duration by 17150 to get distance
  37.     distance = round(distance, 2)  # Round to two decimal points
  38.  
  39.     if distance > 2 and distance < 400:  # Check whether the distance is within range
  40.         # print("Distance:",distance - 0.5,"cm")  #Print distance with 0.5 cm calibration
  41.         return distance - 0.5
  42.     else:
  43.         # print("Out Of Range")                   #display out of range
  44.         return -1
  45.  
  46. def check_PIR():
  47.     if GPIO.input(PIR):
  48.         time.sleep(5) #to avoid multiple detection
  49.         return True
  50.     return False
  51.  
  52.  
  53. def start_buzzer():
  54.     GPIO.output(BUZZER, True)
  55.  
  56.  
  57. def stop_buzzer():
  58.     GPIO.output(BUZZER, False)
  59.  
  60.  
  61. def main():
  62.     while True:
  63.         if check_PIR():
  64.             if checkDistance() < SAFE_DOOR:
  65.                 start_buzzer()
  66.                 time.sleep(20)
  67.                 stop_buzzer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement