I have been playing arround with sharp distance sensor enough and found an issue with inconsistency of the sensor readings. Adding capacitors does help to filter out some bad signals but still the issue seems hard to completely eliminate. That said, beside adding capacitors to sensor as manufactor suggested, you can add some codes that helps smooth out the signal further more. One of the idea is to make several readings then average them as follow.
public static float GetAvgReading (FEZ_Components.DistanceDetector sensor, int x)
{
float avg = 0;
for (int ii = 0; ii < x; ii++)
avg += sensor.GetDistance_cm();
return (avg / x);
}
With that in place you can use it as follow:
private static FEZ_Components.DistanceDetector sensorR = new FEZ_Components.DistanceDetector(FEZ_Pin.AnalogIn.An1, FEZ_Components.DistanceDetector.SharpSensorType.GP2D120);
private static FEZ_Components.DistanceDetector sensorL = new FEZ_Components.DistanceDetector(FEZ_Pin.AnalogIn.An2, FEZ_Components.DistanceDetector.SharpSensorType.GP2D120);
//get an avg of 10 readings
Debug.Print("L: " + GetAvgReading(sensorL, 10) + ", R: " + GetAvgReading(sensorR, 10));
Please Note:
Posted method is pretty simple just to give you an idea what you can do to improve analog readings and not intended to be used for everything out there. In order to reduce error rate on the sensor that I used to test, MarkH has helped me the other night trying to derive best parameters to be used but due to late night, we have not completed it. MarkH may have a better code that does this?
Thanks!