Inputting multiple variables through SERIN to control servo positions.
fmx95
Posts: 9
Hello,
I'm working on a simple camera pan/tilt application, and i would like to have my computer send out two variables in one string, one for the position of the x servo, and one for the y.
Here is the code i am using, i've tried a few different things, but this is the one that i think should work.
The string that i am sending to the stamp is "pos: XXXX,YYYY"
XXXX and YYYY can be either 3 or 4 digits long, but i can have them set to a certain number of digits if that is what i need to do.
When i run this program, both the X and Y servos are set to the x position, and the y variable is ignored. Setting both servo positions to "cy" does nothing.
I've searched the forums but couldnt find anything like this.
Thank you in advance!
,Travis
I'm working on a simple camera pan/tilt application, and i would like to have my computer send out two variables in one string, one for the position of the x servo, and one for the y.
Here is the code i am using, i've tried a few different things, but this is the one that i think should work.
' {$STAMP BS2} ' {$PBASIC 2.5} BAUDMODE CON 16468 cx VAR Word cy VAR Word DO SERIN 16, BAUDMODE, [noparse][[/noparse]WAIT("pos: "),DEC cx,wait(","),DEC cy] PULSOUT 15,cx PULSOUT 14,cy PAUSE 20 LOOP
The string that i am sending to the stamp is "pos: XXXX,YYYY"
XXXX and YYYY can be either 3 or 4 digits long, but i can have them set to a certain number of digits if that is what i need to do.
When i run this program, both the X and Y servos are set to the x position, and the y variable is ignored. Setting both servo positions to "cy" does nothing.
I've searched the forums but couldnt find anything like this.
Thank you in advance!
,Travis
Comments
Try this:
SERIN 16, BAUDMODE, [noparse][[/noparse]SKIP 4, DEC4 cx, DEC4 cy]
This presumes there is only one type of record in the data file. This means all possible records will have "POS: " as the leading characters.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
SERIN·16,·BAUDMODE,·20,timeout,[noparse][[/noparse]WAIT("pos:"),DEC4·cx,,DEC4·cy]
the timeout is optional but might stop the program getting hung and depends on how often you are transmitting the x y values
the·send string would be similar to "pos:· xxxx· CR· yyyy· CR"
DEC4 will read in as many as 4 characters, or if it sees the CR signals the end of data cx and then reads in cy until it has 4 characters or CR
Two other points, you may be aware that the programming port 16 echoes back the transmitted values. If your terminal program does not remove the echo those same values will mix with the next transmitted string. And lastly test with a baud rate of 2400·.
Jeff T.
That was one of the methods i tried, and looking back it would have worked if i would have transmitted a CR after each variable. Thank you for your reply. [noparse]:)[/noparse]
Jeff,
This helped me a lot. The timeout and the baudrate were great tips. After i got it all working with 9600 my servos had terrible jitters, so i dropped it down to 2400 like you said and it works perfectly.
Again, Thank you both for your time!
,Travis