Measuring distance

Hello i have "stolen" som code of the internet that works rather fine :slight_smile: However i can't for the life of me figure out the logic of it :slight_smile: Can someone explain (in details) to me what happens in the code below, i know it is to converting volt to distance, i just don't understand how and why it looks like this :slight_smile:

double get_IR (uint16_t value) {
if (value < 16) value = 16;
return 2076.0 / (value - 11.0);

Thank you :slight_smile:

double get_IR (uint16_t value)  //a function that takes a 16 bit integer named value as a parameter and returns a double
{
  if (value < 16)  value = 16;  //test the value of the parameter passed and if it is less than 16 set it to 16
  return 2076.0 / (value - 11.0);  //do a calculation and return the result

OK, that is the explanation of what the code does, but without seeing the entire program it is impossible to say why it does it or what the "magic" numbers mean.

Hello i have "stolen" som code of the internet that works

You need to go back there to learn what those magic numbers mean, for your mysterious sensor. Hope you don't get arrested.

Can you tell us what hardware you are using?

KeithRB:
Can you tell us what hardware you are using?

KeithRB:
Can you tell us what hardware you are using?

Yes i am using a Sharp sensor (“SHARP GP2Y0A21YK0F") :slight_smile:

The code looks like that because of the details of how volt corresponds to distance given the hardware setup. But there's no real way to work out exactly what's going on because the person that wrote that function didn't bother to give the variables meaningful names. Value (volt?) ranges between 11 and 16. 11 is some sort of overhead that gets subtracted to get the real value. But why it's divided, why a value of 11 would mean a IR of infinity, heck - I dunno.

PaulMurrayCbr:
The code looks like that because of the details of how volt corresponds to distance given the hardware setup. But there's no real way to work out exactly what's going on because the person that wrote that function didn't bother to give the variables meaningful names. Value (volt?) ranges between 11 and 16. 11 is some sort of overhead that gets subtracted to get the real value. But why it's divided, why a value of 11 would mean a IR of infinity, heck - I dunno.

Ya, that annoyed me too :frowning:

What annoyed me was the cross-posting if this question.
But I've deleted it now.

What annoys me, is that some people are arrogant and quite aggressive in these forums some times :slight_smile: I am a noob, i never use arduinos, i was told that these forums are good for learning, so of course im gonna ask smart people about the same question, cause i wanted an answer :slight_smile:

To everyone else, thank you for your answers... i have decided to go straight for the source and read the analog voltage read from the sensor instead of converting it into cm :slight_smile:

of course im gonna ask smart people about the same question, cause i wanted an answer

And imagine someone answers the question in one place, only to find it has already been answered in another, thereby making the person less likely to waste their valuable time on a thoughtless noob next time.

You say "aggressive", I say "prudent".

What annoys me, is that some people are arrogant

Technically, it isn't arrogant if you actually are superior. :smiley:

@sweetamber: not sure what happened to your rapier-wit riposte, but let me explain how it works:

If you cross-post, and you get two responses, you're happy because you've got two replies to compare.
However, you've potentially off-pissed one or both of the respondents, because one of them has wasted their time, and the other might think how easily the tables could have been turned, and he'd ben the second to answer.

If however, to imagine another scenario, both your posts were seen by one member who then reported it to a moderator, you've definitely off-pissed two people, and now the moderator is going to pause to read the report, confirm that the posts were identical, go to one of them, and delete it, thereby wasting his or her time.

Now, moderators have the powers to delete posts, but also, crucially for you, they have the power to suspend or cancel user accounts, particularly in the case of serial cross-posters.

Does that make sense now?

Edit: I found your riposte - it was in the bin.
I didn't put it there, so it was either you or another moderator.
If it was you who put it there - Good call.