Shop OBEX P1 Docs P2 Docs Learn Events
Variable Pin # in PULSOUT — Parallax Forums

Variable Pin # in PULSOUT

John CoutureJohn Couture Posts: 370
edited 2008-04-12 19:17 in General Discussion
I feel like I am missing something obvious.· I want to put the Pulsout command into a subroutine and be able to supply it with a variable pin number.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John J. Couture

San Diego Miramar College

Comments

  • VelocitVelocit Posts: 119
    edited 2008-04-12 04:19
    You can't pass pins through subroutine parameters, nor can you use a variable to declare a pin in the PULSOUT command. Also, the intermediate variables, ServoNum and ServoDur, aren't necessary ; you can save memory by doing away with them. Here's an example of how you might implement your code:

    Main:
      SERVO 1,150
      SERVO 0,100
      goto main
    
    ' -----------------------------------------------------------
    ' Subroutine Code
    ' -----------------------------------------------------------
    SERVO:
    ' USE: SERVO servo#, pulse duration
        IF __PARAM1 = 1 THEN
            PULSOUT Servo_Grip, __PARAM2
        ELSE
            PULSOUT Servo_Base, __PARAM2   
        ENDIF
    ENDSUB
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Paul

    Post Edited (Velocit
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-04-12 15:53
    The cool thing about SX/B is that you can create you own routines. I just wrote this off the top of my head but am pretty confident it will work fine:

    ' Use: PULSE_OUT pin, timing
    ' -- pin is 0 to 15 (RB.0 to RC.7)
    ' -- timing is in microseconds
    
    SUB PULSE_OUT
      IF __PARAMCNT = 2 THEN
        tmpB1 = __PARAM1
        tmpW1 = __PARAM2
      ELSE
        tmpB1 = __PARAM1
        tmpW1 = __WPARAM23
      ENDIF
      
      tmpW2 = 1 << tmpB1                            ' create pin mask
      RBC = RBC XOR tmpW2                           ' toggle the pin
      PAUSEUS tmpW1                                 ' wait
      RBC = RBC XOR tmpW2                           ' toggle back
      ENDSUB
    
  • John CoutureJohn Couture Posts: 370
    edited 2008-04-12 16:12
    Interesting.· Thank you all.· We'll try that in class and see what happens!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-04-12 19:17
    John,

    Note that my version (above) does not make the pin an output; you should make any pin that will be pulsed an output and it the non-pulse state before using the routine.

    Another great thing about SX/B is that you can mix assembly into BASIC, and use interrupts. I've attached reasonably-simple program that has an eight-channel virtual servo controller connected to RB.0 to RB.7. EFX-TEK customers do a lot of servo animations so this framework makes things easy on them; all they have to do is tell a servo where to be in the foreground (via the pos array) and the ISR takes care of the rest -- for up to eight servos.

    Post Edited (JonnyMac) : 4/12/2008 9:41:46 PM GMT
Sign In or Register to comment.