Shop OBEX P1 Docs P2 Docs Learn Events
Controlling 2 servos w/ pushbuttons — Parallax Forums

Controlling 2 servos w/ pushbuttons

LukeBasicLukeBasic Posts: 2
edited 2008-12-19 03:38 in BASIC Stamp
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

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-12-18 08:02
    First off, you may want to consolidate your pauses into one statement (for simplicity's sake). It doesn't really matter where you put it (as long as it's executed every time), but the end is a logical spot.

    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
  • LukeBasicLukeBasic Posts: 2
    edited 2008-12-19 03:38
    Thanks, that did the trick.
Sign In or Register to comment.