Joystick Calculator (New Owners Column, Lesson 5)
- Type
- Education
- Category
- Beginner's BASIC lesson program (math functions)
- Author
- David Plotkin
- Published in
- Antic, July 1986 (Vol. 5, No. 3), New Owners Column, Lesson 5: Number Crunching
- Year
- 1986
- Language
- Atari BASIC
- Requirements
- Any Atari 8-bit, Atari BASIC, joystick in port 1
About the program
"Computers weren't invented" for word processing or games, David Plotkin reminded his New Owners Column readers in July 1986. "They were invented to compute the results of numeric formulas." Lesson 5, "Number Crunching", covers Atari BASIC's arithmetic: the operators + - * / ^, the order in which BASIC evaluates them, the use of parentheses, and the built-in functions, among them SIN, COS, LOG, SQR, ABS, INT and RND. It even includes the recipe for a random whole number in a range.
The lesson's listing is "an expensive calculator", a program that makes the Atari do a job "it's vastly overqualified for". It draws a pocket calculator on the screen and you work it with the joystick. As well as the four arithmetic operations, it has trigonometric and logarithmic keys, squares, reciprocals, powers and a sign change. The code shows how each function is used.
Typing it in
There's only one special character: the inverse 7 in lines 100 and 1085 ({inv}…{/inv}: press the Atari key before and after), which is the highlighted key at the start. The calculator's frame is drawn with CHR$ codes rather than typed graphics characters, so the rest is plain text. By this lesson, Plotkin said, readers should be ready to check the listing with TYPO II.
How to use it
Move the highlight round the keypad with the joystick and press the button to "press" a key. Numbers build up in the display, and an operator or = completes a calculation. Functions such as SIN or SQR act on the number showing. Angles are in degrees, because line 90 sets DEG. If you make an error, such as dividing by zero, the display shows ERROR and you start again with CLR.
How it works
- The keypad is a string.
NMPAD$(line 105) holds the whole keypad laid out in rows of 17 characters, exactly as it is printed on screen. The program works out an index from the cursor position (INDEX=(Y-3)*17+X, line 220) and takes one character for a digit or operator, or three for a function name. - Highlighting (line 240) makes an inverse copy of the key by adding 128 to each character code, prints it over the key, and then restores the normal one.
- Logical expressions for movement. Lines 150–160 turn the joystick into steps with expressions like
(STICK(0)=7)*(X<14), which is 1 only when the stick is pushed right and there's room to move. - Functions. Lines 370–470 apply the chosen function with a chain of
IFs.TANis calculated asSIN/COSbecause Atari BASIC has noTAN, andL10usesCLOG, the base-10 logarithm.TRAP 1100catches impossible requests such as1/Xof zero and shows ERROR instead of stopping the program.
Listing
10 REM THE NEW OWNER'S COLUMN PART 5
20 REM BY DAVID PLOTKIN
30 REM (c) 1986, ANTIC PUBLISHING
90 DEG
100 DIM NM$(20),SYN$(3),H1$(3),H2$(3),NMPAD$(100):NM$="":H1$="7":H2$="{inv}7{/inv}":DOT=0:FIRST=0:NUMALL=0
105 NMPAD$="789/ SIN COS TAN 456* L10 +/- ATN 123- LOG EXP SQR 0.=+ X^2 1/X Y^X CLR"
110 GRAPHICS 0:POKE 752,1:SETCOLOR 2,0,0:REM CLEAR THE SCREEN AND MAKE BLACK BACKROUND
120 GOSUB 1000:REM DRAW CALCULATOR
130 TRAP 1100:REM READ THE STICK
140 POSITION X,Y:PRINT H1$
150 XINC=(STICK(0)=7)*(X<14)-(STICK(0)=11)*(X>1)
160 YINC=(STICK(0)=13)*(Y<7)-(STICK(0)=14)*(Y>3)
170 IF XINC=1 AND X=4 THEN XINC=2
180 IF XINC=-1 AND X=6 THEN XINC=-2
190 IF XINC=1 AND X>=6 THEN XINC=4
200 IF XINC=-1 AND X>=10 THEN XINC=-4
210 X=X+XINC:Y=Y+YINC:IF Y=7 THEN X=6
220 INDEX=(Y-3)*17+X:XLEN=(X>=6)*2
230 H1$=NMPAD$(INDEX,INDEX+XLEN)
240 H2$="":FOR LOOP=1 TO LEN(H1$):H2$(LOOP,LOOP)=CHR$(ASC(H1$(LOOP,LOOP))+128):NEXT LOOP
250 POSITION X,Y:PRINT H2$:IF STRIG(0)=1 THEN FOR WAIT=1 TO 20:NEXT WAIT:GOTO 130
252 IF STRIG(0)=0 THEN 252
253 IF H1$="CLR" THEN HOLD=0:FIRST=0:DOT=0:NM$="":SYN$="":POSITION 1,1
254 IF H1$="CLR" THEN PRINT " ":NUMALL=0:GOTO 130
255 IF LEN(NM$)>16 OR NUMALL=1 THEN 280
260 IF (X<4 AND Y<6) OR (X=1 AND Y=6) THEN NM$(LEN(NM$)+1)=H1$:REM LOOK FOR A NUMBER
270 IF (X=2 AND Y=6) AND DOT=0 THEN DOT=1:NM$(LEN(NM$)+1)=H1$:REM DECIMAL POINT ADDED TO NUMBER
280 POSITION 1,1:PRINT " ":POSITION 18-LEN(NM$),1:PRINT NM$
282 IF LEN(NM$)=0 AND FIRST=0 THEN NM$=STR$(HOLD)
285 IF LEN(NM$)=0 THEN 130
290 IF X<>4 THEN GOTO 350:REM ARITHMETIC OPERATORS
300 IF FIRST=0 THEN FIRST=1:HOLD=VAL(NM$):NM$="":SYN$=H1$:DOT=0:NUMALL=0:GOTO 130
310 GOSUB 600
345 NM$=STR$(HOLD):POSITION 18-LEN(NM$),1:PRINT NM$:NM$="":SYN$=H1$:DOT=0:NUMALL=0:GOTO 130
350 IF X=3 AND Y=6 THEN GOSUB 600:FIRST=0:POSITION 1,1:PRINT " ":GOTO 345:REM =
360 IF X<6 THEN 130
370 IF H1$="SIN" THEN HOLD2=SIN(VAL(NM$))
380 IF H1$="COS" THEN HOLD2=COS(VAL(NM$))
390 IF H1$="TAN" THEN HOLD2=SIN(VAL(NM$))/COS(VAL(NM$))
400 IF H1$="L10" THEN HOLD2=CLOG(VAL(NM$))
410 IF H1$="+/-" THEN HOLD2=-(VAL(NM$))
420 IF H1$="ATN" THEN HOLD2=ATN(VAL(NM$))
430 IF H1$="LOG" THEN HOLD2=LOG(VAL(NM$))
440 IF H1$="EXP" THEN HOLD2=EXP(VAL(NM$))
450 IF H1$="SQR" THEN HOLD2=SQR(VAL(NM$))
460 IF H1$="X^2" THEN HOLD2=VAL(NM$)*VAL(NM$)
470 IF H1$="1/X" THEN HOLD2=1/VAL(NM$)
480 IF H1$="Y^X" THEN POSITION 1,1:PRINT " ":POSITION 18-LEN(NM$),1:PRINT NM$:GOTO 300
500 NM$=STR$(HOLD2):POSITION 1,1:PRINT " ":POSITION 18-LEN(NM$),1:PRINT NM$:NUMALL=1:GOTO 130
600 IF SYN$="/" THEN HOLD=HOLD/VAL(NM$)
610 IF SYN$="*" THEN HOLD=HOLD*VAL(NM$)
620 IF SYN$="-" THEN HOLD=HOLD-VAL(NM$)
630 IF SYN$="+" THEN HOLD=HOLD+VAL(NM$)
635 IF SYN$="Y^X" THEN HOLD=HOLD^VAL(NM$)
640 RETURN
999 END
1000 REM DRAW THE CALCULATOR
1010 POSITION 0,0:PRINT CHR$(17):POSITION 18,0:PRINT CHR$(5)
1020 POSITION 0,8:PRINT CHR$(26):POSITION 18,8:PRINT CHR$(3)
1030 FOR LOOP=1 TO 17:POSITION LOOP,0:PRINT CHR$(18):POSITION LOOP,8:PRINT CHR$(18)
1035 POSITION LOOP,2:PRINT CHR$(18):NEXT LOOP
1040 FOR LOOP=1 TO 7:POSITION 0,LOOP:PRINT CHR$(124):POSITION 18,LOOP:PRINT CHR$(124):NEXT LOOP
1045 POSITION 0,2:PRINT CHR$(1):POSITION 18,2:PRINT CHR$(4)
1050 POSITION 1,3:PRINT "789/ SIN COS TAN"
1060 POSITION 1,4:PRINT "456* L10 +/- ATN"
1070 POSITION 1,5:PRINT "123- LOG EXP SQR"
1080 POSITION 1,6:PRINT "0.=+ X^2 1/X Y^X"
1085 POSITION 6,7:PRINT "CLR":X=1:Y=3:POSITION X,Y:PRINT "{inv}7{/inv}"
1090 RETURN
1100 REM ERROR ROUTINE
1110 POSITION 1,1:PRINT " ":POSITION 7,1:PRINT "ERROR"
1120 HOLD=0:FIRST=0:DOT=0:NM$="":SYN$="":NUMALL=0
1130 FOR LOOP=1 TO 250:NEXT LOOP
1140 GOTO 130
Download
The original program from the Antic disk, to LOAD or ENTER instead of typing it: NEWOWN5.BAS.
Credits
- Program listing: Joystick Calculator (New Owners Column, Lesson 5) — program listing (Antic, July 1986 (Vol. 5, No. 3), New Owners Column, Lesson 5: Number Crunching) — David Plotkin
- Picture: The joystick calculator after entering a 7. — David Plotkin (program); screenshot by the site
- Antic, July 1986 (Vol. 5 No. 3): New Owners Column, Lesson 5 - Number Crunching, by David Plotkin (atarimagazines.com) — David Plotkin, 1986
- Antic disk, July 1986 - Vol. 5 No. 3: NEWOWN5.BAS (detokenized listing) — AtariMagazines.com (Kevin Savetz), 1986
