ASER Lesson 3.5: Morse Code Programming
Morse code is a way of encoding a message so that it can be transmitted via a binary encoding (dots and dashes) such that a human can disc...
Morse code is a way of encoding a message so that it can be transmitted via a binary encoding (dots and dashes) such that a human can discern the message without much trouble. In a previous lesson, we programmed an Arduino to blink an LED at a fixed rate, but Ardunios can be programmed to do something more clever with an LED: blink a special message. In today's lesson, our goal is to learn how to program an Ardunio to blink a message in Morse code and use this as a platform for a game.
We will start off by providing a simple Morse code interface around the digitalWrite and delay functions provided by the Arduino's standard library:
int outputLED = 13;
int timeUnit = 250;
void dot() {
digitalWrite(outputLED, HIGH);
delay(timeUnit);
digitalWrite(outputLED, LOW);
delay(timeUnit);
}
void dash() {
digitalWrite(outputLED, HIGH);
delay(3 * timeUnit);
digitalWrite(outputLED, LOW);
delay(timeUnit);
}
void endLetter() {
delay(3 * timeUnit);
}
void endWord() {
delay(7 * timeUnit);
}
In particular, we provide the functions dot, dash, endLetter, and endWord, which take take no input, but instead perform an effect on pin 13: sending an electrical signal for the units of time specified by Morse code. We can then use these functions in order to program the Ardunio to send a specific sequence of signals corresponding to the message/word we would like it to broadcast via its LED. Here is an example of having the LED connected to pin 13 blink "SOS" in Morse code:
// S
dot();
dot();
dot();
endLetter();
// O
dash();
dash();
dash();
endLetter();
// S
dot();
dot();
dot();
endLetter();
endWord();
Notice how we are just describing a sequence of function calls to make in order to broadcast the message. It is important to note the fact that all function calls end with a semi-colon, and that function names are case-sensitive. Also, it is a good practice to provide a comment before each sequence representing a letter in morse code and separate them with extra lines as needed. We need to specify calls to endLetter and endWord in order to match the definition of Morse code, because without them it would be hard to distinguish the end of letters or words when trying to interpret the LED's pattern. While we could introduce for-loops or perhaps new functions to make it less tedious to specify words, especially if they have repeated letters, as a first taste of programming we keep the syntax and control flow simple.
This leads us to the next component of the lesson, which is the secret word game. We provide the students with starter code, which includes the SOS example and of course the Morse code interface, give each team a secret word on a slip of paper they draw from a box, and a sheet with the encoding of each letter in Morse code. The objective is for each team to build the simple LED circuit connected to pin 13, modify the starter program in order to encode their secret word using the Morse code interface, and upload this code to their Arduinos. Once a team's Arduino is blinking the word correctly, they will go off to other teams and try to figure out what word their Arduinos are blinking. Whichever team collects the most secret words wins and gets the most candy (all teams who participated got candy).