DiDah.bas program

This program is a bit exotic for a beginner, but it is a great learning tool for getting aquainted with a few handy BASIC statements that will certainly lend themselves to other programs. I will step through each section with a quickie explaination. More detailed information can be found in the GWBasic Manual that you undoubtedly have downloaded by now. The Useful Links and Adds link in this tutorial's home page shows a few of the many sources of a good Users Manual for GWBasic.



The prog remark is handy in the first file data if another program is created to search out the program names.
The remaining remark lines are for the programmers greeting and documentation.
10 '           prog    DiDah.bas
20 '
30 '
40 '
50 '
60 '
70 '
80 '
90 '

Vector around the index and data information to the actual beginning of program execution.
The LIST statements are handy to list out any one of the sub modules below.
These line numbers will automatically be updated with any RENUM command while editing.
In the edit mode, type the line number for the module and RETURN. That code only will be displayed.

100 GOTO 340 ' Index of modules
110 '
120 LIST 650-670 ' input from keyboard  ( 1 line)
130 LIST 700-780 ' input from disk      (entire file)
140 LIST 800-840 ' output to monitor    ( 1 line)
150 LIST 900-960 ' output to speaker    ( 1 line)
160 LIST 1000-1160 ' sound the character in n$
170 '
180 '
190 '

Data used in the DIG$ and ALF$ arrays to be loaded later. DIG$ is for the numbers and ALF$ is for the Alphas.
This is a handy way to define the data needed to drive the SOUND sub routine.
200       NUMBERS
210 DATA "-----", ".----", "..---", "...--", "....-"
220 DATA ".....", "-....", "--...", "---..", "----."
230 '
240 '     alphas
250 DATA ".-"   , "-..." , "-.-." , "-.."  , "."    , "..-."
260 DATA "--.." , "...." , ".."   , ".---" , "-.-"  , ".-.."
270 DATA "--"   , "-."   , "---"  , ".--." , "--.-" , ".-."
280 DATA "..."  , "-"    , "..-"  , "...-" , ".--"  , "-..-"
290 DATA "-.--" , "--.."
300 '
310 '
320 '
330 '
340 '    program begins here
350 '

Set the monitor environment and define the sub routines that respond to function keys F1 and F2
360 KEY OFF: COLOR 8,7: CLS
370 ON KEY(1) GOSUB 620: KEY (1) ON
380 ON KEY(2) GOSUB 630: KEY (2) ON

The frequency of the code tone is defined in variable F.
The Dash, Dot, Nul, Inter character and word gap durations are defined as a function of the selected frequency.
It would make it handy if you wanted to program a function key to change the tone frequency during operation.
390 F= 523.25          ' frequency and duration
400 DASH=  4            ' dash duration
410 DOT=   DASH/4       ' dot  duration
420 NUL=   DASH/8       ' null twix beeps
430 ICG=   DASH * .75   ' inter character gap
440 IWG=   DASH * 1.5   ' inter word gap (space)
450 FILE$="text.txt"    ' text file name

Load up the Number and Alpha arrays with the data.
460 DIM DIG$(10): RESTORE 210: FOR N= 1 TO 10: READ DIG$(N): NEXT
470 DIM ALF$(26): RESTORE 250: FOR N= 1 TO 26: READ ALF$(N): NEXT
480 '
490 '

Simple program banner text.
500 PRINT "Type the code you wish to hear"
510 PRINT "use space to seperate the words, as with text"
520 PRINT "Function key 1 (Return) to clear the screen"
530 PRINT "Function key 2 (Return) terminates the program"
540 '

This is the program... Read the keyboard and output to the sound module.
Then keep doing it untile the program is terminated with the F2 function key.
550 GOSUB 650    ' read the keyboard
560 GOSUB 900    ' ouput to speaker
570 GOTO 550
580 END
590 '
600 ' __________________sub routines_______________
610 '

Function key sub routines. They are not processor interups, they are key captures.
They operate while the program is running, not when the interpreter is idle during editing.
620 CLS:RETURN  ' Key (1) sub routine
630 END:RETURN  ' Key (2) sub routine
640 '

