View Full Version : help with morse coder/decoder

10-14-2006, 10:49 PM
I'm writing a morse coder/decoder for a school project, you can find the initial version here (http://www.kirl.nl/morseCoder.html). Enter is used to give in the morse signals, this will later be replaced by the USB port of the computer interfacing with a self made circuitboard with a light sensor (for input) and LED + speaker (for output).

It seems to be working well for the most part, but I'm having trouble sending out the morse signal. The idea is that when a button is pressed javascript should read each character in the morse textfield and then send out a signal of either 9 mlseconds (= short signal represented by ".") or a signal of 27 mlseconds (= long signal represented by "-"). with a noticable pause in between to discern between signals.

For now I'm using the bgcolor of the signal box title to simulate the LED and speaker signal, this will also be changed to a USB signal.

I've tried a sorts of diffrent setInterval() setups, but to no avail. Any help on this is greatly apriciated!

10-16-2006, 01:27 AM
Hm, well I finally managed to fix it (not thanks to you ;) ), but because I won't connect the usb circuit untill tuesday I want to try to add some sound as well.

Would anybody here know if it's possible to use machine sounds (from the inbuilt pc speaker) with javascript?

10-16-2006, 06:10 AM
I have not looked at your fix yet, but in your first version you had
a lot of duplicate code. This coding method is OK, but I find it prone to errors.

I re-wrote the code section (without your delays, light, colors, etc)
to something that I think is more efficient and easier to read.

Incorporate if you want into your code.
Didn't take much time, but the response was delayed due to weekend chores!

<title>Morris Code Generator</title>

<style type =" text/css">
div { border: 1px solid black;
width: 600px;
padding: 10px; }

.morseDictionairy { width: 500px; }

<script type="text/javascript">
LetterStr = "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9|+|=";
DotDashStr =
"._|-...|-.-.|-..|.|..-.|--.|....|..|.---|-.-|.-..|--|" // a-m
+"-.|---|.--.|--.-|.-.|...|-|..-|...-|.--|-..-|-.--|--..|" // n-z
+"-----|.----|..---|...--|....-|.....|-....|--...|---..|----.|" // 0-9
+".-.-.|-...-"; // +,= (space not defined at this time)

DotDashArray = new Array ();
LetterArray = new Array ();

function getMorseCode(letter) {
fnd = -1;
for (var i=0; i<LetterArray.length; i++) {
if (letter == LetterArray[i]) { fnd = i; break; }
if (fnd != -1) { return DotDashArray[fnd]; } else { return ''; }
function InitSetUp() {
DotDashArray = (DotDashStr.toUpperCase()).split("|");
LetterArray = (LetterStr.toUpperCase()).split("|");
function ToMorse() {
obj = document.getElementById('English');
var str = obj.value;
str = str.toUpperCase();
// document.getElementById('MorseCode').value = str; // for testing
letters = str.split("");
str = '';
for (items in letters) { str += getMorseCode(letters[items])+' '; }
document.getElementById('MorseCode').value = str;

function FromMorse() {
alert('Not coded yet!');


<textarea id='English' cols=50></textarea>
<br><button onClick="ToMorse()">To Morse Code</button>

<textarea id='MorseCode' cols=50></textarea>
<br><button onClick="FromMorse()">From Morse Code</button>

<table class ="morseDictionairy" style ="text-align:left;" border="1">
<script type="text/javascript">
// document.write(ShowTable());
var tstr = '<tr><td>';
for (var i=0; i<LetterArray.length; i++) {
tstr += LetterArray[i]+' '+DotDashArray[i]+'<br />';
if ((i % 4) == 3) { tstr += '</td><td>'; }
tstr += '</td></tr>';


Let me know if you have problems coding part from MorseCode to English.

10-16-2006, 11:04 AM
Thanks for your effort, it is indeed a lot cleaner and there's even some new syntax in it for me as well. Morse input through Enter/Return isn't there though but that's no problem.

I'll have a good look at it, thanks again! Very usefull.