Morse Code Translator - QBasic Source Code

Back to: Main Programming Page

Here's a program that takes any phrase you type, and plays it on your computer's speaker in Morse code.

 

Source Code:

'              Morse Code
'
'This program will convert any phrase up to 5000 characters into Morse Code.
'You can either use American Morse Code, or International Morse Code.  Once
'you have typed in your phrase, hit Alt-1 (the 1 on the numeric key pad), and
'the computer will give you your phrase in morse code, displaying the letter
'and code as it does it.
'
'Press "F5" to run the program.
'
'
'
'
'
'
'
'
'Programmed by Jeff Lewis
new:
dot$ = "l10cp10"
dash$ = "l5cp10"
longdash$ = "l2cp10"
pause$ = "p10"
DIM phrase$(5000) 'To make the program accept a longer phrase, make the number
ctr = 0           '  after phrase$ bigger.
x = 1

PLAY "O4T255"  'sets the pitch and tempo for the morse code

CLS
PRINT "Do you want International Morse Code or American Morse Code? i/a"
DO
code$ = LCASE$(INKEY$)
LOOP UNTIL code$ = "a" OR code$ = "i"
LOCATE 1, 67
IF code$ = "i" THEN
PRINT "International"
ELSE
PRINT "American"
END IF
LOCATE 2, 1

PRINT "What phrase do you want converted to code? (Press Enter when done.)"

a:
LOCATE 3, x, 1
DO
a$ = INKEY$
LOOP WHILE a$ = ""
phrase$(ctr) = a$
PRINT a$
IF a$ = CHR$(13) THEN
GOTO morse
END IF
IF a$ = CHR$(8) THEN
LOCATE 3, x - 1
PRINT "  "
x = x - 2
ctr = ctr - 2
END IF
x = x + 1
ctr = ctr + 1
GOTO a:

morse:
max = ctr
ctr = 0
x = 1
y = 3

LOCATE 4, 1

FOR ctr = 0 TO max
y = y + 1
IF y = 24 THEN
y = 4
x = x + 20
  IF code$ = "a" THEN
  x = x + 6
  END IF
END IF

LOCATE y, x, 0

PLAY "p5"



IF code$ = "i" THEN

SELECT CASE LCASE$(phrase$(ctr))

CASE "a"
PRINT "-      "; phrase$(ctr)
PLAY dot$ + dash$

CASE "b"
PRINT "-    "; phrase$(ctr)
PLAY dash$ + dot$ + dot$ + dot$

CASE "c"
PRINT "--    "; phrase$(ctr)
PLAY dash$ + dot$ + dash$ + dot$

CASE "d"
PRINT "-     "; phrase$(ctr)
PLAY dash$ + dot$ + dot$

CASE "e"
PRINT "       "; phrase$(ctr)
PLAY dot$

CASE "f"
PRINT "-    "; phrase$(ctr)
PLAY dot$ + dot$ + dash$ + dot$

CASE "g"
PRINT "--     "; phrase$(ctr)
PLAY dash$ + dash$ + dot$

CASE "h"
PRINT "    "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + dot$

CASE "i"
PRINT "      "; phrase$(ctr)
PLAY dot$ + dot$

CASE "j"
PRINT "---    "; phrase$(ctr)
PLAY dot$ + dash$ + dash$ + dash$

CASE "k"
PRINT "--     "; phrase$(ctr)
PLAY dash$ + dot$ + dash$

CASE "l"
PRINT "-    "; phrase$(ctr)
PLAY dot$ + dash$ + dot$ + dot$

CASE "m"
PRINT "--      "; phrase$(ctr)
PLAY dash$ + dash$

CASE "n"
PRINT "-      "; phrase$(ctr)
PLAY dash$ + dot$

CASE "o"
PRINT "---     "; phrase$(ctr)
PLAY dash$ + dash$ + dash$

CASE "p"
PRINT "--    "; phrase$(ctr)
PLAY dot$ + dash$ + dash$ + dot$

CASE "q"
PRINT "---    "; phrase$(ctr)
PLAY dash$ + dash$ + dot$ + dash$

CASE "r"
PRINT "-     "; phrase$(ctr)
PLAY dot$ + dash$ + dot$

CASE "s"
PRINT "     "; phrase$(ctr)
PLAY dot$ + dot$ + dot$

CASE "t"
PRINT "-       "; phrase$(ctr)
PLAY dash$

CASE "u"
PRINT "-     "; phrase$(ctr)
PLAY dot$ + dot$ + dash$

