IR sensor

i have tested a SHARP 2Y0A02F sensor with the following code.

int sensorpin = 49;

int ledpin = 9;

int sensorValue = 0;

int oldSensorValue = 0;

int Filter(int new_value, int old_value, int max_diff)
{
int diff_values;
int result;

if (old_value==0) // Filter is not initialized (no old value)
return(new_value);
diff_values = new_value - old_value; // Difference with old reading
if (diff_values>max_diff)
result = old_value+max_diff; // We limit the max difference between readings
else
{
if (diff_values<-max_diff)
result = old_value-max_diff; // We limit the max difference between readings
else
result = (new_value+old_value)>>1; // Small filtering (average filter)
}
return(result);
}

void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(sensorpin, INPUT);
delay(20);

Serial.begin(38400);
Serial.println("IR range finder");
delay(1500);
digitalWrite(ledpin, HIGH);
}

void loop()
{

oldSensorValue = sensorValue;
sensorValue = analogRead(sensorpin);
sensorValue = constrain(18000/sensorValue, 20, 150);
sensorValue = Filter(sensorValue, oldSensorValue, 8);

Serial.print(sensorValue);
Serial.println();
delay(500);

}

the readings from the serial monitor shows that the reading is stabilize between 82 to 84 when i try to cover the sensor with a white paper
and then take off the paper.

is this the correct reading that i am suppose to get from the IR sensor?

I don't think you want to do an analogRead() on pin 49. The analogRead() function uses analog input channel numbers, not pin numbers. You're using a Mega, right? Try connecting to one of pins 54 through 69, which are analog pins 0 through 15. So if you connect to pin 54 you would use analogRead(0).

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons

Now i am using this edited code shown below.

int sensor1Pin = 0; // Input pin

int ledPin = 49; //select pin for LED

int sensor1Value = 0;
int old_sensor1Value = 0;
int modo = 0;

long contador= 0;

//This filter limits the max difference between readings and also apply an average filter

int Filter(int new_value, int old_value, int max_diff)
{
int diff_values;
int result;

if (old_value==0) //filter is not initialized (no old value)
return(new_value);

diff_values = new_value - old_value; //difference with old reading
if (diff_values>max_diff)
result = old_value+max_diff; // we limit the max difference between readings
else
{
if (diff_values<-max_diff)
result = old_value-max_diff; // we limit the max differences between readings
else
result = (new_value+old_value)>>1; // small filtering (average filter)
}
return(result);
}

void setup()
{

pinMode (ledPin, OUTPUT);
pinMode (sensor1Pin, INPUT);
delay(500);

Serial.begin(38400);
Serial.println("IR Range finder v0.6");
delay(500);
digitalWrite(ledPin, HIGH);

}

void loop()
{ //read values from sensor
contador++;
old_sensor1Value = sensor1Value;
sensor1Value = analogRead(sensor1Pin); //reading the sensor

// convert sensor units to cm (aprox)
sensor1Value = constrain(18000/sensor1Value, 20, 150); //limit to 20-150cm

sensor1Value = Filter(sensor1Value, old_sensor1Value,8); // filter range data

Serial.print(">>"); // line start
Serial.print(sensor1Value); // output sensor value...
Serial.println(); // line end

delay(250);
}

When the sensor was not covered with anything, the reading is 149. (which is correct)

However when i used my hand to completely cover the sensor, the reading is around 69 to 70. (which is wrong ?)

My question is that why when i completely covered my hand on the sensor, the reading is not 20 ?

I don't think the sensor is meant to be completely covered. A more interesting test is to hold an object at a far distance then slowly bring it closer to the sensor and watch the readings change. There is a point beyond which (i.e., closer than) the sensor does not work properly.

--
The Quick Shield: breakout all 28 pins to quick-connect terminals