Click here if you are stuck in someone else's frames.
Joysticks with QuickBASIC

As you may already know, QuickBASIC and QBasic provide functions that enable programs written in BASIC to communicate with the joysticks.  To read a joystick coordinate, use the STICK(n) function with 'n' set to a value between 0 and 3.  These 'n' values correspond exactly to the 'joyPotVal' array used in our programming examples previously.  One thing to note though is that you must use STICK(0) in order to update all the values retrieved in any of the other STICK(n) function calls.

The joystick button functions allow access of the joystick button status.  You can use the STRIG(n) functions to access the state of any of the 4 standard joystick buttons (2 per stick).  I won't go into detail as to how these functions work since the online help system for QBasic provides much information necessary to make good use of these functions.

Some programmer's of QuickBASIC and QBasic alike feel that these functions, especially STICK, are slow.  It is possible to write code in QBasic that accesses the joystick port directly.  Here, we'll create a variation of the STICK function that reads a joystick pot value directly.  We will also write a function in QBasic that returns the joystick button status using direct port reads.

    ' JOYSTICK.BAS -- Reads joystick coordinates and buttons.
    '
    ' by Gary Neal, Jr. -- garyneal@oocities.com

    FUNCTION Joystick&(Axis AS INTEGER)
        DIM potVal AS LONG             ' Longs are slow but...
        DIM potLimit AS LONG           ' ...Basic has no unsigned type.
        DIM bitMask AS INTEGER

        bitMask = 2 ^ (Axis AND 3)     ' Get bit mask value.

        OUT &H201, 0                   ' Discharge joystick caps.

        WHILE (INP(&H201) AND bitMask) <> 0 AND potLimit < 65536
            potVal = potVal + 1
            potLimit = potLimit + 1
        WEND

        Joystick& = (potVal AND 65535) ' Return joystick pot value
    END FUNCTION

    FUNCTION JoystickButton%(bitMask AS INTEGER)
        JoystickButton% = ((NOT INP(&H201)) AND bitMask)
    END FUNCTION

There is one apparent advantage of the 'Joystick&' function above the 'STICK' function in QBasic.  It doesn't require that you use 'Joystick&(0)' first before retrieving the coordinates for 1, 2, or 3.  However, the values returned from this function may not be stable since it cannot disable hardware interrupts prior to timing the charge time on the joystick caps.  Unfortunately, there is no way from within QBasic or QuickBASIC to enable and disable hardware interrupts (outside of using CALL ABSOLUTE for an assembler call).  If you plan to use this function 'as is,' your program may have to adjust for this possible instability.

Try it either way, using BASIC's STICK function and the Joystick& function, and see which one you like better.  I've seen programs written in QBasic that use direct port reads to get joystick coordinates work quite well.  Even without disabling interrupts.

Previous Page | Main Page | Next page

Send your questions, comments, or ideas here.