CASE "v"
PRINT "-    "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + dash$

CASE "w"
PRINT "-     "; phrase$(ctr)
PLAY dot$ + dash$ + dash$

CASE "x"
PRINT "--    "; phrase$(ctr)
PLAY dash$ + dot$ + dot$ + dash$

CASE "y"
PRINT "---    "; phrase$(ctr)
PLAY dash$ + dot$ + dash$ + dash$

CASE "z"
PRINT "--    "; phrase$(ctr)
PLAY dash$ + dash$ + dot$ + dot$

CASE "1"
PRINT "----   1"
PLAY dot$ + dash$ + dash$ + dash$ + dash$

CASE "2"
PRINT "---   2"
PLAY dot$ + dot$ + dash$ + dash$ + dash$

CASE "3"
PRINT "--   3"
PLAY dot$ + dot$ + dot$ + dash$ + dash$

CASE "4"
PRINT "-   4"
PLAY dot$ + dot$ + dot$ + dot$ + dash$

CASE "5"
PRINT "   5"
PLAY dot$ + dot$ + dot$ + dot$ + dot$

CASE "6"
PRINT "-   6"
PLAY dash$ + dot$ + dot$ + dot$ + dot$

CASE "7"
PRINT "--   7"
PLAY dash$ + dash$ + dot$ + dot$ + dot$

CASE "8"
PRINT "---   8"
PLAY dash$ + dash$ + dash$ + dot$ + dot$

CASE "9"
PRINT "----   9"
PLAY dash$ + dash$ + dash$ + dash$ + dot$

CASE "0"
PRINT "-----   0"
PLAY dash$ + dash$ + dash$ + dashh$ + dash$

CASE " "
PRINT "----"
PLAY dash$ + dot$ + dash$ + dash$ + dot$ + dash$

CASE "."
PRINT "---  ."
PLAY dot$ + dash$ + dot$ + dash$ + dot$ + dash$

CASE ","
PRINT "----  ,"
PLAY dash$ + dash$ + dot$ + dot$ + dash$ + dash$

CASE "?"
PRINT "--  ?"
PLAY dot$ + dot$ + dash$ + dash$ + dot$ + dot$

CASE ":"
PRINT " --  :"
PLAY pause$ + dash$ + dash$ + dot$ + dot$ + dot$

CASE ";"
PRINT "---  ;"
PLAY dash$ + dot$ + dash$ + dot$ + dash$ + dot$

CASE "-"
PRINT "--  -"
PLAY dash$ + dot$ + dot$ + dot$ + dot$ + dash$

CASE "!"
PRINT "---   !"
PLAY dash$ + dash$ + dot$ + dot$ + dash$

CASE "'"
PRINT "----  '"
PLAY dot$ + dash$ + dash$ + dash$ + dash$ + dot$

CASE "/"
PRINT "--   /"
PLAY dash$ + dot$ + dot$ + dash$ + dot$

CASE CHR$(34)
PRINT "--"
LOCATE y, x + 8
PRINT CHR$(34)
PLAY dot$ + dash$ + dot$ + dot$ + dash$ + dot$


END SELECT


ELSE

SELECT CASE LCASE$(phrase$(ctr))

CASE "a"
PRINT "-  "; phrase$(ctr)
PLAY dot$ + dash$

CASE "b"
PRINT "-          "; phrase$(ctr)
PLAY dash$ + dot$ + dot$ + dot$

CASE "c"
PRINT "           "; phrase$(ctr)
PLAY dot$ + dot$ + pause$ + dot$

CASE "d"
PRINT "-           "; phrase$(ctr)
PLAY dash$ + dot$ + dot$

CASE "e"
PRINT "   "; phrase$(ctr)
PLAY dot$

CASE "f"
PRINT "-           "; phrase$(ctr)
PLAY dot$ + dash$ + dot$

CASE "g"
PRINT "--           "; phrase$(ctr)
PLAY dash$ + dash$ + dot$

CASE "h"
PRINT "          "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + dot$

CASE "i"
PRINT "  "; phrase$(ctr)
PLAY dot$ + dot$

CASE "j"
PRINT "--          "; phrase$(ctr)
PLAY dash$ + dot$ + dash$ + dot$

CASE "k"
PRINT "--           "; phrase$(ctr)
PLAY dash$ + dot$ + dash$

CASE "l"
PRINT "           "; phrase$(ctr)
PLAY longdash$

CASE "m"
PRINT "--  "; phrase$(ctr)
PLAY dash$ + dash$

