{"id":803,"date":"2024-10-07T19:24:39","date_gmt":"2024-10-07T23:24:39","guid":{"rendered":"https:\/\/cecilomar.com\/blog\/?p=803"},"modified":"2024-10-07T19:36:14","modified_gmt":"2024-10-07T23:36:14","slug":"morse-code-gcode","status":"publish","type":"post","link":"https:\/\/cecilomar.com\/blog\/morse-code-gcode\/","title":{"rendered":"Morse Code G-Code"},"content":{"rendered":"\n<p>This is just one of my nerding-outs. I sometimes like to make my 3D printers talk to me in Morse code, even though I don&#8217;t understand it myself. Why go through the effort of making my own GCODE if I can make a robot do it for me? This form will convert your text to Morse code, emulate the sound, and display the G-Code that you would need to use to make your 3D printer beep them to you.<\/p>\n\n\n\n<p>Maybe it&#8217;s valuable for someone out there.<\/p>\n\n\n\n<h6 class=\"wp-block-heading has-text-align-center\">Enter text to convert to Morse code<\/h6>\n\n\n\n<center>\n<input type=\"text\" id=\"textInput\" placeholder=\"Enter your text here\" style=\"width: 200;\">\n<button onclick=\"convertToMorse()\">Convert and Beep<\/button>\n<\/center>\n\n<div class=\"output\" id=\"morseOutput\" style=\"margin-top: 20px; white-space: pre-wrap;\"><\/div>\n<div class=\"output\" id=\"gcodeOutput\" style=\"margin-top: 20px; white-space: pre-wrap;\"><\/div>\n\n<script>\n    \/\/ Morse code dictionary\n    const MORSE_CODE_DICT = {\n        'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..',\n        'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',\n        'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..',\n        '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..',\n        '9': '----.', '0': '-----', ' ': '\/'\n    };\n\n    \/\/ Convert text to Morse code and beep using Web Audio API\n    async function convertToMorse() {\n        const textInput = document.getElementById('textInput').value.toUpperCase();\n        const morseOutputDiv = document.getElementById('morseOutput');\n        const gcodeOutputDiv = document.getElementById('gcodeOutput');\n        let morseCodeOutput = 'Morse Code:\\n';\n        let gcodeOutput = 'G-Code:\\n';\n\n        for (let char of textInput) {\n            if (MORSE_CODE_DICT[char]) {\n                const morse = MORSE_CODE_DICT[char];\n                morseCodeOutput += morse + ' ';\n                gcodeOutput += generateGcodeForMorse(morse);\n                await beepMorse(morse);\n            }\n            morseCodeOutput += '   '; \/\/ Space between characters\n            gcodeOutput += 'G4 P600 ; Pause between characters\\n';\n        }\n\n        morseOutputDiv.innerText = morseCodeOutput;\n        gcodeOutputDiv.innerText = gcodeOutput;\n    }\n\n    \/\/ Function to generate G-code commands from Morse code\n    function generateGcodeForMorse(morseCode) {\n        let gcode = '';\n        for (let symbol of morseCode) {\n            if (symbol === '.') {\n                gcode += 'M300 S440 P200 ; Dot (short beep)\\n';\n            } else if (symbol === '-') {\n                gcode += 'M300 S440 P600 ; Dash (long beep)\\n';\n            }\n            gcode += 'G4 P200 ; Short pause between dots\/dashes\\n';\n        }\n        return gcode;\n    }\n\n    \/\/ Function to simulate the beeps using Web Audio API\n    function beepMorse(morseCode) {\n        return new Promise((resolve) => {\n            const audioCtx = new (window.AudioContext || window.webkitAudioContext)();\n            let currentTime = audioCtx.currentTime;\n\n            for (let symbol of morseCode) {\n                const oscillator = audioCtx.createOscillator();\n                oscillator.type = 'sine';\n                oscillator.frequency.setValueAtTime(880, currentTime); \/\/ Tone of 440 Hz\n                const gainNode = audioCtx.createGain();\n\n                gainNode.gain.setValueAtTime(0.1, currentTime);\n                gainNode.gain.exponentialRampToValueAtTime(0.00001, currentTime + (symbol === '.' ? 0.2 : 0.6));\n\n                oscillator.connect(gainNode).connect(audioCtx.destination);\n                oscillator.start(currentTime);\n                oscillator.stop(currentTime + (symbol === '.' ? 0.2 : 0.6));\n                currentTime += (symbol === '.' ? 0.4 : 0.8); \/\/ Pause after each dot or dash\n            }\n\n            \/\/ Pause after each character before resolving the promise\n            setTimeout(resolve, morseCode.length * 400);\n        });\n    }\n<\/script>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is just one of my nerding-outs. I sometimes like to make my 3D printers talk to me in Morse code, even though I don&#8217;t understand it myself. Why go through the effort of making my own GCODE if I can make a robot do it for me? This form will convert your text to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[62,63,64],"class_list":["post-803","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-3d-printing","tag-gcode","tag-morse"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/posts\/803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/comments?post=803"}],"version-history":[{"count":4,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/posts\/803\/revisions"}],"predecessor-version":[{"id":805,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/posts\/803\/revisions\/805"}],"wp:attachment":[{"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/media?parent=803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/categories?post=803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cecilomar.com\/blog\/wp-json\/wp\/v2\/tags?post=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}