Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- import RPi.GPIO as GPIO
- import time
- import math
- import socket
- #GPIO Mode (BOARD / BCM)
- GPIO.setmode(GPIO.BCM)
- #set GPIO Pins
- GPIO_TRIGGER = 2
- GPIO_ECHO = 3
- GPIO_TRIGGER2 = 5
- GPIO_ECHO2 = 4
- #set GPIO direction (IN / OUT)
- GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
- GPIO.setup(GPIO_ECHO, GPIO.IN)
- GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
- GPIO.setup(GPIO_ECHO2, GPIO.IN)
- def distance():
- # set Trigger to HIGH
- GPIO.output(GPIO_TRIGGER, True)
- # set Trigger after 0.01ms to LOW
- time.sleep(0.00001)
- GPIO.output(GPIO_TRIGGER, False)
- StartTime = time.time()
- StopTime = time.time()
- # save StartTime
- while GPIO.input(GPIO_ECHO) == 0:
- StartTime = time.time()
- # save time of arrival
- while GPIO.input(GPIO_ECHO) == 1:
- StopTime = time.time()
- # time difference between start and arrival
- TimeElapsed = StopTime - StartTime
- distance = math.floor((TimeElapsed * 34300) / 2)
- return distance
- def distance2():
- time.sleep(0.003)
- GPIO.output(GPIO_TRIGGER2, True)
- time.sleep(0.00001)
- GPIO.output(GPIO_TRIGGER2, False)
- StartTime = time.time()
- StopTime = time.time()
- while GPIO.input(GPIO_ECHO2) == 0:
- StartTime2 = time.time()
- while GPIO.input(GPIO_ECHO2) == 1:
- StopTime2 = time.time()
- TimeElapsed2 = StopTime2 - StartTime2
- distance2 =math.floor((TimeElapsed2 * 34300) / 2)
- return distance2
- UDP_IP = "192.168.0.107"
- UDP_PORT = 8083
- while True:
- dist = distance()
- dist2 = distance2()
- data = (dist*100)+dist2
- sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
- sock.sendto(str(data),(UDP_IP, UDP_PORT))
- time.sleep(0.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement