Basic Math problem: ADC output to servo pulse
Jeff
Posts: 2
Hi All,
·I'm sure this topic has been covered a bazillion times but I have not been able to dig up any post covering this subject other than on the propeller board.
I am using an ADC0831 to read an analog joystick. The values returned from the ADC are perfect 0-255. I need to convert those number to a pulse width to drive a servo full range. 1-2ms. I am using this formula but not getting the same results as the calculator.
x=ADC output #
(((x/255)*1000)+1000)/2. I get the same results with or without the parenthesis.
The values that are returned on the debug statement are, if x< 254 ·then the coverted value is 500. If x=255 then the value is 1000. I know I am about to feel even dumber, but I could really use some help. Thanks in advance.
Jeff
·
·I'm sure this topic has been covered a bazillion times but I have not been able to dig up any post covering this subject other than on the propeller board.
I am using an ADC0831 to read an analog joystick. The values returned from the ADC are perfect 0-255. I need to convert those number to a pulse width to drive a servo full range. 1-2ms. I am using this formula but not getting the same results as the calculator.
x=ADC output #
(((x/255)*1000)+1000)/2. I get the same results with or without the parenthesis.
The values that are returned on the debug statement are, if x< 254 ·then the coverted value is 500. If x=255 then the value is 1000. I know I am about to feel even dumber, but I could really use some help. Thanks in advance.
Jeff
·
Comments
x */ 1000 + 1000 / 2
That comes out with a number from 500 to 1500 as x goes from 0 to 255. Another way to write it is,
x */ 2000 + 500
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Just to make sure that I havethis right...
·x*/1000 = x/ (1000/256) or x/3.90625. The value of 1000 was used because I have 255 increments from the ADC which also happens to be the denominator of the */ operation and a range of 1000ms for my pulsout. 1000/255=3.92**. (close enough anyway)·Am I on the right track?
It works very well·by the way!·
No, it is x * 3.90625, not divided by. Look at it as a proportion,
x/256
is a number between 0 and 1 (okay, between 0 and 0.996), and then you multiply that times 1000 to get the integer that goes from0 to 996. Inside the Stamp, it multiplies x * 1000 double precision and then divides by 256.
If you want to stretch the range so that it comes out at 500 to 1000 inclusive, then fudge a little with,
y = x */ 502 + 500 ' y=500 to 1000 as x=0 to 255
(I got the desired range wrong in my previous post)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com