Page 1 of 1

Measuring distance using 2 ultrasonic

Posted: Sat Apr 15, 2017 5:07 am
by ilman
hello, i am a newbe with raspi or python. so i have a project to measuring distance with two ultrasonic sensor. i've try with one sensor and it works. i try to write the script with two sensor and it's not work. i know the problem coming from my code. so i need a help to fix my problem please. what's wrong with my code. thanks before
this is script with one ultrasonic

Code: Select all

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)

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)

print "Distance:",distance,"cm"

GPIO.cleanup()
and this is an error script with two ultrasonic

Code: Select all

import RPi.GPIO as GPIO #
Import GPIO library import time #Import time library 
GPIO.setmode(GPIO.BCM) #Set GPIO pin numbering 
GPIO.setwarnings(False)

 TRIG = 23 #Associate pin 23 to TRIG 
ECHO = 24 #Associate pin 24 to ECHO 
TRIG1 = 17 
ECHO1 = 27
 
print "Distance measurement in progress" 

GPIO.setup(TRIG,GPIO.OUT) #Set pin as GPIO out 
GPIO.setup(ECHO,GPIO.IN) #Set pin as GPIO in 
GPIO.setup(TRIG1,GPIO.OUT) #Set pin as GPIO out 
GPIO.setup(ECHO1,GPIO.IN) #Set pin as GPIO in 

while True:

GPIO.output(TRIG, False) #Set TRIG as LOW 
print "Waitng For Sensor To Settle" 
time.sleep(2) #Delay of 2 seconds
 
GPIO.output(TRIG, True) #Set TRIG as HIGH 
time.sleep(0.00001) #Delay of 0.00001 seconds 
GPIO.output(TRIG, False) #Set TRIG as LOW 

while GPIO.input(ECHO)==0: #Check whether the ECHO is LOW 
pulse_start = time.time() #Saves the last known time of LOW pulse
 
while GPIO.input(ECHO)==1: #Check whether the ECHO is HIGH 
pulse_end = time.time() #Saves the last known time of HIGH pulse 

pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable 

distance = pulse_duration * 17160 + 1.1 #Multiply pulse duration by 17150 to get distance 
distance = round(distance, 2) #Round to two decimal points 

while True: 

GPIO.output(TRIG1, False) #Set TRIG as LOW 
time.sleep(2) #Delay of 2 seconds 

GPIO.output(TRIG1, True) #Set TRIG as HIGH 
time.sleep(0.00001) #Delay of 0.00001 seconds 
GPIO.output(TRIG1, False) #Set TRIG as LOW 

while GPIO.input(ECHO1)==0: #Check whether the ECHO is LOW 
pulse_start1 = time.time() #Saves the last known time of LOW pulse

 while GPIO.input(ECHO1)==1: #Check whether the ECHO is HIGH 
pulse_end1 = time.time() #Saves the last known time of HIGH pulse 

pulse_duration1 = pulse_end1 - pulse_start1 #Get pulse duration to a variable 

distance1 = pulse_duration1 * 17160 + 1.1 #Multiply pulse duration by 17150 to get distance 
distance1 = round1(distance, 2) #Round to two decimal points 

print "Distance1:",distance - 0.5,"cm","Distance2:", distance1 - 0.5,"cm" #Print distance with 0.5 cm calibration

Re: Measuring distance using 2 ultrasonic

Posted: Sat Apr 15, 2017 8:31 am
by joan
I can't help with that code.

When you get it working you could compare it with my code for a similar problem. See my answer at https://raspberrypi.stackexchange.com/q ... 0300#60300

Re: Measuring distance using 2 ultrasonic

Posted: Sat Apr 15, 2017 10:50 am
by Gavinmc42
Two

Code: Select all

 while true:
Had a quick look, obvious white space issues but that could be just be a cut paste bug.
Don't think it will get to second while true loop because it will stay in the first while loop.

Re: Measuring distance using 2 ultrasonic

Posted: Sat Apr 15, 2017 3:23 pm
by ilman
Gavinmc42 wrote:Two

Code: Select all

 while true:
Had a quick look, obvious white space issues but that could be just be a cut paste bug.
Don't think it will get to second while true loop because it will stay in the first while loop.
so what should i do? can you help me to get this code work? i really appreciate your answer, i'm a novice in this case

Re: Measuring distance using 2 ultrasonic

Posted: Sat Apr 15, 2017 5:42 pm
by Burngate
It looks to me as though the second script is more-or-less a copy of the first, but the "more-or-less" is where your problems start. You didn't manage to copy it precisely.

Python uses white spaces as controls, so you have to get them exactly right, not just more-or-less right.
Go through the second script, correcting them.

So line 6 has a space at the beginning that may confuse Python.
And when Python reaches the "while true" statement, it will continue to obey all the statements following it that are indented - but there aren't any! - so it will continue doing nothing for the rest of eternity?