Morse Code is probably the most famous way of sending "encoded" messages. In this project we program a robot to tell us what the morse code for each letter in the alphabet 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 a robot sprite and 2 lists already added. Open this starter project by clicking on the following link:
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 in Scratch?
A list (also called an array in other programming languages) 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.
Morse Code List
We've also added what the morse code for each letter is to the "morse codes" list. 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.
Let's get coding! We're going to activate the robot by clicking on it, once we do the robot will ask us what letter we want to translate to morse code.
Add the following code to the Retro Robot sprite.
when this sprite clicked
ask [what letter do you want to encode?] and wait
The ask and wait block is a Sensing block. The block will make the sprite using the block say the question and show an input box at the bottom of the screen.
Whatever you type in the box will be saved into the answer block.
Create a custom block called 'encode' and add an input called 'letter' to it. We will use this custom block to translate the letter to it's corresponding morse code.
Once you've created it you will notice there's a encode block in the toolbox and a define [ encode (letter) ] block in the code area.
Add the encode block underneath the ask and wait block and put the answer block inside it.
when this sprite clicked
ask [what letter do you want to encode?] and wait
encode (answer)
define encode (letter)
You can create your own custom blocks and give them their own instructions (code). To create a custom block follow these steps:
A 'Define My Block Name' will appear in the code area. You can add your code underneath this and then use the My Block Name block in the 'My Blocks' toolbox anytime you wish to run that code.