distance = (duration / 2) * 0.0344;

I don't understand why I have change the speed of sound to a decimal. Even more, why would I multiply my distance by the value?

I don't even know what piece of code you're talking about. I don't know anything about what you're talking about. You should probably fill us in.

Here is my code:
#define trigPin 10
#define echoPin 13

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;

if (distance >= 400 || distance <= 2){
Serial.print("Distance = ");
Serial.println(“Out of range”);
}
else {
Serial.print(“Distance = “);
Serial.print(distance);
Serial.println(” cm”);
delay(500);
}
delay(500);
}

How far does a sound wave travel in a microsecond?

Please remember to use code tags when posting code

On packetlife.net,
Sound travels at 0.000344 meters per microsecond.

What do you mean by code tags?
I’m a newbie and this is my first time posting on the forum, so please tell me about the tags.

Read item 6 in "Read this before posting"

Thanks for the information!!!

Redeyatio:
On packetlife.net,
Sound travels at 0.000344 meters per second.

Not even close.

Redeyatio:
On packetlife.net,
Sound travels at 0.000344 meters per second.

What do you mean by code tags?
I'm a newbie and this is my first time posting on the forum, so please tell me about the tags.

I meant to say microseconds instead of seconds.

I have attached my code.

#define trigPin 10
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  float duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) * 0.0344;
  
  if (distance >= 400 || distance <= 2){
    Serial.print("Distance = ");
    Serial.println("Out of range");
  }
  else {
    Serial.print("Distance = ");
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}
float duration,

pulseIn does not return a float.

I don't understand.....
It shows me no error when I verify the code.
I'll attach what the serial monitor displays...

Distance = 86.05 cm
Distance = 86.89 cm
Distance = 86.43 cm
Distance = 2.98 cm
Distance = 86.45 cm
Distance = 86.40 cm
Distance = 11.61 cm
Distance = 3.08 cm
Distance = 86.83 cm
Distance = 4.01 cm
Distance = 86.76 cm
Distance = 86.65 cm
Distance = 87.12 cm
Distance = 86.29 cm
Distance = 86.29 cm
Distance = 87.58 cm
Distance = 87.96 cm
Distance = 88.84 cm
Distance = 87.55 cm
Distance = 88.84 cm
Distance = Out of range
Distance = Out of range
Distance = Out of range
Distance = 86.45 cm
Distance = 87.22 cm
Distance = 86.41 cm
Distance = 86.40 cm
Distance = 86.41 cm
Distance = 86.40 cm
Distance = 86.77 cm
Distance = 86.88 cm
Distance = 86.76 cm
Distance = 51.45 cm
Distance = 46.92 cm
Distance = 47.54 cm
Distance = 44.05 cm
Distance = 13.16 cm
Distance = 122.17 cm
Distance = 11.39 cm
Distance = 15.19 cm
Distance = 36.88 cm
Distance = 31.25 cm
Distance = 27.67 cm
Distance = 86.33 cm
Distance = 2.51 cm
Distance = 4.37 cm
Distance = 2.65 cm
Distance = 4.01 cm
Distance = 6.09 cm
Distance = 4.90 cm
Distance = 4.06 cm
Distance = 4.02 cm
Distance = 5.88 cm
Distance = 86.45 cm
Distance = 86.46 cm
Distance = 86.45 cm
Distance = 86.45 cm
Distance = 86.45 cm
Distance = 86.46 cm
Distance = 87.29 cm
Distance = 86.81 cm
Distance = 9.00 cm
Distance = 86.40 cm
Distance = 2.65 cm
Distance = 4.63 cm
Distance = 10.04 cm
Distance = 5.06 cm
Distance = 6.47 cm
Distance = 85.97 cm
Distance = 86.40 cm
Distance = 86.40 cm
Distance = 86.38 cm
Distance = 87.27 cm
Distance = 86.40 cm
Distance = 87.27 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.84 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.14 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.43 cm
Distance = 86.12 cm
Distance = 86.45 cm
Distance = 86.43 cm
Distance = 86.03 cm
Distance = 86.02 cm
Distance = 86.43 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.14 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.02 cm
Distance = 86.14 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.03 cm
Distance = 86.02 cm
Distance = 86.14 cm
Distance = 86.88 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.43 cm
Distance = 86.02 cm
Distance = 86.43 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.43 cm
Distance = 86.02 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.40 cm
Distance = 86.40 cm
Distance = 86.40 cm
Distance = 86.91 cm
Distance = 86.40 cm
Distance = 86.02 cm
Distance = 86.81 cm
Distance = 86.86 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.14 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.03 cm
Distance = 86.14 cm
Distance = 86.86 cm
Distance = 86.02 cm
Distance = 86.03 cm
Distance = 86.02 cm

Not every mistake is an error. The C++ compiler will happily let you shoot yourself in the foot or hang yourself from a tree or any manner of other stupid things as long as you use the right syntax.

What AWOL is saying in reply #10 is that pulseIn never returns a number with fractions. It will return 1,2 etc, not 2.71 or 3.14. So no need to use a float (and introduce inaccurate values).

Have you figured out why you have to multiply by the speed of sound? The value in your code and the result from your research should have given you a lightbulb moment :slight_smile:

So it travels 344 meters per second...
but how fast is that in microseconds???
What do I divide it(speed of sound) by?????

Delta_G:
Not every mistake is an error. The C++ compiler will happily let you shoot yourself in the foot or hang yourself from a tree or any manner of other stupid things as long as you use the right syntax.

Right…..

So the verify function only does the syntax...
And we could have entered some crazy nibbles...

Redeyatio:
So it travels 344 meters per second…
but how fast is that in microseconds???
What do I divide it(speed of sound) by???

How many microseconds are there in a second?

You weren’t one of those kids who thought math was dumb and you’d never need it in real life are you?

There are 1,000,000 microseconds in a second.
NO. I love mathematics, clears throat .
|:-|…

You know it is a unit conversion. You’re either going to multiply by 1,000,000 or divide by 1,000,000. Do you think that sound goes farther in a microsecond than it does in a second? So should the answer be a bigger number or a smaller number? Does multiply or divide give you what you need?

Delta_G:
You know it is a unit conversion. You're either going to multiply by 1,000,000 or divide by 1,000,000. Do you think that sound goes farther in a microsecond than it does in a second? So should the answer be a bigger number or a smaller number? Does multiply or divide give you what you need?

Yes, it's a unit conversion. I'm pretty sure I'm dividing. Sound goes slower in a microsecond because it's one millionth of a second. Well, Duh, its's got to be a smaller number.