Shop OBEX P1 Docs P2 Docs Learn Events
Basic Math problem: ADC output to servo pulse — Parallax Forums

Basic Math problem: ADC output to servo pulse

JeffJeff Posts: 2
edited 2008-06-20 17:26 in BASIC Stamp
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
·

Comments

  • MSDTechMSDTech Posts: 342
    edited 2008-06-20 14:19
    You have just run into the problem of integer math with the basic stamp. You might look at http://www.emesystems.com/BS2index.htm. It has an excellent discussion of overcoming the limits of integer math.
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-06-20 14:51
    Probably what you want is the */ operator:

    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
  • JeffJeff Posts: 2
    edited 2008-06-20 16:33
    Thank you very much Tracy, I was just looking at your website and was working on the formula.

    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!·
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-06-20 17:26
    Hi Jeff,

    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
Sign In or Register to comment.