Shop OBEX P1 Docs P2 Docs Learn Events
Help with Setting timer and varying time of pulsout — Parallax Forums

Help with Setting timer and varying time of pulsout

Bray12Bray12 Posts: 3
edited 2012-03-12 16:15 in BASIC Stamp
I'm programming an automatic gate model. I need the gate to open on input of IN3, stay open for 10 seconds and then close. The program below does this, however I would like the servo to move slower. When I introduce a second variable to the subroutine e.g" for counter = 950 to 400, pulsout 13, counter" it results in the subroutine taking longer, as you would expect. I don't know how to slow the servo down without effecting the duration of the subroutine.
Any help is really appreciated, thanks.

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

closetimer VAR Word
counter VAR Word

DO
IF ( IN3 = 1) THEN
GOSUB fob_open
ENDIF
LOOP
'
(subroutines)

fob_open:

FOR closetimer = 1 TO 300
PULSOUT 13, 400
PAUSE 20
NEXT

PULSOUT 13,950

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-12 06:44
    Your FOR loop in the subroutine specifies 300 steps for the servo movement which are all the same (400 x 2us = 0.8ms pulse width + 20ms delay). That's a total servo movement time of 20.8ms x 300 = about 6 seconds. In your case, the servo moves as quickly as possible to the specified position, then sits there for the rest of the 6 seconds. You want to spread this movement out. The starting pulse width is 950 (1.9ms) and you want to move to 400 (0.8ms) over a period of roughly 6 seconds. That would be:
    FOR closetimer = 0 TO 548 STEP 2
       PULSOUT 13, 950 - closetimer
       PAUSE 20
    NEXT
    
    Note that the pulse width value starts at 950 and, over roughly 300 steps, decreases to the desired final value of 400. There are 275 steps because that divides the desired difference in pulse width values exactly. Now you may want the servo to move faster and pause at the open setting for part of the time. You'd just increase the step size so the loop takes less time, then have a 2nd loop that holds the servo in position (pulse width 400) for some period of time. I notice that, at the end of your subroutine, you start to close the gate (back to 950). A single pulse won't really accomplish that. You need another loop since it takes the servo a bit of time to move the distance and it needs control pulses every 20ms.
  • Bray12Bray12 Posts: 3
    edited 2012-03-12 14:48
    Hi Mike,
    Thank you very much. Your help was spot on. Here's what worked:

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

    opentimer VAR Word
    closetimer VAR Word
    counter VAR Word

    DO
    IF ( IN3 = 1) THEN
    GOSUB fob_open
    ENDIF
    LOOP
    '
    (subroutines)

    fob_open:
    FOR opentimer = 0 TO 548 STEP 4
    PULSOUT 13, 950 - opentimer
    PAUSE 20
    NEXT
    FOR counter = 1 TO 420
    PULSOUT 13, 400
    PAUSE 20
    NEXT
    FOR closetimer = 400 TO 970 STEP 4
    PULSOUT 13, closetimer
    PAUSE 20
    NEXT
    RETURN

    Felt great watching it work! My next step is to introduce a sensor to allow the gate to open on approach. I plan to add a new subroutine, very similar to the last, that's called on initiation of an input similar again to the existing IN3. I have a fair idea of what to try as I've completed "what's a micro controller" and built the BOE bot a couple of years ago, but I'm sure I will be in need of more support when the code is assembled and I've not managed to get it working!
    Thanks again for your help,
    Jim.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-03-12 16:15
    Just so you know ... Your fob_open subroutine uses 3 different word variables. The Stamp has only a limited amount of variable storage (26 bytes or 13 words). It probably won't make any difference for your program, but, for a much more complex program, you will need to conserve variable space. It's better to use the same variable for all 3 FOR loops. You could use one called counter or timer or you could use 3 different names if that's helpful, but use an alias definition so the 3 names use the same variable space (see the Stamp Manual or Stamp Editor help files for details).
Sign In or Register to comment.