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.
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
Start a new, empty project at makecode.microbit.org and rename your project "Morse code"
Make a new variable called
Start loop add a
set block from the Variables drawer and change it to
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
on button A pressed block from Input.
set block from Variables and change it to
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 (.).
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 (-).
We can enter letters in Morse code using the A and B buttons, to send them we will use A+B using
radio set group block from Radio to the
on start block.
Set this to your group number
on button A+B pressed block from Input.
radio send string block from Radio, set it to
LetterToSend variable with an empty text block ready for the next letter.
When we receive a letter we will show the Morse code on the screen.
on radio received receivedString block from Radio.
show string block from Basic to show
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 to an empty text block from Advanced, Text.
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
A complete version of the code for the basic telegraph can be found here