CASE "n"
PRINT "-  "; phrase$(ctr)
PLAY dash$ + dot$

CASE "o"
PRINT "            "; phrase$(ctr)
PLAY dot$ + pause$ + dot$

CASE "p"
PRINT "         "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + dot$ + dot$

CASE "q"
PRINT "-          "; phrase$(ctr)
PLAY dot$ + dot$ + dash$ + dot$

CASE "r"
PRINT "           "; phrase$(ctr)
PLAY dot$ + pause$ + dot$ + dot$

CASE "s"
PRINT "           "; phrase$(ctr)
PLAY dot$ + dot$ + dot$

CASE "t"
PRINT "-   "; phrase$(ctr)
PLAY dash$

CASE "u"
PRINT "-           "; phrase$(ctr)
PLAY dot$ + dot$ + dash$

CASE "v"
PRINT "-          "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + dash$

CASE "w"
PRINT "-           "; phrase$(ctr)
PLAY dot$ + dash$ + dash$

CASE "x"
PRINT "-          "; phrase$(ctr)
PLAY dot$ + dash$ + dot$ + dot$

CASE "y"
PRINT "          "; phrase$(ctr)
PLAY dot$ + dot$ + pause$ + dot$ + dot$

CASE "z"
PRINT "          "; phrase$(ctr)
PLAY dot$ + dot$ + dot$ + pause$ + dot$

CASE "1"
PRINT "--          1"
PLAY dot$ + dash$ + dash$ + dot$

CASE "2"
PRINT "-         2"
PLAY dot$ + dot$ + dash$ + dot$ + dot$

CASE "3"
PRINT "-         3"
PLAY dot$ + dot$ + dot$ + dash$ + dot$

CASE "4"
PRINT "-         4"
PLAY dot$ + dot$ + dot$ + dot$ + dash$

CASE "5"
PRINT "---           5"
PLAY dash$ + dash$ + dash$

CASE "6"
PRINT "        6"
PLAY dot$ + dot$ + dot$ + dot$ + dot$ + dot$

CASE "7"
PRINT "--          7"
PLAY dash$ + dash$ + dot$ + dot$

CASE "8"
PRINT "-         8"
PLAY dash$ + dot$ + dot$ + dot$ + dot$

CASE "9"
PRINT "--          9"
PLAY dash$ + dot$ + dot$ + dash$

CASE "0"
PRINT "           0"
PLAY longdash$

CASE " "
PRINT "----"
PLAY dash$ + dot$ + dash$ + dash$ + dot$ + dash$

CASE "."
PRINT "--        ."
PLAY dot$ + dot$ + dash$ + dash$ + dot$ + dot$

CASE ","
PRINT "--          ,"
PLAY dot$ + dash$ + dot$ + dash$

CASE "?"
PRINT "--         ?"
PLAY dash$ + dot$ + dot$ + dash$ + dot$

CASE ":"
PRINT "--         :"
PLAY dash$ + dot$ + dash$ + pause$ + dot$ + pause$ + dot$

CASE ";"
PRINT "         ;"
PLAY dot$ + dot$ + dot$ + pause$ + dot$ + dot$

CASE "-"
PRINT " -     -"
PLAY dot$ + dot$ + dot$ + dot$ + pause$ + dot$ + dash$ + dot$ + dot$

CASE "!"
PRINT "---          !"
PLAY dash$ + dash$ + dash$ + dot$

CASE "'"
PRINT "- -     '"
PLAY dot$ + dot$ + dash$ + dot$ + pause$ + dot$ + dash$ + dot$ + dot$

CASE "/"
PRINT "- -         /"
PLAY dot$ + dot$ + dash$ + pause$ + dash$

CASE CHR$(34)
PRINT "- -"
LOCATE y, x + 14
PRINT CHR$(34)
PLAY dot$ + dot$ + dash$ + dot$ + pause$ + dash$ + dot$

CASE "("
PRINT " -      ("
PLAY dot$ + dot$ + dot$ + dot$ + dot$ + pause$ + dash$ + dot$

CASE ")"
PRINT "     )"
PLAY dot$ + dot$ + dot$ + dot$ + dot$ + pause$ + dot$ + dot$ + pause$ + dot$ + dot$

END SELECT

END IF

NEXT ctr

PRINT "Do you want to do another? y/n"
DO
y.n$ = INKEY$
LOOP UNTIL y.n$ = "y" OR y.n$ = "n"
IF y.n$ = "y" THEN
GOTO new
END IF