Building a secret message encoder

Lead Image © kudryashka, 123RF.com

Secret Code

Build a secret message encoder with Morse code.

In this issue, I'll show you how to use the Scratch list feature to build a message encoder (or translator) that can be played back using a simple circuit on the GPIO. From a programming skills perspective, it will demonstrate how to manipulate the individual characters of much larger strings and transform them into something new.

I remember creating codes with my friends so that we could write "secret" notes to each other in school. Sometimes, the code used simple substitution (e.g., Z for A, Y for B, etc.), and other times the code was gibberish. In this project, I'm going to show you how to build your own message encoder using Scratch to substitute letters and numbers for Morse code.

Even though the project can ultimately be used to encode messages based on any rules you might want to make up, I'm going to demonstrate it with Morse code. Morse code, although it has many historical uses, including amateur radio, aviation, and of course the emergency signal SOS, is not encountered too often by the public.

In this project, the code will be played back through a piezoelectric buzzer wired up to the Raspberry Pi's GPIO pins, which can be controlled with ScratchGPIO [1]. I gave an introduction to ScratchGPIO in issue 7 of Raspberry Pi Geek [2].

The actual circuit for this project is very simple, as seen in Figure 1, and this project can easily be modified to work with another component, such as an LED, or without any external electronics. However, the programming piece of the project will work extensively with lists (arrays) to create a "dictionary" of values that can be looked up to perform the encoding. To encode the message properly, I'll show you how to parse each message (a string), character by character.

Figure 1: The circuit showing how to connect a buzzer to the Rasp Pi to output Morse code.

Generating Morse Code

One of the great things about Morse code is that you can transmit messages in a variety of ways, including electrical pulses, tones, or flashes of light. Morse is composed of a pattern of dots and dashes to identify each character in the message. The difference between a dot and dash is the duration of the signal. A dash is three times longer than a dot, so if you are sending Morse code with an audible tone, the dot is one-third as long as the dash.

The spacing requirements for sending Morse code are summarized at the top of Figure 2, where unit is the duration of the signal and can typically be measured in milliseconds, as in one unit equals 50ms.

Figure 2: A table showing the international Morse code values for letters and numbers. (Image is public domain from Wikipedia.)

I'll use each of the five unit lengths in the Scratch encoder to distinguish between letters and words to ensure that I generate understandable Morse code. The bottom part of Figure 2 shows the international Morse code values for each character. If you scan the list, you'll notice that the letter E is represented by a single dot. That's because E is the most frequently used letter in the English language, and it's represented by the shortest code. The more frequent the letter usage, the shorter the code.

With that basic understanding, I'll take a look at the scripts.

Initializing the Encoder

Figure 3 shows the initialization script for the project. In addition to setting some initial values, this script will prompt the user to type a message and then store it in a list named phrase. The ask () and wait block prompts the user for input and stores the input in the answer variable when the user presses Enter.

Figure 3: The initialization script, which sets some project values and prompts the user for a phrase.

As the script loops through the answer variable, each character is added to the list as a unique item. For example, if the user enters Hi Mike, then the seven-character variable becomes a list with seven items. Item 1 would be the letter H.

The add () to () block inside the repeat loop adds a blank entry to the list with each pass through the loop. The replace item () of () block replaces the last item in the list with the value in the current position of the answer block, as counted by the answer# variable. After incrementing the answer# counter, the loop checks to see if it should run again.

After each character in the answer variable is evaluated, the script sends a broadcast (encode) message, which signals the next part of the script into action.

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