Controlling 2 servos w/ pushbuttons
LukeBasic
Posts: 2
Hi,
I am new to Basic Stamp 2 and programming all together. I am trying to control two servos independently using pushbuttons. I can get one to work just fine but when I add the code for a second one it does weird things. I am sure I am not coding correctly to add the second servo. I also can't figure out how to show seperate debug info for each servo. If someone could give me any suggestions I would appreciate it. Here is the code so far:
' What's a Microcontroller - ServoControlWithPushbuttons.bs2
' Press and hold P4 pushbutton to rotate the servo counterclockwise,
' or press the P3 pushbutton TO rotate the servo clockwise.
' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
duration = 750
DO
IF IN3 = 1 THEN
IF duration > 500 THEN
duration = duration - 5
ENDIF
ELSEIF IN4 = 1 THEN
IF duration < 1000 THEN
duration = duration + 5
ENDIF
ENDIF
PULSOUT 14, duration
PAUSE 10
IF IN5 = 1 THEN
IF duration > 500 THEN
duration = duration - 5
ENDIF
ELSEIF IN6 = 1 THEN
IF duration < 1000 THEN
duration = duration + 5
ENDIF
ENDIF
PULSOUT 15, duration
PAUSE 10
DEBUG HOME, DEC4 duration, " = duration"
LOOP
I am new to Basic Stamp 2 and programming all together. I am trying to control two servos independently using pushbuttons. I can get one to work just fine but when I add the code for a second one it does weird things. I am sure I am not coding correctly to add the second servo. I also can't figure out how to show seperate debug info for each servo. If someone could give me any suggestions I would appreciate it. Here is the code so far:
' What's a Microcontroller - ServoControlWithPushbuttons.bs2
' Press and hold P4 pushbutton to rotate the servo counterclockwise,
' or press the P3 pushbutton TO rotate the servo clockwise.
' {$STAMP BS2}
' {$PBASIC 2.5}
duration VAR Word
duration = 750
DO
IF IN3 = 1 THEN
IF duration > 500 THEN
duration = duration - 5
ENDIF
ELSEIF IN4 = 1 THEN
IF duration < 1000 THEN
duration = duration + 5
ENDIF
ENDIF
PULSOUT 14, duration
PAUSE 10
IF IN5 = 1 THEN
IF duration > 500 THEN
duration = duration - 5
ENDIF
ELSEIF IN6 = 1 THEN
IF duration < 1000 THEN
duration = duration + 5
ENDIF
ENDIF
PULSOUT 15, duration
PAUSE 10
DEBUG HOME, DEC4 duration, " = duration"
LOOP
Comments
If you want to display both servos duration, add a cls screen at the top, remove the home part of the debug statement that you have, and copy it to after the line of pulsout 14.
Also, your servos will move in tandem. Why? Because you're using the same pulseout duration for both. The easiest thing would be to add a second variable (duration2 for example) and use that.
Finally, when posting a whole program if it's a) large, then use the attachment manager to attach the source code, or use the formatted option to keep the code formatted nicely.
Post Edited (SRLM) : 12/18/2008 8:09:47 AM GMT