• Please review our updated Terms and Rules here

GW-BASIC joystick programming

EvanK

VCFed Founder
Joined
Aug 14, 2003
Messages
1,017
Location
New Jersey
How do I program joystick input in GW-BASIC?

It's insanely easy in Applesoft: you use the PDL(0) and PDL(1) commands to determine where the stick is located along X/Y (0-255 on each scale) and then tell it what do based on those locations.

For example, in Applesoft, my Lego robot navigation code looks like this:

3000 REM NEVIGATION
3010 FB=PDL (1):LR=PDL (0)
3020 IF FB < 75 THEN M=5
3030 IF FB > 180 THEN M=10
3040 IF LR < 75 THEN M=9
3050 IF LR > 180 THEN M=6
3060 POKE L,M: REM SEND COMMANDS

(The variable L is the address of the interface card to the control box. The variable M gets translated into binary to turn the relevant bits on/off at the control box, which runs the motors, sensors, and lights.)

But I've never had to program for joystick control in a standard BASIC; obviously Apple customized theirs make it easier.

How do I do this kind of control in GW?

Also what about reading the button presses? Over in Applesoft land its IF PEEK (49249) > 127 and IF PEEK (49250) > 127 .... nice and simple for buttons 0 and 1.
 
GW-BASIC/QBASIC's built-in joystick functions are rather slow and clunky. So it was common to read port 201h directly:

Code:
OUT &H201, 0
Btn = INP(&H201)
 
Btn1 = (Btn AND &H10) = 0
Btn2 = (Btn AND &H20) = 0
Btn3 = (Btn AND &H40) = 0
Btn4 = (Btn AND &H80) = 0

AxisX = 0
AxisY = 0
Count = 32767
OUT &H201, 0
DO
  Count = Count - 1
  Axis = INP(&H201)
  IF Axis AND 1 THEN AxisX = AxisX + 1
  IF Axis AND 2 THEN AxisY = AxisY + 1
LOOP WHILE Count > 0 AND (Axis AND 3) <> 0
 
Last edited:
So it was common to read port 201h directly

Only on faster systems. The code you posted won't work with any appreciable accuracy on XT-class systems, and likely no 286 systems either, due to the code having to run interpreted, and due to interrupts being on during the measurements which would lead to visible jitter. The BASIC/BASICA/GW-BASIC routines will produce much higher resolution readings on slow systems.

Reading the joystick buttons, however, is likely just fine grabbing port 201h values. I admit GW-BASIC's implementation of button presses as ONTIMER events is clunky.
 
Yeah my code is too slow for an XT. But you are going to get jitter with any analog joystick even using STICK. Actually in my experience STICK is useless on faster computers since it uses an 8-bit counter which overflows.
 
Back
Top