Building a secret message encoder

Encoding the Message

The script in Figure 4 is the core piece of this project because it actually takes the user-supplied message and encodes it into something new. In the current project, the "something new" is Morse code, but in your project, it can be anything you want.

Figure 4: This script shows how the encoder parses the phrase and turns it into an encoded message.

By reviewing Figure 4, you see that the script introduces two more lists named characters and morse code. The characters list includes a list of the characters I want to find by scanning, which is A-Z and 0-1. The morse code list includes the encoded values for the items in the characters list. Keeping these values in sync is crucial for the success of the encoder.

An easy way to get these values into a Scratch list is to create two text files: one containing the characters and one containing the corresponding Morse code. Then, you can use the import feature of Scratch lists by right-clicking on the list's stage monitor and selecting import. This will allow you to browse for your file and import the values into Scratch. The two-list approach, as I'm describing in Scratch, mimics the key-value pair mapping that can often be achieved with a dictionary or associative array in other programming languages.

An easy way to check for errors is to compare the length of each list. If you use letters and numbers as listed in Figure 2, you will have 36 items in each list.

Although I do not show the code in any of the figures, my project includes two error checks in the initialization script that checks to make sure the characters and morse code lists are unique and are the same length. A third possible check could be incorporated to check a value to make sure it matches some expected values. For example, the fifth position of the character list should be the letter E and the fifth position of the morse code list should be a single dot.

Now, I'll go back to the logic of the script. The main repeat () block is looping through each item in the phrase list and assigning it to the lookup variable. Creating the new variable to store the value I'm looking up is not necessary, but it simplifies future blocks and helps reinforce what's happening in the script.

The first evaluation checks to see whether the value assigned to the lookup variable is equal to a space; if so, I use the join () () block to append seven spaces to the encoded phrase variable. I'm using seven spaces because the Morse code unit length between words is seven, which will be a factor on playback.

If the lookup value is not a space, then the script tries to match a value in the characters list. If a match is made, then the next step is to use the item number assigned to char# to retrieve and join the value from the corresponding item number from the morse code list. Then, the script appends three spaces to follow the Morse code unit length of three units between letters.

After that, the next item in the phrase list is looked up. In the current script, anything that is not a letter or a number is quietly ignored. Of course, you can identify other characters to encode by adding their values to the characters and morse code lists.

After all the phrase values are looked up and encoded, it's time to play back the new message.

Playing Back the Message

To play back the message, I'll use a piezo buzzer to generate a tone. The buzzer is attached to pin 11 of the Rasp Pi's GPIO with a 470ohm resistor. Remember, the output pins are 3.3V, so the voltage range of your buzzer must be wide enough to work with the Pi's output pins. Pins 0 and 2 are 5V power pins, but that would keep the buzzer on constantly, which is not what you want.

If you don't want to wire up a circuit or use ScratchGPIO, then the script in Figure 5 can be modified as needed. My first version of this project used the set (ghost) to () block to hide and show a sprite in response to the pattern in the encoded message.

Figure 5: The playback script turns the encoded message into audible tones.

As the script in Figure 5 loops through the individual characters in the encoded message variable, it is checking for three items: a dot (.), a dash (-), or a space. The critical pieces are the speed and pin11 variables.

The speed variable is set in the initialization (Figure 3) script and can be adjusted as appropriate. When I was flashing a sprite on the screen, I slowed the message playback down (higher speed value). However, when the message is output to a tone, I can detect the pattern at a much faster speed. Additionally, because this is your Scratch project, you should feel free to take any timing rules as a suggestion.

When the character being evaluated is a period (dot), then I set the pin11 variable to 1, which is on. The wait (speed) block specifies the buzzer duration before being turned off by setting pin11 to 0, or off. This creates the duration of the tone.

If the character is a dash (-), then I use the same code with one adjustment: I leave the buzzer on for three times the speed value, because dashes are three times the length of dots.

If the character is a space, then I ensure the buzzer stays off. And, because the spaces are added directly to the encoded variable, the script doesn't provide any speed multiplier to change the duration that the tone is off.

If you recall my quick reaction project [2], then the pin11 variable likely seems familiar. ScratchGPIO allows Scratch to communicate with the GPIO using either broadcast messages or variables. In that previous article, I used broadcasts, but this time, I used variables. To work with a variable, you need to create a new global (for all sprites) variable with the pin name (e.g., pin11). Note that there are no spaces. Setting the variable value to 0 equals off and 1 equals on. You can see that syntax in the script in Figure 5. For comparison, if I were to use a broadcast message, the message would be pin1lon.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content