Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. GPIO.setmode(GPIO.BCM)
  5.  
  6. GPIO_TRIGGER = 18
  7. GPIO_ECHO = 24
  8.  
  9. GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
  10. GPIO.setup(GPIO_ECHO, GPIO.IN)
  11.  
  12. def distance():
  13. GPIO.output(GPIO_TRIGGER, True)
  14. time.sleep(0.00001)
  15. GPIO.output(GPIO_TRIGGER, False)
  16.  
  17. StartTime = time.time()
  18. StopTime = time.time()
  19.  
  20. while GPIO.input(GPIO_ECHO) == 0:
  21. StartTime = time.time()
  22.  
  23. while GPIO.input(GPIO_ECHO) == 1:
  24. StopTime = time.time()
  25.  
  26. TimeElapsed = StopTime - StartTime
  27. distance = (TimeElapsed*34300)/2
  28.  
  29. return distance
  30.  
  31. if __name__ == '__main__':
  32. while True:
  33. dist = distance()
  34. print ("Measured Distance = %.1f cm" % dist)
  35. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement