-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistance.py
55 lines (43 loc) · 1.34 KB
/
distance.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
import explorerhat
import time
# while True:
# sensor = explorerhat.analog.one.read()
# inches = sensor / 0.00819
# print(inches)
# time.sleep(0.1)
explorerhat.output.one.off()
time.sleep(1)
def get_distance():
# Sends TRIG Out
explorerhat.output.one.on()
time.sleep(0.00001)
explorerhat.output.one.off()
# Gets ECHO Back
while explorerhat.input.one.read()==0:
pulse_start = time.time()
while explorerhat.input.one.read()==1:
pulse_end = time.time()
print ("Pulse Start: {}".format(pulse_start))
print ("Pulse End : {}".format(pulse_end))
pulse_duration = pulse_end - pulse_start
distance = pulse_duration / 0.000148
distance = round(distance,4)
print("Distance: {} inches".format(distance)) #in cm
return distance
while True:
distance = get_distance()
time.sleep(0.1)
if distance < 10:
print("stop")
explorerhat.motor.two.stop()
explorerhat.motor.one.stop()
elif distance < 20 and distance > 10:
print("backwards")
explorerhat.motor.two.forward(100)
explorerhat.motor.one.backward(100)
elif distance > 50:
explorerhat.motor.one.forward(100)
explorerhat.motor.two.backward(100)
else:
explorerhat.motor.one.stop()
explorerhat.motor.two.stop()