<< Back to Projects

Joystick/Game Port Photogate

Measure the speed of objects for less than one dollar!

November 2005

Testing the photogates

The joystick port is a quick and dirty analog to digital converter that can be used for many more things than just joysticks and gamepads. In this project I used the joystick port to input the value of two LDRs (Light Dependent Resistors) to be used to calculate the velocity of an object. Two flashlights (or other sources of light) are placed in front of the two LDRs. The computer records the time between the blockage of the first and second sensors. Using the equation v = d / t the computer calculates the average velocity of the object.

Diagram of sensor triggering

The circuit connects to the PC via a joystick/game port. Two 100k resistors are required to make sure that the resistance never surpasses the joystick's limit when its dark. Beyond 100k the PC registers the value as zero and causes the program to function improperly. Below is a circuit diagram/schematic:

diagram/schematic

To limit the amount of ambient light to the LDRs I hot glued them in place in two halves of an empty ballpoint pen. I then glued each to a large washer, which acts as a base. Diagram:

diagram of sensors

Next I wrote a simple program in QBASIC. Stick(n) function gets the integer value of a sensor where n = the input number. 0 = Joystick #1 X-axis and 1 = Joystick #1 Y-axis (2 and 3 are for Joystick 2 and we do not need to use them.) Below is the source code of the program (which is a bit messy...) The important parts are marked in yellow.

REM "This is a program to measure the velocity of an object"
REM "passing through 2 photogates connected to the joystick port."
CLS
PRINT "Physics Photogate Velocity Program v1.0"
PRINT "By Andrew Wiens 2005"
PRINT
COLOR 15, 1
PRINT " "
PRINT " ## # # # # ### "
PRINT " # # ### ### ### ### ### ## ### ### ## # # "
PRINT " ## # # # # # # # # # # # # ## # # # "
PRINT " # # # ### ## ### ## ### ## ### # # # "
PRINT " # ### ### # ### "
PRINT " "
COLOR 7, 0
PRINT
start:
PRINT "Press C to capture X1 high light value..."
DO UNTIL INKEY$ = "c"
LOOP
XMAX = STICK(0)
COLOR 15
PRINT XMAX; " Range:"; XMAX; "-"; XMAX + 10
COLOR 7
PRINT
PRINT "Press C to capture Y1 high light value..."
DO UNTIL INKEY$ = "c"
LOOP
YMAX = STICK(1)
COLOR 15
PRINT YMAX; " Range:"; YMAX; "-"; YMAX + 10
COLOR 7
PRINT
INPUT "Distance (m):"; DIST
PRINT
PRINT "Successful!"
SLEEP 1
REDO:
CLS
PRINT "Press S to begin test."
DO UNTIL INKEY$ = "s"
LOOP
PRINT "Testing..."
PRINT "(press Q to end)"
COLOR 9
DO UNTIL STICK(0) > XMAX + 10
IF INKEY$ = "q" THEN GOTO ending
LOOP
PRINT "X1 Tripped..."
TIMEBEGIN = TIMER
DO UNTIL STICK(1) > YMAX + 10
LOOP
PRINT "Y1 Tripped..."
TIMEEND = TIMER
TTOTAL = TIMEEND - TIMEBEGIN
COLOR 7
PRINT
PRINT
PRINT "Elapsed Time:"
COLOR 15
PRINT TTOTAL; "s"
PRINT
COLOR 7
PRINT "Average Velocity: "
COLOR 15
IF TTOTAL <> 0 AND DIST <> 0 THEN
MS = DIST / TTOTAL
PRINT "M/S: "; MS
PRINT "KPH: "; MS * .2777777778#
PRINT "MPH: "; MS * .44704
ELSE
PRINT " 0 m/s"
END IF

COLOR 7
PRINT
SLEEP 2
ending:
COLOR 7
PRINT
PRINT "What now?"
COLOR 15
PRINT "1 - Start over"
PRINT "2 - Redo"
PRINT "3 - Quit"
COLOR 7
INPUT ans$
IF ans$ = "1" THEN GOTO start
IF ans$ = "2" THEN GOTO REDO
IF ans$ <> "1" AND ans$ <> "2" AND ans$ <> "3" THEN
CLS
GOTO ending
END IF

PRINT "Press any key to terminate."
DO
LOOP WHILE INKEY$ = ""

Important note: the DOS emulator in NT, 2000, and XP does not support the game port. So, you must use a DOS emulator such as DOSBox, which is a free download. Also, the object must first pass through the x-axis sensor before passing through the y-axis sensor. This is due to the nature of my program. Below is a screenshot of the running program:

screenshot

Download exe | Download QBASIC source code

View test movie

Questions/Comments may be sent to adwiens AT gmail DOT com.

<< Back to Projects