-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewleft_sensor.py
More file actions
45 lines (37 loc) · 1.26 KB
/
newleft_sensor.py
File metadata and controls
45 lines (37 loc) · 1.26 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
# Written and Debugged by: Vishal Venkateswaran and Shantanu Ghosh
import RPi.GPIO as GPIO
import time
while True:
try:
GPIO.setmode(GPIO.BOARD)
#assign gpio pin numbers to trigger&echo
PIN_TRIGGER = 13
PIN_ECHO = 15
#assign trigger&echo to proper gpio i/o status
GPIO.setwarnings(False)
GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)
#set trigger to low
GPIO.output(PIN_TRIGGER, GPIO.LOW)
#sensor calibration/settle time
time.sleep(1.4)
#set trigger to high
GPIO.output(PIN_TRIGGER, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(PIN_TRIGGER, GPIO.LOW)
#condition to set start/stop time based on echo
while GPIO.input(PIN_ECHO)==0:
pulse_start_time = time.time()
while GPIO.input(PIN_ECHO)==1:
pulse_end_time = time.time()
#calculate distance based on times. assume speed of sound to be 17150 cm/s. round distance to 2 decimal places
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
#write distance to a file so the webpage can use the data from the sensors
with open('leftsensor.txt', 'w') as num_file:
num_file.write(str(distance))
#allow keyboard interrupt to stop program
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
break