Morse Code

Introduction

In the nineteenth and early twentieth centuries, before telephones and the internet, the quickest way to send messages was by telegraph. This was a network of wires suspended on telegraph poles (on land) and in thick rubber cables (under the sea). Messages could be sent as electrical pulses at very high speed.

Samuel Morse invented a code that represents letters as sets of short and long electrical pulses, known as 'dots' (.) and 'dashes' (-).

There is a short pause between letters and a longer pause between each word. Telegraph operators could send and receive messages very quickly by listening to these electical pulses as beeps and tapping their reply with a button.

We are going to program our Microbit to work like a telegram using Morse code. Then we can be telegraph operators and swap messages with other Microbits.

In our code we will use text to store dots and dashes, and the radio to send the code to another Microbit.

Dots and Dashes

We are going to enter a dot by pressing the A button and a dash by pressing B. Each letter can have between one and five dots and dashes, we will store these in a text variable called letterToSend.

Start a new, empty project at makecode.microbit.org and rename your project "Morse code"

Make a new variable called LetterToSend

In the Start loop add a set block from the Variables drawer and change it to LetterToSend.

Add and empty text block from the Advanced, Text drawer.

Now we have a variable to store our Morse code and we have set it to be empty, we can use it to store button presses by the telegraph operator. Each time a button is pressed we will use the Text block join to add the code to our LetterToSend variable.

Add an on button A pressed block from Input.

Add a set block from Variables and change it to LetterToSend.

Add a join block from Text

In the top of the join block add a copy of LetterToSend from Variables.

Set the empty text block at the bottom of the join block to be a dot (.).

Add a show string block from Basic

Set it to LetterToSend to show all the dots and dashes we have entered so far.

Duplicate this code, changing the button press to B and the dot (.) to a dash (-).

Sending a letter

We can enter letters in Morse code using the A and B buttons, to send them we will use A+B using radio.

Add a radio set group block from Radio to the on start block.

Set this to your group number

Add an on button A+B pressed block from Input.

Add a radio send string block from Radio, set it to LetterToSend

Reset the LetterToSend variable with an empty text block ready for the next letter.

Receiving a letter

When we receive a letter we will show the Morse code on the screen.

Add an on radio received receivedString block from Radio.

Set a show string block from Basic to show receivedString.

We might be in the middle of trying to send a letter ourselves when we receive one. This can get confusing, so if we receive a letter while we are trying to send one we'll delete the code we have entered so far in LetterToSend.

Set LetterToSend to an empty text block from Advanced, Text.

Sending telegraph messages

We now have a working telegraph system that can send and receive Morse code.

Try sending a message to your partner and get them to send one back.

Can you think of a way to tell each other when it's their turn to send a message?

This works as a telegraph, in the advanced section of this project we make a version that translates received Morse code into letters using an array.

A complete version of the code for the basic telegraph can be found here

Projects designed and written by &

CC BY-SA 4.0