In this lesson we will turn our Microbit into a Morse code decoder. We will enter the dots and dashes into the Microbit and it will then tell us what letter it is.
Morse code is a method used in communication to encode text characters as sequences of two different signal lengths, called dots and dashes or dits and dahs. Morse code is named after Samuel Morse, an inventor of the telegraph.
In an emergency, Morse code can be generated by improvised methods such as turning a light on and off, tapping on an object or sounding a horn or whistle, making it one of the simplest and most versatile methods of telecommunication. The most common distress signal is SOS – three dots, three dashes, and three dots.
We've created a starter project that has 2 lists (also called arrays) already added.
The first list is called 'alphabet' and has all the letters on the alphabet in it. The second list is called 'morse codes' and has the dots and dashes for each letter.
Open this starter project by clicking on the following link and then click the 'Edit Code' button.
https://makecode.microbit.org/_bHVCaDbwkR7b
To open a Microbit starter project:
You can then continue with the next steps in the lesson.
Once you've opened the starter project, you'll notice that there are 2 lists created and we've already added some items to the lists.
What is a list?
A list (also called an array) is a tool that can be used to store multiple pieces of information at once. It can also be defined as a variable containing multiple other variables. A list consists of a numbers paired with items. Each item can be retrieved by its paired number.
Alphabet List
We've added all the letters in the alphabet to the "alphabet" list/array.
Morse Code List
We've also added what the Morse code for each letter is to the "morse codes" list/array. We've added these in the order of the alphabet, for example the first item in the "Morse codes" list is for 'a', the second item for 'b', the third for 'c' and so on.
In this project we are going to use the A and B buttons to enter a Morse code message (a series of dots and dashes) for a letter of the alphabet. The Microbit will then decode what letter it is and then display the letter using the LEDs on the Microbit.
We will need a variable to store the Morse code message that we enter, so create a variable called 'message'.
In the Variables toolbox, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
We're going to program the A button to add a dot (the full stop character on your keyboard '.') to the message the Microbit needs to decode.
Add the following code, this will add a dot to the end of the message variable each time you press the A button.
input.onButtonPressed(Button.A, function () { message = "" + message + "." })