Shop OBEX P1 Docs P2 Docs Learn Events
ADC0831 -> Scaling readings to drive a servo problems. — Parallax Forums

ADC0831 -> Scaling readings to drive a servo problems.

Turnbull2112Turnbull2112 Posts: 65
edited 2010-02-15 20:37 in BASIC Stamp
I have been at this for a couple of weeks, (about to throw it off my balcony!!). I am trying to get this thing running so I can sell some of my machines. Here is what I'm doing. I am reading a voltage from an ADC0831. I want to take the result from the ADC and scale it to drive a servo which usually takes 500 to 1000 to equal the servo's full 180* travel. I actually only need 90* or so but I can't get the thing to go further than 60*. The minimum result from the ADC is "51" and the max is "255" I need this to = 0* of travel through 90* + degrees of travel. HELP!! I am about to give up. Here is my program. I left the scaling part blank. Any help would be greatly appreciated. Again, I am not a student or anything, this is for a work problem I am trying to solve so if the solution is obvious to someone then please tell me what it is. THANKS!

' {$STAMP BS2}
' {$PBASIC 2.5}



CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 9

Cnts2Mv CON $139C 'x 19.6 (to mV)

result VAR Byte
mVolts VAR Word


Main:
DO

GOSUB Read_0831
mVolts = result */ Cnts2Mv


result = result */ 11
result = result + 500
PULSOUT 9, result


PAUSE 20
LOOP

'
[noparse][[/noparse]subrtnes]

Read_0831:

LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [noparse][[/noparse]result\9]
HIGH CS
RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-02-14 22:45
    Look in Stamp Works the link is here

    http://www.parallax.com/Portals/0/Downloads/docs/books/sw/Web-SW-v2.1.pdf

    EX28-ADC0831-Simple.BS2·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Turnbull2112Turnbull2112 Posts: 65
    edited 2010-02-14 22:52
    Yea, I got the original base for my program from there. The "result" is the value I'm trying to scale to drive the servo. The voltage measurement part is working fine. Thanks,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rob Turnbull
    Audaci Favet Fortuna
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-02-14 23:28
    ·I know this feeling· ·(about to throw it off my balcony!!).···· Put down for a few days it will come to you how to fix this problem that you are· having

    smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Turnbull2112Turnbull2112 Posts: 65
    edited 2010-02-14 23:46
    HAHAA!! My girlfriend just told me the same thing! It's already been put away. I'll let this post soak for a while and hopefully someone will throw me a bone here. Thanks again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rob Turnbull
    Audaci Favet Fortuna
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-14 23:46
    Change 'result' to a WORD value. It's 9 bits (or more) and you're trying to store it in an 8-bit byte.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-02-15 00:07
    Did you do EX26 and EX27 this might help as well

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Turnbull2112Turnbull2112 Posts: 65
    edited 2010-02-15 13:19
    I am starting to make some progress now. I am still unable to get the 90* or even 180* throw that I'm looking for. It seems that I am not getting the "bottom end" 1.0ms pulse and the servo hits the top end of its travel at the max voltage from the ADC. Here is the program. Is my timing off? Should I use HEX with the */? HELP!! Thanks

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    CS PIN 0
    Clock PIN 1
    DataIn PIN 2
    SERVO PIN 9

    Cnts2Mv CON $139C 'x 19.6 (to mV)

    result VAR Word
    mVolts VAR Word

    Main:
    DO
    HIGH 8
    GOSUB Read_0831
    mVolts = result */ Cnts2Mv

    result = result */ 395
    result = result + 150
    PULSOUT 9, result

    PAUSE 20
    LOW 8
    LOOP

    '
    [noparse][[/noparse]subrtnes]

    Read_0831:

    LOW CS
    SHIFTIN DataIn, Clock, MSBPOST, [noparse][[/noparse]result\9]
    HIGH CS
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rob Turnbull
    Audaci Favet Fortuna
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-15 15:06
    I suggest you use DEBUG to look at the intermediate results of your calculations like:

    mVolts = result */ Cnts2Mv

    DEBUG "1st ",result
    result = result */ 395
    DEBUG "2nd ",result
    result = result + 150
    DEBUG "3rd ",result
  • Turnbull2112Turnbull2112 Posts: 65
    edited 2010-02-15 15:45
    I put that in there and the debug terminal spit out a bunch of gibberish. Do you have any idea what the calculation should look like to get the scaling to work? The "result" is reported as 51 through 255 in the debug terminal. What do I put in there to get the full range between 1.0ms to 2.0ms. I keep asking this question and no one has given me any answers related directly to the question. It seems like things don't add up from the stamp projects in the book to when I try to combine different projects. Whats the point of all of this if the only use for these things is to do the book projects?!?!? If there is someone out there on this site that wants to sell a few hundred BS2's then please give me something I can use, otherwise I am going to have to bail. Sorry if I sound grumpy but I am quickly going in that direction.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rob Turnbull
    Audaci Favet Fortuna
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-15 17:19
    I'm sorry. I forgot the "DEC" before the "result". The DEBUG statement should be like: DEBUG "1st ", DEC result

    That's what I get for switching back and forth among several different programming languages.

    If you have a "result" range from 51 through 255, you want to transform that to a range of 500 through 1000 for use with the PULSOUT statement and a BS2.

    For an approximate fit, try multiplying by 4 and adding 150. That will give you a range of 354 through 1170. (like PULSOUT 9,result*4+150)

    For a more exact fit, you'd want: newResult = (oldResult-51) * ((1000-500)/(255-51)) + 500
    or: newResult = (oldResult - 51) * 2.45 + 500

    This comes out to: PULSOUT 9, ((result-51)*/627)+500
  • Turnbull2112Turnbull2112 Posts: 65
    edited 2010-02-15 20:37
    MIKE!! You are the man! It is finally working. Thanks for you patience with me in this matter.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Rob Turnbull
    Audaci Favet Fortuna
Sign In or Register to comment.