Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Rasberry Pi HELP! So I am working on a code for ultrasonic sensor that should get the speed of an object. Everything on breadboard is
Rasberry Pi HELP! So I am working on a code for ultrasonic sensor that should get the speed of an object. Everything on breadboard is set up correct. ive tried multiple breadboard and sensors so its not that. and the code did at one point but no longer does and I cant figure it out.
'import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
TRIG = 17
GPIO.setup(TRIG,GPIO.OUT)
ECHO = 16
GPIO.setup(ECHO,GPIO.IN)
def measure_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
if distance > 2 and distance
print("Distance:",distance - 0.5,"cm")
else:
print("Out Of Range")
return distance
def get_speed(distance, time):
speed = (distance / 3600)
return speed
while True:
distance = measure_distance()
print("Distance: %.2f" % distance)
time.sleep(1)
speed = get_speed(distance, time)
print("Speed: %.2f m/s" % speed)
time.sleep(1)
GPIO.cleanup()'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started