To the left is our GP2D12 calibration data. From this data, we were able to to determine a power equation to fit the data to a function. Using this equation, we wrote an function to convert the GP2D12's analog reading into a distance from the wall.
Note: The chart to the left is Analog Reading vs. Distance from wall. After, we had to flip this data so that we could convert an analog reading to a distance from the wall.
We also had to eliminate the data from our readings closer than 10 cm from the wall. This data provided analog readings that would clash with the readings in the range of 20-100 cm.
float gp2d12() {
//returns cm to wall
int averageReading = 0;
//takes an average of 10 readings.
for (int i = 0; i < 10; i++) averageReading += analogRead(A3);
averageReading/=10;
//calulates the distance from the average reading
double tmp = 36341*pow(averageReading,-1.33);
return tmp*0.843+3.5-5; //fudge factor
}
After we implemented our code, we had to implement a fudge factor. We quickly logged some data of the errors that we were off by, and wrote an equation to correct for these errors. Thankfully, our errors were linear, so it was easy to correct for them.