In our basic Morse code project we had to translate the Morse code we received ourselves. Using arrays
to store lists of all the Morse codes we can get the Microbit to translate the code for us. We can also use these lists to check that the Morse code we enter to send is correct before we send a letter.
Setting up a new array is similar to setting up a normal variable. We make a new variable and give it a name in the Variables draw.
Instead of setting it to one value as we would with a normal variable we use a create array with
block from the Advanced, Array drawer.
Now we can set the variable to hold a list of values. These can either be numbers, or text but not a mix of both.
Open a new project at makecode.microbit.org
Make a new variable called myArray
In the start loop add a set item to
block and set it to myArray
.
Add a create array with " "
block from Advanced, Arrays.
Use the blue cog icon and drag three more value
blocks to add more places on your array list.
Fill in the empty text blocks with a list of objects.
We have created an array called myArray
holding a list of words but how do we use this list in code?
The list is stored in order, the first item is stored at position 0, the second at 1, the third at 2..
We can see what is at any positon on the list by using a get value at
block from Advanced, Arrays.
Add an on Button A pressed
block from Input
Put a show string
block from Basic inside the on Button A pressed
block.
Add a get value at
block from Advanced, Arrays and change the variable to myArray
Try this code in the simulator, what happens when you press button A?
Change the number in your get value at
block to see other items on the list.
Now we know about arrays we can use them to code our advanced telegraph machine.
We will create arrays to hold all the letters of the alphabet alphabet
and all the Morse code values morse
.
By storing the letter in alphabet
at the same number in the list as it's morse code value in morse
we can quickly convert between the two.
Open the project at makecode.microbit.org/_5ew60yasTi6b and click edit.
This has all the code from the basic Morse code project and two array
lists for the morse codes morse
and all the letters of the alphabet alphabet
. They have already been filled in for you.
First we will add a check before we send a letter to make sure it is in the morse
array.
If it is we will show the letter from alphabet
on the display and send the morse
value by radio
If LetterToSend
isn't in our morse
list we will show the message "error" on the screen and reset LetterToSend
Make a new variable calledarrayPosition
- we will use this when we search morse
for our letter.
In the on button A+B pressed
add a set item to
block and set it to arrayPosition
From Advanced, Arrays, more get a list find index of
block. Change list
to morse
and plug in a copy of LetterToSend
from Variables.
This block searches the morse
array for the text held in LetterToSend
. If it finds the text in the array it will set arrayPosition
to the place in the array list where the text is stored.
Next add an if then else
block from Logic to check if the list find index of
block found our coded letter in the morse
array.
Get a get value at
block from Advanced, Arrays and set it to search alphabet
for the value at the position held in arrayPosition
. Plug this into the if
block
If the letter we are trying to send is in the array the get value at
block will be true and then
will be used, if it isn't it will be false and the else
will be run instead.
Add two show string
blocks from Basic to then
and else
.
In the then
duplicate the code from if
For the else
type "Error"
Move the radio send string
block into the then
block.
Your complete block will now look like this.
Download your code to your Microbit and test it. You can still input letters to send using A and B. Now when you send them with A+B it will check if you have entered a letter and show it on the display, or show error if you have made a mistake.
Using the same technique we can convert any letters we receive from morse
to alphabet
.
Copy all the code from on button A+B pressed
and add it to the on radio received string
block.
Delete the radio send string
block
Change LetterToSend
to receivedString
Now when the Microbit receives a letter it will check if it can find the code in the morse
array and show the letter at the same place in the alphabet
array.
We still reset the LetterToSend
variable, in case you were in the middle of sending a letter when you received one
Download the code to your Microbit and test sending messages.
Try typing an incorrect Morse code, what happens?
A complete version of the code for the advanced telegraph can be found here