Anyone help me with this infrared distance sensor : Sharp GP2Y0A02

Ok here is my example code :

int IRpin = A0;
void setup() {

Serial.begin(9600);
}

void loop() {
float volts = analogRead(IRpin)0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65
pow(volts, -1.10);
Serial.println(distance);
delay(100);
}

I have a huge problem here. I copied this code from the store that I bought this sensor
A problem is this sensor has a range between 20 - 150 cm

Its work fine in range of 20 - 150cm but when it is out of range. the output doesnt show "Inf" or "Out of range" but it show output randomly(sorry for my bad english)

like the range that sensor detect is 180 cm and the output show 20 or 45 or 300 or 90 or 130

my project is like(just example) range finder when you are standing away from the wall more than 150 cm,nothing will happen. but if you are standing at 120 cm from the wall ,the vibration motor will vibrate.

so this shit problem is very huge problem and must be fix. I must finished this shit project within this Friday. anyone help me please

i think it is because of this code.

Try:

float distance = 65*pow(volts, -1.10); 
if(distance >= 20.0 && distance <= 150.0)
{       
  Serial.println(distance);
  delay(100);
}

No its mean. When I place the range finder 180 cm away from the object but the range finder has the maximum range at 150 cm. The output (distance) not show "inf" or "out of range",It show a number since 20-300 and inf (randomly). I want the output say "inf" every time when it farther than 150 cm

How will your program know if the distance is "out of range"?

Carefully study this graph, which shows the sensor output voltage as a function of distance to the target:

This image is show the output and the value of volts. This test is out of range (more than 150 cm)

and the output show like this. You will see the value of volts is also random.

jremington:
How will your program know if the distance is "out of range"?

Carefully study this graph, which shows the sensor output voltage as a function of distance to the target:

When it shows inf = infinite the value of volts is 0.0 (rarely happen)

What if I get 3 output value in 3 int and compare it that does it similar to others value. How can I do that?

Anyone please put the code in the comment. please...