-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMulti-HCSR04.py
More file actions
100 lines (87 loc) · 3.72 KB
/
Multi-HCSR04.py
File metadata and controls
100 lines (87 loc) · 3.72 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Written and Debugged by: Shantanu Ghosh, Vishal Venkateswaran
import RPi.GPIO as GPIO
import time
import pigio
import sys
while True:
try:
GPIO.setmode(GPIO.BOARD)
sensor1 = [7, 11]
sensor2 = [16, 18]
sensor3 = [33, 35]
sensor4 = [36, 38]
# Assigns the value of the pin position on the Pi
GPIO.setup(sensor1[0], GPIO.OUT)
GPIO.setup(sensor1[1], GPIO.IN)
GPIO.setup(sensor2[0], GPIO.OUT)
GPIO.setup(sensor2[1], GPIO.IN)
GPIO.setup(sensor3[0], GPIO.OUT)
GPIO.setup(sensor3[1], GPIO.IN)
GPIO.setup(sensor4[0], GPIO.OUT)
GPIO.setup(sensor4[1], GPIO.IN)
'''Assigns the variables Trig and Echo to the
appropriate I/O GPIO function.'''
GPIO.output(sensor1[0], GPIO.LOW)
GPIO.output(sensor2[0], GPIO.LOW)
GPIO.output(sensor3[0], GPIO.LOW)
GPIO.output(sensor4[0], GPIO.LOW)
# Sets the output pin, Trig to LOW
time.sleep(1.5)
#Gives sensors enough time to settle before triggering again
GPIO.output(sensor1[0], GPIO.HIGH)
GPIO.output(sensor2[0], GPIO.HIGH)
GPIO.output(sensor3[0], GPIO.HIGH)
GPIO.output(sensor4[0], GPIO.HIGH)
# Sets the output pin, Trig to HIGH
time.sleep(0.00001)
# A small pause of 0.01 ms
GPIO.output(sensor1[0], GPIO.LOW)
GPIO.output(sensor2[0], GPIO.LOW)
GPIO.output(sensor3[0], GPIO.LOW)
GPIO.output(sensor4[0], GPIO.LOW)
# Resets the value of Trig to LOW
while GPIO.input(sensor1[1])==0:
startTime1 = time.time()
while GPIO.input(sensor1[1])==1:
endTime1 = time.time()
while GPIO.input(sensor2[1])==0:
startTime2 = time.time()
while GPIO.input(sensor2[1])==1:
endTime2 = time.time()
while GPIO.input(sensor3[1])==0:
startTime3 = time.time()
while GPIO.input(sensor3[1])==1:
endTime3 = time.time()
while GPIO.input(sensor4[1])==0:
startTime4 = time.time()
while GPIO.input(sensor4[1])==1:
endTime4 = time.time()
'''Conditional statement to tell the sensor to calculate the time
based on whether the input Echo is equal to 0 or 1.'''
durationTime1 = endTime1 - startTime1
durationTime2 = endTime2 - startTime2
durationTime3 = endTime3 - startTime3
durationTime4 = endTime4 - startTime4
distance_L = round(durationTime1 * 17150, 2)
distance_R = round(durationTime2 * 17150, 2)
distance_RE = round(durationTime3 * 17150, 2)
distance_U = round(durationTime4 * 17150, 2)
with open('distance.txt', 'w') as num_file:
if distance_L < 200:
num_file.write(str("Left Distance:",distance_L/30.48,"ft"))
elif distance_R < 200:
num_file.write(str("Right Distance:",distance_R/30.48,"ft"))
elif distance_RE < 200:
num_file.write(str("Rear Distance:",distance_RE/30.48,"ft"))
elif distance_U < 200:
num_file.write(str("Underside Distance:",distance_U/30.48,"ft"))
else:
pass
'''Calculate the total time based on the total time, and calculates the distance
between the sensor and the measured object based on the speed of sound in one
direction 17150 cm/s. This result is rounded to two decimal places in cm and then
converted to feet by dividing by 30.48 and finally, returned with a prompt. This is
done for all directions in which the sensor will be facing.'''
finally:
GPIO.cleanup()
# Resets any assigned values to the pins so the code can be ran fresh again.