CHAPTER THREE :
HOW TO USE TI BASIC
PRACTICAL PROGRAM WRITING
Before you switch your console on to write a program, or even
gather a large pile of paper to work on, sit and think about
your proposed program.
Work out what your program is to do, and try to split it into
small blocks of tasks to be accomplished. Then you can write the
coding for each block, and check it to ensure you have made no
mistakes, before moving to the next block.
It is much easier to write a small program that works well
than to write in one go a 15k program : it is improbable that it
will work first time, and you will be faced with a lot of
checking!
Some experienced programmers can just sit at their console
and input a new program,but certainly at first, you should write
your proposed program down on paper. Check the flow of the
program before you input it: can any variable or input reach a
value which would cause the computer problems? If so, is an
'error trap' required, or does the program need rethinking?
The Basic language is in some ways similar to English: the
same task can be accomplished in several ways, but some ways are
more efficient than others. A method which works well in one
program may be inappropriate in another, therefore it is not
possible to give any more than the most general guidelines.
A digital stopwatch can be an advantage when you are trying
to find the quickest way of doing something. Usually a single
process is too fast to time, but place it in a loop.
(A loop is a part of the program which is repeated several
times, until a particular condition is met. In the following FOR
... NEXT loop the condition is met when the variable I reaches a
value of 1000).
FOR I=1 TO 1000 CALL HCHAR(3,4,45) NEXT I
To bring the line you want on screen, key in the line number,
then hold FCTN down and press key E or X. Your line will appear
with the cursor at the beginning of the line.
Use FCTN and keys S and D to move the cursor over the line
without deleting or altering anything.
If you wish merely to alter the line, typing over it may be
sufficient, but the 99/4A also allows you to delete and insert
characters.
To DELETE a character,place the cursor over it (FCTN plus S
or D) and then press FCTN and key 1. This will delete the
character the cursor is on, and everything following will move
one space to the left.
To INSERT text, place the cursor after the last character you
wish to leave untouched, using FCTN and S & D, then press FCTN
and key 2. Now anything you type will force everything after the
insert point one character to the right,and your inserted text
will appear. The cursor position also moves to the right as you
type. You leave INSERT mode by pressing FCTN and S or D, or
pressing ENTER.
When your line is correct, press ENTER to enter the new line
into memory, and leave EDIT mode. However, if you wish to amend
also the line before or after the line just finished, you may
move directly to that by pressing FCTN and E or X.
The maximum line length in TI Basic is four screen lines, but
the computer is often capable of taking a longer line, as the
absolute restriction is on the length of the line in BYTES of
memory used, not the number of characters on the screen.
You may use the edit function to insert 'overlong' lines as
follows:
Type in the last part of the required line first. Press ENTER.
With the line on screen, press FCTN and key 2, then key in
the first part of your line,from the beginning. The part you
first entered is pushed to the right.
Now return the line to the screen using the edit mode: type in
the line number and then press FCTN and key X.
It is possible to overfill a line this way, and you will receive an error message if you do. However you will usually be able to go to an extra half screen line, and in some cases you may be able to squeeze two extra lines in!
The advantage of putting as much as possible in a line is
that by using less program lines you save a little memory. In
general, lines with a lot of numbers in will be difficult to
expand in this fashion, but lines with a lot of text or commands
can usually be considerably extended.
When the line is fully entered,press ENTER. Then LIST the
line just to make sure it is all right.
You do not have to INSERT at the beginning of the line, but
you may find it easier to do so. Give the computer time to move
all the characters to the right when using INSERT. Keep your
eyes on the screen.
A brief word of warning: in TI BASIC, your program uses the
same area of memory as the values of variables, separated by a
'marker' in memory. As a program runs, the free memory is
continually filling. Unwanted values are only purged when the
memory is full, resulting in short pauses in program operation.
If the variable area of memory is almost full when you stop
to edit a program, inserting extra material MAY result in the
permanent loss of the marker which marks the limit of the actual
program. This will cause irreversible damage to your program and
may prevent the LIST function from operating correctly, or cause
a system lockout when you try to run the program.
Therefore try to avoid running the program and then editing
it! Before you edit, take a copy of the program to tape - this
has the added benefit of apparently clearing the garbage, and
not only gives you a security copy but actually prevents the
problem occurring!
This problem will not be apparent if you use extended basic
plus the 32k ram expansion, as variables then occupy a different
sector of memory.
DEBUGGING
Having entered your program, you type in RUN, and instead of
the program running, you receive an error message. DEBUGGING is
required.
A BUG is quite simply an error in the program, either a
mistype or an error in your use of Basic.
The computer does check the lines you enter, but will only
spot such things as using only a single bracket ( or quotation
mark ".
When you RUN your program, the computer first goes through
your program and sets aside memory for each variable and sub
program that you have used. During this 'prescan' further errors
may be spotted and an error message printed on the screen.
Typical errors spotted at this time are incorrect use of arrays
(trying to use DIM after the variable has been used) or a
mismatched number of FORs and NEXTs.
Most errors will only produce an error message as the
computer finds them when your program is actually running - for
instance trying to GOTO a non existant line number, or trying to
RETURN when there is no outstanding GOSUB.
The error messages generated by the TI99/4A are well
described in the manual, and will usually indicate a line number
in which the computer has met something it cannot cope with.
Unfortunately, the actual error may not be in the line stated
in the error message.
For instance, BAD VALUE IN 100 may refer to:
100 CALL HCHAR(ROW,COL,42)
FOR I=1 TO 5 READ A VAR(I)=A NEXT I DATA 23,54,8,A,5
This simple graphics demonstration program uses several of the
features of TI BASIC described in the preceding article.
The descriptive text which follows will help you to follow the
program.
To enter this program, select TI BASIC, then ENTER the word NUM.
This will automatically provide the line numbers and you will only
have to type in the remainder of the lines.
100 REM DEMO OF COLOUR
110 REM IN TI BASIC
120 REM
130 RANDOMIZE
140 PRINT " "::::"ONE MOMENT
..."::::::
150 DIM F(100)
160 DEF RAN(A)=INT(A*RND)+1
170 M$="0"
180 FOR CHARR=32 TO 152 STEP
8
190 CALL CHAR(CHARR,M$)
200 NEXT CHARR
210 FOR SET=1 TO 16
220 CALL COLOR(SET,16,SET)
230 CALL VCHAR(1,2*SET-1,24+
SET*8,48)
240 NEXT SET
250 FOR SET=1 TO 9
260 CALL HCHAR(22,2*SET,48+S
ET)
270 NEXT SET
280 CALL HCHAR(22,20,48)
290 FOR SET=11 TO 16
300 CALL HCHAR(22,2*SET,38+S
ET)
310 NEXT SET
320 FOR SET=10 TO 16
330 CALL HCHAR(22,2*SET-1,49)
340 NEXT SET
350 FOR X=1 TO 1000
360 NEXT X
370 FOR SET=3 TO 14
380 CALL HCHAR(2*SET-5,1,24+
SET*8,32)
390 NEXT SET
400 FOR X=1 TO 4000
410 NEXT X
420 INPUT " ENTER TO CONTINU
E":A$
430 CALL CLEAR
440 CALL SCREEN(12)
450 FOR X=1 TO 150
460 R=RAN(24)
470 VR=RAN(32)
480 SET=RAN(16)
490 CALL HCHAR(R,VR,24+SET*8)
500 NEXT X
510 FOR X=1 TO 50
520 R=RAN(24)
530 VR=RAN(32)
540 SET=24+8*RAN(16)
550 NO=RAN(32-VR)
560 CALL HCHAR(R,VR,SET,NO)
570 CALL HCHAR(25-R,33-VR,24
+8*RAN(14),NO)
580 CALL VCHAR(1+VR/2,R/1.5+
1,SET,3/4*NO)
590 NEXT X
600 FOR COUNT=32 TO 152 STEP
8
610 CALL CHAR(COUNT,"88CCEEF
F88CCEEFF")
620 CALL CHAR(COUNT+1,"80C0E
0F0F8FCFEFF")
630 CALL CHAR(COUNT+2,"7F3F1
F0F070301")
640 CALL CHAR(COUNT+3,"FF3C1
81818183CFF")
650 CALL CHAR(COUNT+4,"00C3E
7E7E7E7C3")
660 CALL CHAR(COUNT+5,"FFFEF
CF8F0E0C080")
670 CALL CHAR(COUNT+6,"00010
3070F1F3F7F")
680 CALL CHAR(COUNT+7,"183C7
EFFFF7E3C18")
690 NEXT COUNT
700 FOR COUNT=1 TO 100
710 CALL HCHAR(RAN(24),RAN(3
1),RAN(129)+31)
720 NEXT COUNT
730 X=110
740 K=2^(1/50)
750 FOR COUNT=1 TO 100
760 F(COUNT)=X*K^COUNT
770 NEXT COUNT
780 FOR COUNT=1 TO 100
790 CALL SOUND(100,F(RAN(100
)),1)
800 CALL COLOR(RAN(16),RAN(1
6),RAN(16))
810 IF RAN(10)>3 THEN 830
820 CALL SCREEN(RAN(16))
830 NEXT COUNT
840 FOR Z=1 TO 5
850 FOR COUNT=3 TO 30
860 CALL VCHAR(8,COUNT,RAN(1
29)+31,2)
870 CALL VCHAR(5,COUNT,RAN(1
29)+31,2)
880 CALL SOUND(-100,F(RAN(10
0)),1)
890 NEXT COUNT
900 FOR COUNT=30 TO 3 STEP -1
910 CALL COLOR(RAN(16),RAN(1
6),RAN(16))
920 CALL VCHAR(12,COUNT,RAN(
127)+32,2)
930 CALL VCHAR(15,COUNT,RAN(
127)+32,2)
940 CALL SOUND(-110,F(RAN(10
0)),1)
950 NEXT COUNT
960 NEXT Z
970 FOR Z=1 TO 26
980 FOR X=1 TO 28
990 M$=CHR$(RAN(127)+32)&M$
1000 NEXT X
1010 PRINT M$
1020 M$=""
1030 CALL SOUND(-400,F(RAN(1
00)),1)
1040 NEXT Z
1050 FOR X=1 TO 100
1060 CALL COLOR(RAN(16),RAN(
16),RAN(16))
1070 NEXT X
1080 M$="1234567809ABCDEF"
1090 X=0
1100 FOR Z=1 TO 8
1110 FOR CT2=1 TO 16
1120 P$=P$&SEG$(M$,RAN(1
6),1)
1130 CALL SOUND(-100,RAN(200
)+110,RAN(10))
1140 NEXT CT2
1150 FOR CT=1 TO 8
1160 CALL CHAR(8*CT+24+X,P$)
1170 NEXT CT
1180 X=X+1
1190 P$=""
1200 NEXT Z
1220 CALL SCREEN(RAN(16))
1230 FOR X=1 TO 100
1240 CALL HCHAR(RAN(24),RAN(
32),RAN(127)+32)
1250 CALL SOUND(-50,F(RAN(10
0)),RAN(20))
1260 CALL COLOR(RAN(16),RAN(
16),RAN(16))
1270 NEXT X
1280 CALL SCREEN(RAN(16))
1290 FOR X=1 TO 100
1300 CALL COLOR(RAN(16),RAN(
16),RAN(16))
1310 CALL SOUND(-20,110+20*R
AN(40),3)
1320 NEXT X
1330 CALL SCREEN(RAN(16))
1340 M$=""
1350 FOR Z=1 TO 26
1360 FOR CT=1 TO 28
1370 M$=CHR$(RAN(127)+32)&M$
1380 NEXT CT
1390 PRINT M$
1400 M$=""
1410 CALL SCREEN(RAN(16))
1420 CALL COLOR(RAN(16),RAN(
16),RAN(16))
1430 NEXT Z
1440 FOR CT2=1 TO 100
1450 CALL COLOR(RAN(16),RAN(
16),RAN(16))
1460 NEXT CT2
1470 CALL SCREEN(RAN(16))
1480 STOP
The use of brackets after RAN in line 160:
DEF RAN(X)=...
does NOT indicate that RAN is a variable array. They inform
the computer that when the newly created FUNCtion RAN is used
in a program, a variable will be passed to the definition by means
of the brackets.
e.g. 460 R=RAN(24)
The computer replaces RAN(24) with the defined statement
using the value 24 in place of X.
The use of brackets does not always imply an array!
Explanation of the Colour Demonstation program:
This program has been provided to give a practical
demonstration of some of the features of TI BASIC described
earlier.
The program has been written in small blocks, and each block
will be described separately.
The first section, lines 100 to 420 form the start of the
program. The first block is intended to display the colours
available, and by making them cross over each other, show the
relative contrasts.
Because some random patterns are created later, RANDOMIZE has
been used to provide different patterns each time the program is
run. The array F contains frequency values for use with CALL
SOUND later on. As it will contain 100 values, DIM is used to
instruct the computer to allocate memory to hold the values.
The DEF function is used to create a new random function,
which will provide integer (eg no fraction) numbers from 1 to
the figure used with the new function in the program: watch out
for the new function RAN(X) in the program.
M$ has been set to "0" (a string with a zero in it) for use
in defining all the characters as blanks (eg spaces) in the
following loop in lines 180 to 200. The string "0" could have
been placed in the definition function in line 190, instead of
the string variable.
NB: Although the TI99/4A distinguishes the number 0 from the
letter O on screen by squaring the O, in listings a slashed 0
usually represents the number.
Each group of eight characters is in a separate character
set, and each set may be a different colour. Lines 210 to 240
change the colour of each set: TI Basic has 16 sets, and 16
colours. The foreground colour of every set has been set to
WHITE( code 16).
Line 230 places a number of vertical stripes on the screen,
each a different colour. As the screen is 32 columns wide, the
stripes have been set to 2 columns wide each, and therefore each
CALL VCHAR uses 48 characters (2x the 24 rows).
So that we know which colours are which, they are labelled by
the routine in lines 250 to 340.
Lines 350-360 give a small delay.
It is not possible to provide 16 horizontal bands with a space
between each, as the screen only has 24 rows, but lines 370 to
390 cross the screen with as many rows as can fit.
In lines 430 to 500, random characters are placed on a blank
screen, and in lines 510 to 590, random stripes and columns are
placed on screen.
Lines 600 to 690 redefine the characters for the purposes of
the following sections of the program. Remember that the strings
used with CALL CHAR can only contain the numbers 0 to 9 and the
letters A to F.
700 to 720 again place random characters on screen.
Lines 730 to 770 fill the F array with values to be used with
subsequent CALL SOUNDs. Line 730 sets the lowest possible
frequency, and line 740 sets the basis for the tones: the
formula used creates what is known as 'microtonal' music, with
very little difference between adjacent tones.
Then, accompanied with random tones, the character colours
are varied at random in lines 780 to 830.
Lines 840 to 960 provide sound, colour changes, and small
bars of random characters.
Lines 970 to 1040 use the PRINT routine to place random
characters on screen, and the colour is varied in 1050 to 1070.
In lines 1080 to 1200, characters are given a random
definition and in 1220 to 1260, the colours are varied, random
chracters placed and tones generated.
1270 to 1310 again varies the colours, and uses a different
random tone generation method (line 1300).
The remainder of the program again uses PRINT to provide a
random display and the colours are varied.
Note in particular the usefulness of the DEF statement in this
program.
There are many 'loops', and some loops contain other loops :
see for example lines 840 to 960. These loops are 'nested', with
the COUNT loop inside the Z loop.
An ARRAY is used to store frequencies.
The loop counters (eg CHARR, SET and so on) also function as
numeric variables in the loops. Their value increases by one for
each cycle of the loop until the maximum value (set by the TO X
in FOR TO NEXT ) has been reached.
In line 1120, SEG$ is used with the string variable M$ (set in
1080) to create a random definition of a character. A letter is
chosen at random from the string variable M$, and used to create
the definition in the variable P$.
Note that P$ is reset to a 'nul' (empty) string after each
character has been defined. Then it is reused with different
letters to form a new definition.