What range are you expecting, 3.5 to 9.9 inches? or what?
I would calibrate the reading using the map() function. Since map() is integer math, Use 1/10th inch increments, such as, for 5.7, the value would be 57. Then when you get the result, change it to float and divide by 10, so 57 would turn back to 5.7.
LarryD, the measurement will be constantly changing. Isn't the 5/1023 is just telling the arduino how to set the scale of 5v. Thats where the accuracy comes from, right. How should I go about this?
int val = analogRead(A0);
val = map(val, 0, 1023, 0, 50);
float inches = val * (50 / 10);
I don't think that is exactly what you want.
While taking analog readings, check measure at about 11.6 inches, and record what the analog reading is at that length
such as 11.6 inches produces 423 analog input.
Then test it at about 12.4 inches and record the reading
such as 12.4 inches produces 986 analog input.
Then the code would look something like
Convert the inches to tenth inches for integer math 11.6 inches = 116 etc.
int val = analogRead(A0);
val = map(val, 423,986 , 116, 124);
float inches = val / 10;
Of course, based on your readings, not the example numbers I came up with.
OH, Ok. I understand now. Im still waiting for my linear slide to arrive, but I want to have all of my ducks in a row as I will only have a couple days to get it working before my trip. Thanks so much for your help, adown
Hi All, never mind, I figured it out. Using the standard pot to inches calculation with some tweaking. I figured my linear pot value for full stroke and simply added the 9.6 to it like I asked in an earlier post.
Try dividing by 10.0 instead.
Dividing by 10.0, or by any number containing a decimal point, will force floating-point division.
Integer divided by integer gives you an integer.
Integer divided by float gives you a float.
In this context, 10 is an integer, but 10.0 is a float.
You could also write ((float)(10)) to cast the number 10 to floating-point.