Thisis almost a joke for a sub routine ;-)
Type in a line of text and it is loaded into C$ for processing later. It could very easily be
relocated where this sub routine is called from. However, it would be handy as a sub routine
if you wanted to make it more elaborate later and keep the original program loop clean.
650 ' input from keyboard to C$ (one line)
660 LINE INPUT"";C$
670   RETURN
680 '
690 '

This module isn't used in this program, but will be used in another program very similar to this one.
700 ' input from disk (entire file)
710 OPEN FILE$ FOR INPUT AS #1
720     LINE INPUT#1, C$
730   IF EOF(1) THEN 770
740       GOSUB 800
750        PRINT
760         GOTO 720
770    CLOSE #1
780     RETURN
790 '

This also is to be used in another program. It will echo whatever is read from disk to the monitor.
It can also be sent to the SOUND sub routine to play the string in Morse Code.
800 ' output to monitor (one line)
810 FOR N= 1 TO LEN (C$)
820  PRINT MID$(C$,N,1);
830   NEXT N
840    RETURN
850 '
860 '
870 '
880 '
890 '

This sub routine sends a single line of code to the SOUND sub routine, one character at a time.
Each character is terminated by sounding the Inter Character time duration silently.
When the entire line is completed, the program execution returns to the main program.
900 '             output C$ to speaker (one line)
910  FOR N= 1 TO LEN(C$)
920   N$= MID$(C$,N,1)
930    GOSUB 1000 ' sound the character in N$
940     SOUND 32767,ICG
950      NEXT N
960 RETURN
970 '
980 '
990 '



This is the speaker driver, using the SOUND instruction.
Alpha$ is initially cleared with a "". Then it is loaded with the dots or dashes that are to be sounded.
If a space is detected in N$ then the SOUND statement will output the silent tone for the duration of IWG (inter word gap)

If it is a period, then .-.-.- will be loaded for output. The same is true for a comma, interogative, backslash and the at sign.
If it is a number between "0" and "9" then the appropriate data will be loaded from the DIG$ array.
If it is a character between "A" and "Z", or between "a" and "z" then the appropriate code will be taken from the ALF$
The position in the array is determined by subtracting the bias from ascii decimal equivelant for the character detected...
for example the ASC(n) for "A" minus 64 is 1, the first element of the DIG$ array, which is a ._
The ASC(n) for "B" less 64 is two, the second element of the DIG$ array, which is _... and so on for all the Morse Code characters.

Finally, with ALPHA$ loaded with the appropriate symbols, The FOR NEXT loop sounds each of these characters.
If its a dot then F is sounded for the DOT duration. If its a dash the F is sounded for the DASU duration.
in either case, the NUL period is sounded to seperate the dots and dashes be sounding the silent frequency of 32767.

Finally, the program returns to the main program to pick up another character string from the operator.

1000 '         output N$ to speaker ( 1 character)
1010 ALPHA$= ""

1020 IF N$=" " THEN SOUND 32767,IWG
1030 IF N$="." THEN ALPHA$= ".-.-.-."
1040 IF N$="," THEN ALPHA$= "--..--"
1050 IF N$="?" THEN ALPHA$= "..--.."
1060 IF N$="/" THEN ALPHA$= "-..-."
1070 IF N$="@" THEN ALPHA$= ".--.-."
1080 IF N$=>"0" AND N$<="9" THEN ALPHA$= DIG$(ASC(N$)-47)
1090 IF N$=>"A" AND N$<="Z" THEN ALPHA$= ALF$(ASC(N$)-64)
1100 IF N$=>"a" AND N$<="z" THEN ALPHA$= ALF$(ASC(N$)-96)

1110    FOR S= 1 TO LEN (ALPHA$)
1120       S$= MID$(ALPHA$,S,1)
1130 IF S$= "." THEN SOUND F,DOT:  SOUND 32767,NUL
1140 IF S$= "-" THEN SOUND F,DASH: SOUND 32767,NUL
1150  NEXT S
1160 RETURN
1170 '
1180 '
1190 '

The END statement keeps me from accidently executing the next line of code.
The SAVE instruction backs up my work periodically,
in case the interpreter freezes or some similar catastrophy occurs, like a typo on my part ;-)
1200 END     ' save file to disk
1210 SAVE "didah",A