Hello, KK4HFJ.
We spoke on the phone earlier. Your formula is correct, and it would work in a more Math-friendly environment such as Excel or Mathematica:
- Code: Select all Expand
outputValue = ((sensorValue * (5000/1024)) - 500)/0.133;
However, there are many quirks about arithmetic in C/C++ that will cause you problems.
When you divide an integer by an integer in C or C++, the result will actually be an integer, rounded down. So "(5000/1024)" is actually 4, which is way off from the value of 4.8828125 that you wanted. Some people resort to floating point arithmetic to fix this, but floating point arithmetic is very expensive on an AVR. A simpler solution is to multiply sensorValue by 5000 and then divide the product by 1024. I think you should cast to "long" (32-bit int) to make sure no overflows happen in the multiplication. So now we have:
- Code: Select all Expand
outputValue = ( ((long)sensorValue * 5000 / 1024) - 500 )/0.133;
That could should work for you, but because you wrote a floating point literal "0.133", your program will still be doing floating point arithmetic. An easy way to fix this is to change "/0.133" to " * 1000 / 133 ":
- Code: Select all Expand
outputValue = ( ((long)sensorValue * 5000 / 1024) - 500 ) * 1000 / 133;
You can point out to your readers that "((long)sensorValue * 5000 / 1024)" has a physical meaning on its own; it's the voltage on the sensor's output in millivolts.
--David