Morse Code with Sound and Light

Step 1 - Build the Project

If you understand the earlier project that used Morse code and LEDs or the project that used Morse code and a speaker, you’ll see that this project is similar, but you are combining the digitalWrite commands for the LED with the tone commands for the speaker.

Step 2 - Upload the Code

/* * Morse Code - LED blinks text messages as Morse code dots and dashes */ // create constant array of the messages you want Morse'd out // Change the "SOS" to any message you like... const char message[ ] = "SOS"; // In ASCII code, 'space' is 32, '0'-'9' is 48-57, and 'A'-'Z' is 65-90 const String morse[] = { ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", ".-", "-...", "-.-.", "-..", ".","..-.","--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; const byte led = 13; const byte speaker = A5; // LED const int dotLen = 200; //delay in milliseconds const int dashLen = 3*dotLen; // 3X longer than the dot const int wordLen = 7*dotLen; // space between words. void setup() { pinMode(led, OUTPUT); pinMode(speaker, OUTPUT); } // blink out one morse code character. void signalMorse(String c) { int i = 0; while (c[i]) { // for each dot or dash in the letter... digitalWrite(led,HIGH); // turn the LED on tone(speaker, 450); // speaker plays a tone of 450 hertz if (c[i] == '.') { delay(dotLen); // if dot, short delay } else if (c[i] == '-') { delay(dashLen); // if dash, longer delay } digitalWrite(led,LOW); //LED off noTone(speaker); // speaker off delay(dotLen); i++; //bring in the next dot or dash from the morse letter!]' }; // We already delayed one dot length in the loop. A Word break = dashLen.. delay(dashLen-dotLen); } void loop() { int i = 0; //counter variable to move through the array char C; //holds each letter as it gets converted to blinks while (message[i]) { // fails when reaches the end of the string ("0") C = toupper(message[i]); if (C == ' ') { delay(wordLen); // space between words } else if (C>47 && C<58) { // 0-9. ASCII 48-57 signalMorse(morse[C-48]); } else if (C>64 && C<91) { // A-Z, ASCII 65-90, 10-34 in morse[] signalMorse(morse[C-55]); } else { delay(wordLen); } i++; // move to the next character in your message } delay(2 * wordLen); } /* Try to put the void loop() code inside an 'if' statement so that the * loop (and your message) only run if you press a button. Don't forget * the setup()!*/ // (c) 2017 Let's Start Coding. License:

Step 3 - Read the Walkthrough

To create Morse code with sound and light, you first need to convert ASCII code to Morse code, then Morse code to tone commands and digitalWrite commands.

First, we create a list of values called an array. The message array holds the characters you’d like to convert to Morse code. The rest of the code can access these letters with a command like: “Go get the 10th thing in the array” and the code will fetch the 10th item in the array.

Second, we create another array. This array- named morse- holds the dot/dash code for each number and letter of the alphabet.

Next you’ll create your variables for your speaker pin, LED pin, and how long dots, dashes, and spaces should take.

The next chunk of code is a function. It is a group of code that always runs together. These are a little bit like a variable, in that you create it, name it, and give it a value (a series of tasks to do). This function is designed to take in dots and dashes and convert them to tone and digitalWrite commands. It checks the Morse character and uses your dotLen and dashLen variables to determine how long to blast the tone from the speaker. Because both the tone and digitalWrite commands use the model “on, wait, off”, placing the commands right next to each other will work perfectly.

In this code, the loop() section is taking in the text characters from the message array, making them uppercase, and converting the ASCII number to the Morse number. Then, it passes the morse array number to signalMorse and says “Here, take this number and do your job with it”. The i++ at the bottom of the loop then moves the loop on to the next character in the array.

The signalMorse number receives the morse array number, which contains dashes and dots. Its job is to unpack the morse array into dashes and dots and then turn on the tone and LED for the duration of those dashes and dots. By the time it is done beeping and flashing out the character, signalMorse is ready to receive another morse array number from the loop!