Sensor rollover, please help.

Hi. i am trying to use a range sensor with my arduino uno, but the variable rolls over.

int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
double val = 0; // variable to store the value read

void setup()
{
Serial.begin(9600); // setup serial
}

void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

Just using that code, i check the value that the sensor gives me. when removing my hand, it reads from 1-5, meaning that there is nothing, when i get my hand closer it increases up and up till it hits about 600, then it starts to go down to 200 again, as i move my hand closer. how do i prevent this?

That is the way the sensor works, it says so in the data sheet. So there is no way to eliminate this other than using two diffrent types of sensors at the same time but that is messy.

G_M must be psychic to know what the datasheet says when the OP didn't say what the sensor was... 8)

But have a look at this chart for a Sharp sensor. It shows clearly, as G_M says, that the output drops again as you get too close. Yours seems to exhibit the same characteristic.

G_M must be psychic to know what the datasheet says when the OP didn't say what the sensor was...

It just so happens I have been looking at distance sensors for a project for a book I am writing so I knew :slight_smile: :slight_smile:
The real answer is that you can't use the sensor at this close range.

wow.. guess my researching skills could use a bit of training! thanks for the help!

(You should also remember to at least name the parts you're talking about, or better yet, link to the datasheet or supplier site :P)