LIGHT PEN DOODLE
Learning by trial and error
SYNOPSIS
This drawing program requires BASIC, 16K and a light pen. Futurehouse of Chapel Hill, NC., and Tech-Sketch of Fairfield,NJ, are two manufacturers of light pens for Atari computers. The program runs on all models of Atari computers. 1200xl owners may have to change the statement: SETCOLOR 4,0,14 in line 1010. If you get a distorted display, change th 14 to 8 or 10.
The name "light pen' is actually a misnomer. Unlike a miniature flashlight, a light pen does not emit a beam of light. Rather, it is simply a slender rod that contains a phototransistor which responds to the light emitted by a TV or monitor screen. As a result, light pens are most responsive when there is a sharp contrast in brightness between the screen's background and the characters being displayed. Several manufacturers offer light pens for sale. These range in price from $25 to over $150. And if you're handy with a soldering iron, you can build one from scratch.
EDUCATIONAL USES
Because parents and schools often introduce children to computers at a very early age--sometimes before they can even read or spell--the children often lack the gross motor skills needed to use a joystick or keyboard. But most children are able to point. By using light pens as extensions of their fingers, young children can indicate answers even if they cannot read or spell. Thus, time that would have otherwise been spent laboriously locating numbers or letters on the keyboard can instead be spent thinking about and enjoying a program. Obviously, the light pen is not appropriate for every program or topic. But it does provide a friendly interface between the user--especially the young user--and the computer.
ATARI IMPLEMENTATION
The Atari computers use seven memory locations to control the light pen. Location 53277 sets up ("latches") the pen. It must be cleared and reset prior to each reading. Locations 53264-53267 specify which joystick port is to be used by the pen. Location 53264 indicates that it is plugged into Port 1; location 53267 indicates Port 4. This location also determines if the light pen is in use. (It normally contains a one, but its value is set to zero when the pen is placed close to the screen).
Locations 564 and 565 contain the horizontal and vertical positions of the light pen. These values do not correspond directly to the screen position for any particular graphics mode, but you can still think of the screen as a coordinate plane.
HORIZONTAL/VERTICAL POSITIONING
The light pen's horizontal position is linked to color clock units. Because TV sets and monitors are subject to over-scan, the left-hand edge of the screen is indicated by 76 in the horizontal-position register. As the pen moves across the screen, the value in the horizontal register increases until it reaches 224. The next color clock (to the right) causes the horizontal-position register to be reset to zero. If you continue to move to the right, the screen's right-hand edge will be represented by 16 in the horizontal register. Vertical position is determined by counting scan lines from the top of the screen to the pen's position. Again due to overscan, the top of the screen is represented by 16 in the vertical-position register; the bottom is represented by 111. These values are independent of the graphics mode being used, so it's necessary to scale the pen's readings to matchthe correct screen positions for the graphics mode in use.
USING YOUR LIGHT PEN
To get an idea of how a light pen works, plug your pen into joystick Port 1, then type in and run the following program:
5 GRAPHICS 0
10 POKE 53277,0:P0KE 53277,4
20 PEN = PEEK(53264)
30 IF PEN=1 THEN 10
40 H = PEEK(564):V= PEEK(565)
50 PRINT H,V
60 GOTO 10
Line 5 sets up Graphics Mode 0 so that the light pen's positions can be easily displayed. Line 10 prepare the computer for the pen. Line 20 checks to see if the pen has been used. Line 30 sends control back to line 10 if it hasn't been used. Line 40 gets the horizontal and vertical positions from the appropriate memory locations. Line 50 prints these values. Line 60 repeats the process until you press [BREAK].
VARYING READINGS
As you run this demonstration program, you may notice that successive reading can differ significantly. The degree of variation depends for the most part on the quality of your light pen. If it has an on/off switch, you have more control over the readings. On the other hand, if your pen is the basic, no-frills model(like ours), you must be very careful when you point the pen. Otherwise, you're likely to get a reading from the lamp above your TV screen.
However, even a good light pen may exhibit some degree of scatter in the horizontal position. This position is determined by counting the color clocks. Because an inexpensive pen has fewer parts, it tends to be inaccurate with respect to horizontal positioning on the screen.
There are several ways to compensate for this. First, take several readings. Then average them to determine the horizontal position, or select the middle reading, or select the most common value. You can also take successive pairs of readings until the two readings fall within a given tolerance. (Fortunately, vertical readings do not normally exhibit similar discrepancies.)
LIGHT PEN DOODLE
Light Pen Doodle is a short program that demonstrates some of the capabilities of even an inexpensive light pen. When you run the program, a white screen divided into a picture area and a color-selection area is displayed. The color-selection portion contains five rectangles. The four rectangles on the left are used to select colors; the fifth one erases the screen. This program is written in Graphics Mode 7, which restricts us to the use of four colors.
Since our light pen does not have an on/off switch, we use the keyboard to turn it on and off. Each time a key is pressed, the value in location 764 changes. A value of 255 in this location indicates that a key has not been pressed. Pressing the space bar changes the value in location 764, and thus activates or deactivates the pen.
This feature works as follows. First, select a color by pointing to one of the rectangles in the color-selection area. Then point the pen at the picture area and press the space bar to turn the pen on. To change colors, point the pen away from the picture area and press the space bar to turn it off. Select a new color, and then repeat the process.
TAKE APART
Light Pen Doodle is shown in Listing 1. Its major segments are explained below: Lines 10-120: This subroutine determines the horizontal and vertical positions of the light pen. These positions are then corrected for the TV overscan and returned in variables H and V. It isn't necessary to perform any additional scaling for Graphics Mode 7.
Lines 200-250: This subroutine determines which color is used. Color selection is determined by horizontal position of the pen. If the clear-screen selection is made, a flag is set to one.
Lines 1000-1180: This is the initialization portion of the main program. It selects Graphics Mode 7, sets up the color registers, and displays the picture and color-selection areas.
Lines 1190-1310: This is the main program loop used to doodle. It selects colors, turns the pen on and off, and displays points in the picture area. Frequent subroutine calls are made to the routines at lines 10 and 200.
CONCLUSION
This complets our quick demonstration of how a simple light pen works. We've found that there is currently a lack of information about techniques needed to interface a light pen with an Atari computer; a certain amount of trial and error will probably be required to make your pen work in a particular application. A major part of the problem is that most of the inexpensive light pens on the market today were designed as input devices for multiple-choice selection, rather than as high-resolution-graphics tools. But recent advances in light pen technology should soon change this situation for the better.
John and Mary Harrison are parents, teachers and Atari hobbyists. Mary teaches math and computer science at the high school level. John holds an M.S. in computer science and develops educational software. They coordinate the Education Department for ANTIC.
Listing: LITEPEN.BAS Download