Shop OBEX P1 Docs P2 Docs Learn Events
BS2P Code Advice for Novice — Parallax Forums

BS2P Code Advice for Novice

BrianmcmBrianmcm Posts: 33
edited 2007-11-28 17:59 in BASIC Stamp
I'm working on a project with the hope of using a PS2 controller through a BS2P to control a track vehicle. I've mapped the buttons and joysticks on the controller to determine the input values. My request is guidance on·code that would read the values then based on the values output commands to the various functions on the vehicle. I realize that a bunch of If-Then statements should do it but I can't help but think there is a more streamlined way of accomplishing this. At first "read" it appears that the "Branch" command would do it but am I correct in that the "offsets" values have to be numerically sequenced; 0,1,2? The values on each controller button is unique and not necessarily in a·sequence.
Any guidance would be very helpful.

Brian

Comments

  • NetHogNetHog Posts: 104
    edited 2007-11-20 19:55
    IF-THEN-ELSE chains can be replaced by SELECT-CASE statements. Another approach is to use LOOKDOWN to get a numerical sequence followed by BRANCH on that sequence.

    What joystick do you have for this project?
  • BrianmcmBrianmcm Posts: 33
    edited 2007-11-21 22:33
    I'm presently using the Lynxmotion Controller and Atom Bot Board. Used the program listed in January 2007 issue of Servo, written by Pete Miles to map the buttons. The program divided the controller into six sections, each joystick was identified by two sections; one for forward and back movement, the other for side to side movement. The rocker switch on the left side of the controller was one section, the four buttons on the right was one section. However the four buttons on the front changed the output of the four buttons on the top right. Thus the section representing the four buttons, on top right,·could have up to 16 values.

    Brian
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-22 05:24
    Hi Brian, if the values from your controller are all consecutive even if they are not received sequentially you could use the instruction ON....GOSUB.

    Jeff T.
  • BrianmcmBrianmcm Posts: 33
    edited 2007-11-24 23:15
    Let me see if I have this correct: Let's say for example the code for the "Right" joystick, to move the bot forward or reverse. Based on the mapping done earlier when the joystick is moved forward the "value" reported was "128"; likewise when it is moved back the "value" is "255". so if I wanted to use the "ON-GOSUB" routine would it appear as such?
    ··Joystick VAR Byte
    Main:
    DEBUGIN "joystick", DEC joystick,
    ON·joystick·GOSUB forward,·back
    forward:
    ·pulsout, something
    ·pulsout, something
    ·GOTO main

    back:

    ·pulsout, something

    ·pulsout, something

    ·GOTO main

    Am I on the right track? As stated earlier, knowing the amount of button/joystick combinations that can be sent I'm trying to streamline the code.

    Thanks for the help

    Brian

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-11-25 02:04
    Yes thats right Brian. It needs one more instruction LOOKDOWN.

    ·Joystick VAR Byte
    ·result VAR Byte

    DEBUGIN DEC joystick
    LOOKDOWN joystick, [noparse][[/noparse]128, 255], result
    ON·result GOSUB forward, back

    LOOKDOWN matches the joystick value with the values in the lookdown table and puts the position of the value in result. Result will now·have a number 0,1,2,3... etc that can be used for the ON....GOSUB index.

    Jeff T.
  • BrianmcmBrianmcm Posts: 33
    edited 2007-11-28 17:59
    Jeff,

    Got it. Thank you so much for your help.

    Brian
Sign In or Register to comment.