Analog read to inches question

Hi, I have done numerous searches on this and can't find what I'm looking for.

I want my analog read to display in inches with 10th's. I am using a 5k ohm linear slide with 5v to track my motorcycle air shock length.

Here is the code I found:

int sensorValue = analogRead(A0); // read the input on analog pin 0:
float inches = sensorValue * (5/ 1023.0);

How can I read with whole numbers and tenths, such as 3.4"?

Thanks, adown

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.

analogRead returns an integer 0-1023
Converting the value to a float will not give you an increase in accuracy.

However, you can use sprintf to format output to your LCD or serial monitor for display purposes.

.

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?

jack wp, thanks I will look into that.

can I just put a plus sign to add 9.6(empty shock length). so it ends up in the" inches" calculation like this

It would have been orders of magnitude faster to have just tried that.

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.

lcd.print(rinches);

just add ,1

lcd.print(rinches,1);

now it will print only one decimal

adown:
So instead of 96 turning into 9.6, it's just 9.000. At 13, I get 13.00.

  float rinches = shockHeight / 10; // rear height in inches

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.