ADC0831 -> Scaling readings to drive a servo problems.
Turnbull2112
Posts: 65
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
' {$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
http://www.parallax.com/Portals/0/Downloads/docs/books/sw/Web-SW-v2.1.pdf
EX28-ADC0831-Simple.BS2·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
' {$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
mVolts = result */ Cnts2Mv
DEBUG "1st ",result
result = result */ 395
DEBUG "2nd ",result
result = result + 150
DEBUG "3rd ",result
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna