Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts

    help with morse coder/decoder

    I'm writing a morse coder/decoder for a school project, you can find the initial version here. 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!
    Last edited by Kirl; 10-15-2006 at 12:43 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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?

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    38
    Thanked 505 Times in 499 Posts

    Delayed response.

    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!

    PHP Code:
    <html>
    <
    head>
    <
    title>Morris Code Generator</title>

    <
    style type =" text/css">
    div    border1px solid black
                
    width600px;
                
    padding10px; }
            
    .
    morseDictionairy    width500px;    }
    </
    style>

    <
    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=0i<LetterArray.lengthi++) {
        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!');
    }
    </script>
    </head>

    <body>
    <center>

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

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

    <table class ="morseDictionairy" style ="text-align:left;" border="1">
    <script type="text/javascript">
    // document.write(ShowTable());
      InitSetUp();
      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>';
      document.write(tstr);
    </script>
    </table>

    </center>
    </body>
    </html> 
    Let me know if you have problems coding part from MorseCode to English.

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •