thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Ultrasonic distance measurement+ voice warning

Fri Oct 10, 2014 12:53 pm

I am working on a project involves ultrasonic sensors. i need to measure the distance and play 4 different audio when it reaches the first four feet. How i will use the GPIO pins in this matter. any help will be grateful. thank you.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Ultrasonic distance measurement+ voice warning

Fri Oct 10, 2014 1:22 pm

A Google search for "raspbian ultrasonic.sensor" quickly found this -

https://www.modmypi.com/blog/hc-sr04-ul ... spberry-pi

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Fri Oct 10, 2014 1:56 pm

Thanx for the information. the problem i'm facing is lets say when there is a object in 100cm range i need to play a audio. when it gets closer lets say 75cm-100cm i need to play a different audio. like this i need to play different audios for 50-75 and 25-50. if you have any idea to help me out is very helpful. thanx again.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Ultrasonic distance measurement+ voice warning

Fri Oct 10, 2014 2:18 pm

Why is your requirement a 'problem'?

Simply search for how to play an audio file from your RPi. Once you know how to play one audio file it will probably become clearer how you can choose which one of 4 different files to play. [There is an example of how to play an mp3 file as part of the Screaming Jelly Baby project on the Raspberry Pi Foundation resources - http://www.raspberrypi.org/learning/scr ... jellybaby/ ] Your project is similar - you are just using a distance sensor instead of pressing two paper clips together.

In other words, decomposition. 'Divide and conquer'. Tackle small parts of the problem rather than worrying about getting the whole thing right at the first attempt.

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 12, 2014 4:58 am

Thank you very much for the information and head up. i will try to work on the program and see if i can get it working. thank you
again.

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 4:43 am

hey,
I am using this code to measure the distance using ultrasonic sensors. i want to put this in a loop in order to get continues readings. can you please help me to fix my code. thank you.

regards Thama.



import Rpi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BOARD)

TRIG = 7
ECHO = 12

GPIO.setup (TRIG,GPIO.OUT)
GPIO.output(TRIG.0)

GPIO.setup(ECHO,GPIO.IN)

time.sleep(0.1)

print "start messuring"

GPIO.output(TRIG,1)
time.sleep(0.00001)
GPIO,output(TRIG,0)

while GPIO.input(ECHO) ==0:
pass
start = time.time()

while GPIO.input(ECHO) ==1:
pass
stop = time.time()

diatsnce = (stop - start) * 17000

distance = round(distance, 2)
print "Distance",distance,"cm"
time.sleep(1)

GPIO.cleanup()

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 7:50 am

You have had replies in your duplicate topic: http://www.raspberrypi.org/forums/viewt ... 84#p631584

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 8:22 am

hey,

Thank you. the above code have some syntax errors. here is a fixed version of this. can you help me to put this in a loop so i get continues reading. i tried "return distance" but its dose not seem to working.
regards,

thama.

import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BOARD)

TRIG = 7
ECHO = 12

GPIO.setup (TRIG,GPIO.OUT)
GPIO.output(TRIG,0)

GPIO.setup(ECHO,GPIO.IN)

time.sleep(0.1)

print "start messuring"

GPIO.output(TRIG,1)
time.sleep(0.00001)
GPIO,output(TRIG,0)

while GPIO.input(ECHO) == 0:
pass
start = time.time()

while GPIO.input(ECHO) == 1:
pass
stop = time.time()

distance = (stop - start) * 17000

distance = round(distance, 2)
print "Distance",distance,"cm"
time.sleep(1)

GPIO.cleanup()

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 9:08 am

Another duplicate post, and another question that it is impossible to answer because you have failed to format the code so that we can see the indentation which is part of the syntax of a python script. (Use the grey [

Code: Select all

] button at the top of the message editing screen for this forum.)

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13092
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 9:58 am

deleted duplicate post, and unlocked this first original post.

Please don't create duplicate identical questions, they will be deleted!
as white space is important in python you must use the code tags when posting python code, or the meaning of the code gets lost.

Code: Select all

while GPIO.input(ECHO) == 1:
    pass
stop = time.time()
not

while GPIO.input(ECHO) == 1:
pass
stop = time.time()

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 10:56 am

hi,
Thank you for the information on how to do a post. since im new to this forum please excuse me.

regards,
thama.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode (GPIO.BOARD)

TRIG = 7
ECHO = 12

GPIO.setup (TRIG,GPIO.OUT)
GPIO.output(TRIG,0)

GPIO.setup(ECHO,GPIO.IN)

time.sleep(0.1)

print "start messuring"

GPIO.output(TRIG,1)
time.sleep(0.00001)
GPIO,output(TRIG,0)

while GPIO.input(ECHO) == 0:
  pass
start = time.time()

while GPIO.input(ECHO) == 1:
  pass
stop = time.time()

distance = (stop - start) * 17000

distance = round(distance, 2)
print "Distance",distance,"cm"
time.sleep(1)

GPIO.cleanup()
Last edited by thamaeng on Sun Oct 26, 2014 10:59 am, edited 1 time in total.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13092
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 10:58 am

I see no indentations at all! Really? Or did you simply copy paste from your previous post?

thamaeng
Posts: 24
Joined: Fri Oct 10, 2014 12:48 pm

Re: Ultrasonic distance measurement+ voice warning

Sun Oct 26, 2014 11:02 am

I have edited the code. please have look. thank you.

regards,
Thama.

Return to “Beginners”