So I have a SHARP GP2Y0A02YK0F IR sensor and I was wondering how to go about converting the value given into actual measurement of distance given the odd curve of the output value charts. I'm using just a standard Arduino UNO for the programming and for the moment a single sensor to just merely tell distance.
Currently this is my code:
// Sets up IR Sensor
int pinIR = A0 ;
// For convertToDistance()
double sensorValue = 0.0 ;
double distanceFromIR = 0.0 ;
void setup()
{
// Sets up IR Sensor
Serial.begin( 9600 );
}
void loop()
{
// For convertToDistance()
sensorValue = analogRead( pinIR ); // Reading from sensor
convertToDistance();
Serial.print( "Distance: " );
Serial.println( distanceFromIR );
delay( 600 );
}
// Converts the data read out by the sensor to a measurement of distance
void convertToDistance()
{
sensorValue = .0049;
Serial.print( "Value: " );
Serial.println( sensorValue );
distanceFromIR = 3pow ( ( sensorValue - 3 ) , 4 ) + 15; // Output on datasheet shows it not to be linear so a converstion using 3* the power of something was needed in order to get a closer to exact answer
// Current equation assuming that its sitting on top of the robot spinning and will always maintain a distance of AT LEAST 15 inches from the floor
}
I'm assuming a maintained distance of at least 15 inches at all times, how ever I'm still getting an error of anywhere from 2-5 inches. Also I'd like to find a formula that would help me encompass below 15 inches as well.