Go Down

### Topic: Ultrasonic Sensor (Read 3112 times)previous topic - next topic

#### Vincent19

##### Jul 06, 2013, 05:53 pm
Code: [Select]
`#define trigPin 9#define echoPin 8void setup() {  Serial.begin (9600);  pinMode(trigPin, OUTPUT);  pinMode(echoPin, INPUT);}void loop() {  long duration, distance;  digitalWrite(trigPin, LOW);  // Added this line  delayMicroseconds(2); // Added this line  digitalWrite(trigPin, HIGH);  delayMicroseconds(10); // Added this line  digitalWrite(trigPin, LOW);  duration = pulseIn(echoPin, HIGH);  distance = duration*340 / 20000;        Serial.print(distance);    Serial.println(" cm");    delay(500);  }  `

Hey guys, this is the code that I get somewhere else online.

Can anyone try to explain to me why is the calculation for distance is such ? I understand that thius is the formula for distance  = time* velocity. How does that 20000 comes from ? I know that the distance that we interest in have to be divided by 2. So I guesse there is where the 2 comes from but why multiple 10 power of 4 ?

Thanks !

#### Arrch

#1
##### Jul 06, 2013, 05:59 pm
Speed of sound is ~34 cm per millisecond. pulseIn() returns a value in microseconds.

#### Vincent19

#2
##### Jul 06, 2013, 06:14 pm
Okay, now I am suck in the unit conversion. Get confused although I know the facts LOL.

Anyone ?

#### AWOL

#3
##### Jul 06, 2013, 06:25 pm
What's the question?

#### Vincent19

#4
##### Jul 06, 2013, 06:26 pm
How did I convert it ? I just get myself too confused with that

#### Arrch

#5
##### Jul 06, 2013, 06:28 pm

How did I convert it ? I just get myself too confused with that

Convert what to what?

#### Vincent19

#6
##### Jul 06, 2013, 06:29 pm
Ok to be clear, why should I divide by 20000 ?

Thanks

#### AWOL

#7
##### Jul 06, 2013, 06:31 pm
340 metres per second.
100 cm per metre.
Twice the distance (round trip timing)

#### Arrch

#8
##### Jul 06, 2013, 06:32 pm

Ok to be clear, why should I divide by 20000 ?

Thanks

Because you receive a time in microseconds, and speed travels at 0.034029 centimeters per microsecond

#### Vincent19

#9
##### Jul 06, 2013, 06:33 pm
Okay, will take some time to do the math tricks here

Thanks

By the way, why do we need the microsecond(2) in between when trigger LOW and HIGH ?

Thnaks

#### AWOL

#10
##### Jul 06, 2013, 06:35 pm
Because the sonar needs a minimum pulse length  to trigger it

#### Vincent19

#11
##### Jul 06, 2013, 06:38 pm
And the minimum pulse length is 2 microseconds ? So means thet after 2 uS, the trigger pin will be trigger to send sonar ?

Thanks

#### Arrch

#12
##### Jul 06, 2013, 07:03 pm

And the minimum pulse length is 2 microseconds ?

Don't know, what does the Datasheet say for whatever sensor you are using?

#### Vincent19

#13
##### Jul 06, 2013, 07:05 pm
I am using HC-SR04 Okay, I should read the datasheets in more details then.

Thanks !

#### keeper63

#14
##### Jul 06, 2013, 07:54 pm

I am using HC-SR04 Okay, I should read the datasheets in more details then.

Vincent19 - If there is anything you should learn from this exchange, it is this:

Magic numbers are bad.

That is what "20000" is in that piece of code - it's a blatent example of a "magic number" in programming:

https://en.wikipedia.org/wiki/Magic_number_%28programming%29#Unnamed_numerical_constants

Ideally, rather than "20000", named constants or similar (inline or block comments, for instance) would have been used to explicitly tell you, as another programmer, where and why that number was used.

You see these kinds of numbers all over the place in code everywhere (it isn't just limited to code for the Arduino, either, unfortunately); it is a hallmark of poor programming practice. There are only very rare cases when "undefined" constants may be utilized in programming (the stupid thing is invariably you'll see someone stick a comment at the end of these cases that says something like "adding one to this variable").

Learn and understand this, and don't do this in your own code; whether you intend to publish the code or not, you'll save all of us headaches and questions down the road, and you'll be a better programmer as a result.
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Go Up