Float division returns “inf” and 0Coding DebuggingSpeed Control L298 motorsArduino code to control 4...
How to pass a regex when finding a directory path in bash?
How would a aircraft visually signal in distress?
Why is the relationship between frequency and pitch exponential?
Russian equivalents of "no love lost"
How can you travel on a trans-Siberian train when it is fully booked?
How were concentration and extermination camp guards recruited?
After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?
Trapping Rain Water
Strat tremolo bar has tightening issues
Etymology of 'calcit(r)are'?
About the expansion of seq_set_split
How to translate “Me doing X” like in online posts?
Select items in a list that contain criteria #2
Does the growth of home value benefit from compound interest?
How to make a setting relevant?
Why does the Schrödinger equation work so well for the Hydrogen atom despite the relativistic boundary at the nucleus?
Traffic law UK, pedestrians
Translating 'Liber'
Do the English have an ancient (obsolete) verb for the action of the book opening?
What are the words for people who cause trouble believing they know better?
Bent spoke design wheels — feasible?
Did Darth Vader wear the same suit for 20+ years?
Secure offsite backup, even in the case of hacker root access
Does the "6 seconds per round" rule apply to speaking/roleplaying during combat situations?
Float division returns “inf” and 0
Coding DebuggingSpeed Control L298 motorsArduino code to control 4 led's from 4 buttonsArduino + SIM808 HTTP GET POST headers
I'm new to Arduino and I'm doing an ultrasonic project where I test for the speed of sound. My code is below and for some reason, this is what the serial port returns:
This is the measured speed of sound: inf m/s
This is the measured speed of sound: 0m/s
This is the measured speed of sound: 0m/s
etc...
Here's my code. Any help's massively appreciated:)
int trig = 13;
int echo = 11;
float target_distance = 0.3;
float recieved;
float speed_of_sound;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2000);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
recieved=pulseIn(echo, HIGH);
recieved = recieved*1000000.0;
speed_of_sound = (2*target_distance)/recieved;
Serial.print("This is the measured speed of sound: ");
Serial.print (speed_of_sound);
Serial.println("m/s");
delay(3000);
}
arduino-uno
New contributor
add a comment |
I'm new to Arduino and I'm doing an ultrasonic project where I test for the speed of sound. My code is below and for some reason, this is what the serial port returns:
This is the measured speed of sound: inf m/s
This is the measured speed of sound: 0m/s
This is the measured speed of sound: 0m/s
etc...
Here's my code. Any help's massively appreciated:)
int trig = 13;
int echo = 11;
float target_distance = 0.3;
float recieved;
float speed_of_sound;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2000);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
recieved=pulseIn(echo, HIGH);
recieved = recieved*1000000.0;
speed_of_sound = (2*target_distance)/recieved;
Serial.print("This is the measured speed of sound: ");
Serial.print (speed_of_sound);
Serial.println("m/s");
delay(3000);
}
arduino-uno
New contributor
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago
add a comment |
I'm new to Arduino and I'm doing an ultrasonic project where I test for the speed of sound. My code is below and for some reason, this is what the serial port returns:
This is the measured speed of sound: inf m/s
This is the measured speed of sound: 0m/s
This is the measured speed of sound: 0m/s
etc...
Here's my code. Any help's massively appreciated:)
int trig = 13;
int echo = 11;
float target_distance = 0.3;
float recieved;
float speed_of_sound;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2000);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
recieved=pulseIn(echo, HIGH);
recieved = recieved*1000000.0;
speed_of_sound = (2*target_distance)/recieved;
Serial.print("This is the measured speed of sound: ");
Serial.print (speed_of_sound);
Serial.println("m/s");
delay(3000);
}
arduino-uno
New contributor
I'm new to Arduino and I'm doing an ultrasonic project where I test for the speed of sound. My code is below and for some reason, this is what the serial port returns:
This is the measured speed of sound: inf m/s
This is the measured speed of sound: 0m/s
This is the measured speed of sound: 0m/s
etc...
Here's my code. Any help's massively appreciated:)
int trig = 13;
int echo = 11;
float target_distance = 0.3;
float recieved;
float speed_of_sound;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2000);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
recieved=pulseIn(echo, HIGH);
recieved = recieved*1000000.0;
speed_of_sound = (2*target_distance)/recieved;
Serial.print("This is the measured speed of sound: ");
Serial.print (speed_of_sound);
Serial.println("m/s");
delay(3000);
}
arduino-uno
arduino-uno
New contributor
New contributor
edited 7 hours ago
Ghanima
401417
401417
New contributor
asked 8 hours ago
ThisUsernameHasBeenTakenThisUsernameHasBeenTaken
252
252
New contributor
New contributor
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago
add a comment |
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Assuming the speed of sound is of the order of 330 m/s, you expect
the round-trip to the target and back to last about 1.8 ms. Then
recieved = pulseIn(echo, HIGH);
should give a value close to 1,800, and
recieved = recieved * 1000000.0;
should make received
something like 1.8 × 109.
Then
speed_of_sound = (2*target_distance)/recieved;
gives a speed of sound of 3.3 × 10−10. It is the
correct value if you want that speed in meters per picosecond, but this
is inconsistent with your program printing “m/s” after the value.
The Arduino's Serial.print()
is quite dumb with floating point
numbers. It defaults to printing two digits after the decimal point,
irrespective of the order of magnitude of the number. Thus, anything
smaller than 0.005 gets printed as “0”.
I don't quite understand why you got ∞ on the first loop. Looks like
pulseIn()
missed the pulse and returned zero, in which case the speed
is computed as 0.6 ÷ 0 = ∞.
The solution is to compute received
as
recieved = pulseIn(echo, HIGH) * 1e-6;
This can be remembered by thinking that a quantity (round-trip time) is
a numeric value (pulseIn(echo, HIGH)
) multiplied by a unit (1e-6
,
which stands for “microseconds”).
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "540"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
ThisUsernameHasBeenTaken is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65935%2ffloat-division-returns-inf-and-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming the speed of sound is of the order of 330 m/s, you expect
the round-trip to the target and back to last about 1.8 ms. Then
recieved = pulseIn(echo, HIGH);
should give a value close to 1,800, and
recieved = recieved * 1000000.0;
should make received
something like 1.8 × 109.
Then
speed_of_sound = (2*target_distance)/recieved;
gives a speed of sound of 3.3 × 10−10. It is the
correct value if you want that speed in meters per picosecond, but this
is inconsistent with your program printing “m/s” after the value.
The Arduino's Serial.print()
is quite dumb with floating point
numbers. It defaults to printing two digits after the decimal point,
irrespective of the order of magnitude of the number. Thus, anything
smaller than 0.005 gets printed as “0”.
I don't quite understand why you got ∞ on the first loop. Looks like
pulseIn()
missed the pulse and returned zero, in which case the speed
is computed as 0.6 ÷ 0 = ∞.
The solution is to compute received
as
recieved = pulseIn(echo, HIGH) * 1e-6;
This can be remembered by thinking that a quantity (round-trip time) is
a numeric value (pulseIn(echo, HIGH)
) multiplied by a unit (1e-6
,
which stands for “microseconds”).
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
add a comment |
Assuming the speed of sound is of the order of 330 m/s, you expect
the round-trip to the target and back to last about 1.8 ms. Then
recieved = pulseIn(echo, HIGH);
should give a value close to 1,800, and
recieved = recieved * 1000000.0;
should make received
something like 1.8 × 109.
Then
speed_of_sound = (2*target_distance)/recieved;
gives a speed of sound of 3.3 × 10−10. It is the
correct value if you want that speed in meters per picosecond, but this
is inconsistent with your program printing “m/s” after the value.
The Arduino's Serial.print()
is quite dumb with floating point
numbers. It defaults to printing two digits after the decimal point,
irrespective of the order of magnitude of the number. Thus, anything
smaller than 0.005 gets printed as “0”.
I don't quite understand why you got ∞ on the first loop. Looks like
pulseIn()
missed the pulse and returned zero, in which case the speed
is computed as 0.6 ÷ 0 = ∞.
The solution is to compute received
as
recieved = pulseIn(echo, HIGH) * 1e-6;
This can be remembered by thinking that a quantity (round-trip time) is
a numeric value (pulseIn(echo, HIGH)
) multiplied by a unit (1e-6
,
which stands for “microseconds”).
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
add a comment |
Assuming the speed of sound is of the order of 330 m/s, you expect
the round-trip to the target and back to last about 1.8 ms. Then
recieved = pulseIn(echo, HIGH);
should give a value close to 1,800, and
recieved = recieved * 1000000.0;
should make received
something like 1.8 × 109.
Then
speed_of_sound = (2*target_distance)/recieved;
gives a speed of sound of 3.3 × 10−10. It is the
correct value if you want that speed in meters per picosecond, but this
is inconsistent with your program printing “m/s” after the value.
The Arduino's Serial.print()
is quite dumb with floating point
numbers. It defaults to printing two digits after the decimal point,
irrespective of the order of magnitude of the number. Thus, anything
smaller than 0.005 gets printed as “0”.
I don't quite understand why you got ∞ on the first loop. Looks like
pulseIn()
missed the pulse and returned zero, in which case the speed
is computed as 0.6 ÷ 0 = ∞.
The solution is to compute received
as
recieved = pulseIn(echo, HIGH) * 1e-6;
This can be remembered by thinking that a quantity (round-trip time) is
a numeric value (pulseIn(echo, HIGH)
) multiplied by a unit (1e-6
,
which stands for “microseconds”).
Assuming the speed of sound is of the order of 330 m/s, you expect
the round-trip to the target and back to last about 1.8 ms. Then
recieved = pulseIn(echo, HIGH);
should give a value close to 1,800, and
recieved = recieved * 1000000.0;
should make received
something like 1.8 × 109.
Then
speed_of_sound = (2*target_distance)/recieved;
gives a speed of sound of 3.3 × 10−10. It is the
correct value if you want that speed in meters per picosecond, but this
is inconsistent with your program printing “m/s” after the value.
The Arduino's Serial.print()
is quite dumb with floating point
numbers. It defaults to printing two digits after the decimal point,
irrespective of the order of magnitude of the number. Thus, anything
smaller than 0.005 gets printed as “0”.
I don't quite understand why you got ∞ on the first loop. Looks like
pulseIn()
missed the pulse and returned zero, in which case the speed
is computed as 0.6 ÷ 0 = ∞.
The solution is to compute received
as
recieved = pulseIn(echo, HIGH) * 1e-6;
This can be remembered by thinking that a quantity (round-trip time) is
a numeric value (pulseIn(echo, HIGH)
) multiplied by a unit (1e-6
,
which stands for “microseconds”).
answered 6 hours ago
Edgar BonetEdgar Bonet
25.5k22546
25.5k22546
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
add a comment |
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
nice, thorough answer. (voted)
– Duncan C
5 hours ago
nice, thorough answer. (voted)
– Duncan C
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
Ah, a maths error... thats embarrasing. I assumed it was my code. Thanks for taking the time to help though! I just changed the faulty line to: recieved = recieved/1000000 and apart from the first line being "inf", it works well now.
– ThisUsernameHasBeenTaken
5 hours ago
add a comment |
ThisUsernameHasBeenTaken is a new contributor. Be nice, and check out our Code of Conduct.
ThisUsernameHasBeenTaken is a new contributor. Be nice, and check out our Code of Conduct.
ThisUsernameHasBeenTaken is a new contributor. Be nice, and check out our Code of Conduct.
ThisUsernameHasBeenTaken is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Arduino Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f65935%2ffloat-division-returns-inf-and-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you expect the division of 0.6 with 1000000 * some value?
– Vaibhav
8 hours ago