Shop OBEX P1 Docs P2 Docs Learn Events
servo problem — Parallax Forums

servo problem

When the GOSUB nine is executed and program ends the pointer on the servo moves a couple degrees clockwise. Why does the servo move just
before end statement? Could somebody please explain? I would like the pointer not to move when program ends. I also couldn't figure out how to post a video of the servo. Thanks in advance.




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

counter VAR Word

GOSUB three
GOSUB two
GOSUB noon
GOSUB ten
GOSUB nine
END

two:
FOR counter = 1 TO 100      '2pm position
PULSOUT 14, 500
PAUSE 20
NEXT
RETURN

noon:
FOR counter = 1 TO 100      'noon position
PULSOUT 14, 700
PAUSE 20
NEXT
RETURN

nine:
FOR counter = 1 TO 100      '9pm position
PULSOUT 14, 1250
PAUSE 20
NEXT
RETURN

ten:
FOR counter = 1 TO 100      '10pm position
PULSOUT 14, 900
PAUSE 20
NEXT
RETURN

three:
FOR counter = 1 TO 100      'threepm position
PULSOUT 14, 250
PAUSE 20
NEXT
RETURN

Comments

  • When the END statement is executed (or a STOP statement), the I/O pins get reinitialized to input mode which causes them to "float". The servo thinks this is an additional pulse and moves a little. The way to prevent this is to not let the program end. Put

    DO : LOOP

    after the GOSUB nine and before the END
  • Mike Green wrote:
    When the END statement is executed (or a STOP statement), the I/O pins get reinitialized to input mode which causes them to "float".
    Mike, I had prepared exactly that response before double checking with the BASIC Stamp manual. According to the manual for the END command,

    "Just as with the SLEEP command, pins will retain their input or output settings after the BASIC Stamp is deactivated by END. ..."

    The catch, though, is that every 2.3 seconds, the output switches to an input for 18 ms, which could cause the servo to twitch. So, in fact, your cure should work as prescribed.

    -Phil
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    A simpler solution (1 less command) is to put a STOP in place of the END statement. This is what I do when I need a program to end without the glitches.
  • I am using a standard servo which has 180 degrees of motion. The arm does travel a couple degrees clockwise upon reaching the 9pm position with the code "PULSOUT 14, 1250. "

    I solved the problem by replacing " PULSOUT 14, 1250" with "PULSOUT 14, 1100."
    Thanks for the effort anyway.

  • Why so many subroutines? Put the positions in constants and pass it to one subroutine:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'servoclock1
    three    CON   250
    two      CON   500
    noon     CON   700
    ten      CON   900
    nine     CON  1100
    
    servo  PIN 14
    
    pos      VAR  Word
    
    pos = three
    GOSUB movearm
    pos = two
    GOSUB movearm
    pos = noon
    GOSUB movearm
    pos = ten
    GOSUB movearm
    pos = nine
    GOSUB movearm
    
    END
    ' ----------------------
    movearm:
    counter  VAR  Word
      FOR counter = 1 TO 100
        PULSOUT servo, pos
        PAUSE 20
      NEXT
    RETURN
    ' ======================
    
    
Sign In or Register to comment.