-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVelocity.py
58 lines (35 loc) · 1.01 KB
/
Velocity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
print "Distance Measurement In Progress"
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.output(TRIG, False)
print "Waiting For Sensor To Settle"
time.sleep(2)
def distcheck():
GPIO.output(TRIG, True) #fires a pulse
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0: #measure some times
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start #does the maths
distance = pulse_duration * 17150
distance = round(distance, 2)
print "Distance:",distance,"cm"
return distance
#the new bit that measures speed
interval=int(input("Choose your interval time"))
dist1=distcheck()
time.sleep(interval)
dist2=distcheck()
displacement=dist2-dist1
velocity=displacement/interval
velocity = round(velocity, 2)
print("Your velocity is")
print(velocity)
GPIO.cleanup()