Back to Contents Page

Lesson: Flashing the lights

Using the work you have done in the previous lessons, you should now have a dictionary of all the conversions of characters to morse code. Using this dictionary you have written a function that will take a string and convert it into a string of morse code. Now we will write the function that will take this string of morse code and represent it using flashing lights with the Raspberry Pi.

Parsing the morse code string

Imports

We will want the LED to shine for varying periods of time to represent a dot, a dash, a space or signify the next letter.

To get Python to work with the I/O of a Raspberry Pi, we need to import a library called RPi.GPIO. To get this library, type:

import RPi.GPIO as GPIO

at the top of your Python file.

To vary the amount of time the LED switches on/off for, we will also need the time library. Similarly:

import time

Setting up the Raspberry Pi's I/O

The following code is needed for setting up the I/O.

ledPin = 23
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)

GPIO.setmode(GPIO.BCM) sets the I/O mode to "Broadcom SOC channel number" (BCM). Understanding this isn't important for implementing the morse code LED flasher.

GPIO.setup(ledPin, GPIO.OUT) sets the pin represented by ledPin (which is 23) to an output pin, allowing the pin to be either set to on or off.

We now have everything we need to begin to represent the characters of our morse code dictionary using the LED.

We need to write individual functions for a dot, a dash, a space and the next letter representation.

The dot should turn the LED on for a short period of time, then suspend the thread for the same amount of time, before turning the LED off and then again suspending the thread.

The dash function should be similar except the LED should turn on for a noticeably longer period of time.

These statements will help you write the functions you will require to represent morse code:

GPIO.output(pin, 1/0) - sets the specified pin of the I/O to on(1)/off(0).

time.sleep(secs) - suspends execution of the current thread by the given number of seconds. (Pauses the program).

Write four functions, you will need

-dot()

-dash()

-nextLetter()

-space()

Once you have these functions, you are ready to begin writing a function that will parse your morse code string.

How we parse morse code strings is we look through each character in the string in order. Since we encoded the string we know that there can only be dashes, dots and spaces representing characters. If the character is none of these then it denotes a space. We can write an if statement for each of these cases and flash the LED accordingly.

Write a new function called encodeToLight, this function should take a string which is your sentence, as a parameter. Now we need to encode your sentence.

In a new string called encodedSentence, take your string and call your function to convert your sentence to morse code on it.

Now you have your encoded string, iterate through each character in the string and write out if statements that deals with the character being a dot, a dash and a space(remember that space in the encoded string denotes the end of a character, not a space!). If none of these are satisfied, the character must be denoting a space. You can deal with this case using a final else statement.

Marking

Example functions for each morse code character:

ledPin=23

def dot():
   GPIO.output(ledPin,1)
   time.sleep(0.2)
   GPIO.output(ledPin,0)
   time.sleep(0.2)

def dash():
   GPIO.output(ledPin,1)
   time.sleep(0.6)
   GPIO.output(ledPin,0)
   time.sleep(0.2)

def nextLetter():
   GPIO.output(ledPin,0)
   time.sleep(0.6)

def space():
   GPIO.output(ledPin,0)
   time.sleep(1.4)

Marks should be deducted if students do not clearly differentiate between each function. For example if the amount of time that the LED flashes for a dot and dash cannot be clearly differentiated. Students should also choose sensible and consistent interval times so that when the LED is read using the eye it is possible to discern which character is being represented. 25% of the marks for this lesson should be allocated to the LED functions.

50% of the marks should be allocated to the parsing of the string. An example of this function is given below:

def encodeToLight(sentence):
   encodedSentence = convertToMorseCode(sentence)
   for character in encodedSentence:
   if character == '-':
       dash()
   elif character == '.':
       dot()
   elif character == ' ':
       nextLetter()
   else:
       space()

Students should be awarded marks if they can successfully iterate through a string and correctly identify each character in the string. Marks should be awarded for the correct calling of the LED functions that they wrote earlier in this lesson.

The remaining 25% of the marks should be allocated based on the neatness and readability of the code. Factors such as variable naming should be taken into account. The variable names chosen should help to identify what the variables are representing. Similarly for functions, the function names should clearly describe what the functions are doing.