I took some positions on that curve and made my multi-linear mapping...
void setup()
{
Serial.begin(115200);
}
void loop()
{
float dist = getSharpDistance(0);
Serial.println(dist);
delay(200);
}
float getSharpDistance(int sensorPin) {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
float dist = 0.0f;
if (voltage < 0.4f) {
return 0;
}
else if (voltage < 0.8f) {
dist = floatMap(voltage, 0.4f, 0.8f, 150, 70);
} else if (voltage < 1.5f) {
dist = floatMap(voltage, 0.8f, 1.5f, 70, 40);
} else if (voltage < 2.8f) {
dist = floatMap(voltage, 1.5f, 2.8f, 40, 15);
}
return dist;
}
float floatMap(float sourceValue, float sourceMin, float sourceMax, float destMin, float destMax) {
float sourceRange = sourceMax - sourceMin;
float percent = ((sourceValue - sourceMin ) / sourceRange);
float destRange = destMax - destMin;
return destMin + percent * destRange;
}
BTW I just found Jeremy Blythes blog . He used a formula he found on Sparkfuns page for the sensor
I think the source is jmatson11 and the formula is
"Distance = 16.2537 * x4 – 129.893 * x3 + 382.268 * x2 – 512.611 * x + 306.439 *Where x=Voltage read on ADC and Distance is in cm."I must try this formula!
The sharp sensor attached to "Flip" |
I attached the sensor to Flip, just under the ultrasonic sensor, giving Flip double vision. Initial tests seems fine but The USB-cable for the Arduino is a little bit bad positioned so it blocks the sharps cable.
I really should take the droid out to the shed to drill some new mounting holes for the arduino board. I just used two existing badly spaced holes and placed the arduino between them.
No comments:
Post a Comment