This challenge is based around conversions between text and morse code.   It could be done as a straightforward Python program but if completed on a Raspberry Pi, Arduino or similar then it could be linked into the GPIO pins and a LED output could be produced. You can use the morse code files located here.

The Task

Read in the morse code file into a dictionary so letters can easily be converted to their morse code equivalent.

Some Helpful Hints.

This is not a complete solution but it should help you get started and providing you read carefully you should be able to figure out the missing steps.

Step1: Start by opening the file I have provided below using the open command.

f = open('morsecodeshort.txt', 'r')

Notice how we are using r as we are reading from the file. Once the file is open you can loop through the lines as shown on the linked page and below

for line in f:

Test this is working by printing out each line.

Step 2: Each line consists of a letter and the corresponding morse code.  They are separated by tabs.  We are going to store our text file in a Python Dictionary (aka Associative Array aka Key Value Array) but before we can do this we must split each line using

key, value = line.split(‘t’)

With the split done we should store our key/values into a dictionary.

Notice that I have used strip() to remove unnecessary rubbish from the ends of the keys and values and don’t forget to declare the dictionary first

morsedict = dict()

Step 3: Now you have your morse code dictionary loaded you are ready to get words from the command line.   You can use the raw_input command to achieve this.

phrase = raw_input(“Enter the phrase to transmit: “)

Before we can process the input we must split our word into an list (aka array) of single letters

phraselist = list(phrase)

With the letters in a list we are now in a position to loop through the letters finding the corresponding morse codes and transmit them.

Step 4: To find each code you will want to use the following command

code = morsedict.get(letter.upper())

Note how I’m converting the letter to uppercase to match the uppercase letters in the text file.

Possible Extension

On a Raspberry Pi you could output the message as a series of LED flashes.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.