Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #Libraries
  2. import RPi.GPIO as GPIO
  3. import time
  4. import math
  5. import socket
  6.  
  7. #GPIO Mode (BOARD / BCM)
  8. GPIO.setmode(GPIO.BCM)
  9.  
  10. #set GPIO Pins
  11. GPIO_TRIGGER = 2
  12. GPIO_ECHO = 3
  13.  
  14. GPIO_TRIGGER2 = 5
  15. GPIO_ECHO2 = 4
  16.  
  17. #set GPIO direction (IN / OUT)
  18. GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
  19. GPIO.setup(GPIO_ECHO, GPIO.IN)
  20.  
  21. GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
  22. GPIO.setup(GPIO_ECHO2, GPIO.IN)
  23.  
  24. def distance():
  25. # set Trigger to HIGH
  26. GPIO.output(GPIO_TRIGGER, True)
  27.  
  28. # set Trigger after 0.01ms to LOW
  29. time.sleep(0.00001)
  30.  
  31. GPIO.output(GPIO_TRIGGER, False)
  32.  
  33. StartTime = time.time()
  34.  
  35. StopTime = time.time()
  36.  
  37. # save StartTime
  38. while GPIO.input(GPIO_ECHO) == 0:
  39. StartTime = time.time()
  40.  
  41. # save time of arrival
  42. while GPIO.input(GPIO_ECHO) == 1:
  43. StopTime = time.time()
  44.  
  45. # time difference between start and arrival
  46. TimeElapsed = StopTime - StartTime
  47.  
  48. distance = math.floor((TimeElapsed * 34300) / 2)
  49.  
  50. return distance
  51.  
  52. def distance2():
  53.  
  54. time.sleep(0.003)
  55. GPIO.output(GPIO_TRIGGER2, True)
  56. time.sleep(0.00001)
  57. GPIO.output(GPIO_TRIGGER2, False)
  58. StartTime = time.time()
  59. StopTime = time.time()
  60. while GPIO.input(GPIO_ECHO2) == 0:
  61. StartTime2 = time.time()
  62.  
  63. while GPIO.input(GPIO_ECHO2) == 1:
  64. StopTime2 = time.time()
  65.  
  66. TimeElapsed2 = StopTime2 - StartTime2
  67. distance2 =math.floor((TimeElapsed2 * 34300) / 2)
  68. return distance2
  69.  
  70. UDP_IP = "192.168.0.107"
  71. UDP_PORT = 8083
  72. while True:
  73. dist = distance()
  74. dist2 = distance2()
  75. data = (dist*100)+dist2
  76. sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  77. sock.sendto(str(data),(UDP_IP, UDP_PORT))
  78. time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement