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?