Post by Dominic A on Apr 15, 2015 16:48:04 GMT -8
Morse Code Generator
Objective: Students will write a program that will be able to flash a Morse code message using an LED.
Things learned: How to light up an LED. How to use functions to make programs simpler.
Morse Code has been used for many years to communicate. Even recently, it has been used to send secret messages. Just in 2010, the Colombian military hid it in a pop song to send a message to hostages (here).
Note: I designed this for students with very little programming experience (they only had a basic Hello World an some basic variable manipulation before this). If your group is more advanced, I have some ideas for further expansion at the end of this post.
Stage 1: Lighting the LED
Give students the following code
This code will flash an onboard LED on and off every 200 ms (the onboard LED is linked to the output of pin 13). After students have this working, ask them to make the following modifications to the code.
Solutions (highlight text to view):
To make the LED be off most of the time, and then quickly flash on, shorten the first delay, and lengthen the second delay.
To make the LED be on most of the time, and then quickly flash off, lengthen the first delay, and shorten the second delay.
To make the LED blink faster, shorten both delays. When the LED blinks really quickly, it appears to be on all the time but dimmer. (This will has implications for how the motors can drive slowly or quickly).
After students finish this, have them wire up an LED to pin 13. See the Pi-Bot Manual for detailed instructions about how to do this.
Basically, place a wire from pin 13 to an open row on the breadboard. Place a small resistor (~200 ohms) in that row and another row. Connect the positive end of the LED to the resistor, and the negative end to the ground.
You should be able to see the LED flashing.
Stage 2: Flashing SOS in Morse Code
Now, ask the students to modify to flash SOS in Morse code (... --- ...), three short blinks, three long blinks, and three short blinks.
Hint: The four lines in the loop turn the LED on and off once, what happens if you copy that code many times?
When they are finished, the code should look something like this.
Stage 3: Using functions to improve the code
Ask the students if they thought copying the code over and over again was a good idea. What would you have to do if you wanted to change how long a short or long flash was? Is this practical for longer messages?
Introduce them to functions to improve the code. I gave them the following template to follow to fill in
After they finish this, show them how to use the functions.
Tell them to rewrite their SOS program to take advantage of these new functions. The final code should look something like this.
Stage 4: Further Abstraction
The new functions for fast and slow blinks is nice, but would still could be difficult to use for longer messages, like
.... . .-.. .-.. --- / .-- --- .-. .-.. -.. (Hello World)
With a longer messages, it is easy to make a mistake typing it in, making the message much more difficult to understand. Also, as a programmer, it is difficult to tell what message we are writing. Instead, we can use more functions to represent each letter.
It would be difficult to understand if we made any mistake typing in any of the Morse code for each letter. Instead, we can use more functions to represent each different letter.
Have the students come up with a short message to encode in Morse Code. Have them figure out what letters they need to use, and write functions to blink the Morse code for that letter. A template for each function is below.
Here is an example code for my message "Hello World"
Once they have finished this, you can have them try to decode other student's messages as they flash.
Objective: Students will write a program that will be able to flash a Morse code message using an LED.
Things learned: How to light up an LED. How to use functions to make programs simpler.
Morse Code has been used for many years to communicate. Even recently, it has been used to send secret messages. Just in 2010, the Colombian military hid it in a pop song to send a message to hostages (here).
Note: I designed this for students with very little programming experience (they only had a basic Hello World an some basic variable manipulation before this). If your group is more advanced, I have some ideas for further expansion at the end of this post.
Stage 1: Lighting the LED
Give students the following code
//Set the proper pin for the LED
int led = 13;
void setup()
{
// Set the LED pin to be an output
pinMode(led, OUTPUT);
}
void loop()
{
//Turn the LED on, then off
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
This code will flash an onboard LED on and off every 200 ms (the onboard LED is linked to the output of pin 13). After students have this working, ask them to make the following modifications to the code.
- Make the LED be off most of the time, and then quickly flash on
- Make the LED be on most of the time, and then quickly flash off
- Make the LED blink faster. What happens if the LED is blinking really quickly (<5 ms delays)?
Solutions (highlight text to view):
To make the LED be off most of the time, and then quickly flash on, shorten the first delay, and lengthen the second delay.
To make the LED be on most of the time, and then quickly flash off, lengthen the first delay, and shorten the second delay.
To make the LED blink faster, shorten both delays. When the LED blinks really quickly, it appears to be on all the time but dimmer. (This will has implications for how the motors can drive slowly or quickly).
After students finish this, have them wire up an LED to pin 13. See the Pi-Bot Manual for detailed instructions about how to do this.
Basically, place a wire from pin 13 to an open row on the breadboard. Place a small resistor (~200 ohms) in that row and another row. Connect the positive end of the LED to the resistor, and the negative end to the ground.
You should be able to see the LED flashing.
Stage 2: Flashing SOS in Morse Code
Now, ask the students to modify to flash SOS in Morse code (... --- ...), three short blinks, three long blinks, and three short blinks.
Hint: The four lines in the loop turn the LED on and off once, what happens if you copy that code many times?
When they are finished, the code should look something like this.
//Set the proper pin for the LED
int led = 13;
void setup()
{
// Set the LED pin to be an output
pinMode(led, OUTPUT);
}
void loop()
{
// Three short flashes for S
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
// Three long flashes for O
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
// Three short flashes for S
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
// One final delay so there is a pause between each SOS sent
delay(1000);
}
Stage 3: Using functions to improve the code
Ask the students if they thought copying the code over and over again was a good idea. What would you have to do if you wanted to change how long a short or long flash was? Is this practical for longer messages?
Introduce them to functions to improve the code. I gave them the following template to follow to fill in
void fast()
{
//Insert code for a fast blink
}
void slow()
{
//Insert code for a slow blink
}
After they finish this, show them how to use the functions.
// Flashes an S in Morse code
fast();
fast();
fast();
Tell them to rewrite their SOS program to take advantage of these new functions. The final code should look something like this.
//Set the proper pin for the LED
int led = 13;
void setup()
{
// Set the LED pin to be an output
pinMode(led, OUTPUT);
}
void fast() // A fast blink
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
void slow() // A slow blink
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void loop()
{
// Three short flashes for S
fast();
fast();
fast();
// Three long flashes for O
slow();
slow();
slow();
// Three short flashes for S
fast();
fast();
fast();
// One final delay so each message looks distinct
delay(1000);
}
Stage 4: Further Abstraction
The new functions for fast and slow blinks is nice, but would still could be difficult to use for longer messages, like
.... . .-.. .-.. --- / .-- --- .-. .-.. -.. (Hello World)
With a longer messages, it is easy to make a mistake typing it in, making the message much more difficult to understand. Also, as a programmer, it is difficult to tell what message we are writing. Instead, we can use more functions to represent each letter.
It would be difficult to understand if we made any mistake typing in any of the Morse code for each letter. Instead, we can use more functions to represent each different letter.
Have the students come up with a short message to encode in Morse Code. Have them figure out what letters they need to use, and write functions to blink the Morse code for that letter. A template for each function is below.
//Function to display the letter S
void s()
{
fast();
fast();
fast();
// Add a short delay to distinguish letters
delay(500);
}
Here is an example code for my message "Hello World"
//Set the proper pin for the LED
int led = 13;
int letter_delay = 500; // Makes it easier to change delay between letters
void setup()
{
// Set the LED pin to be an output
pinMode(led, OUTPUT);
}
void fast() // A fast blink
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
void slow() // A slow blink
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void h() // Blink letter H
{
fast();
fast();
fast();
fast();
delay(letter_delay);
}
void e() // Blink letter E
{
fast();
delay(letter_delay);
}
void l() // Blink letter L
{
fast();
slow();
fast();
fast();
delay(letter_delay);
}
void o() // Blink letter O
{
slow();
slow();
slow();
delay(letter_delay);
}
void w() // Blink letter W
{
fast();
slow();
slow();
delay(letter_delay);
}
void r() // Blink letter R
{
fast();
slow();
fast();
fast();
delay(letter_delay);
}
void d() // Blink letter D
{
slow();
fast();
fast();
delay(letter_delay);
}
void loop()
{
// Display HELLO
h();
e();
l();
l();
o();
delay(1500) // Delay between words)
w();
o();
r();
l();
d();
}
Once they have finished this, you can have them try to decode other student's messages as they flash.
Further Explorations/For students with more programming experience
Write a better function to flash each letter
Right now, a separate function is used for each letter, which is really inefficient and makes the code really long, it could be further improved by using a Lookup Table, or an array holding the Morse code pattern for each letter. Then, they could be quickly looked up and called. (sorry, no example code yet!)
Have it be able to convert a string to flashes
This goes along with the previous one... once that code is written, make it able to read in a string character by character, displaying the Morse code for each letter. (sorry, no example code yet!)
Very Advanced: Use the line follower as a light sensor to decode someone else's message
The line follower consists of three light sensors. If the LED is placed close to the them, it is possible to read in the flashes. You could then read someone else's message and display it over the serial connection. (sorry, no example code yet!)
Write a better function to flash each letter
Right now, a separate function is used for each letter, which is really inefficient and makes the code really long, it could be further improved by using a Lookup Table, or an array holding the Morse code pattern for each letter. Then, they could be quickly looked up and called. (sorry, no example code yet!)
Have it be able to convert a string to flashes
This goes along with the previous one... once that code is written, make it able to read in a string character by character, displaying the Morse code for each letter. (sorry, no example code yet!)
Very Advanced: Use the line follower as a light sensor to decode someone else's message
The line follower consists of three light sensors. If the LED is placed close to the them, it is possible to read in the flashes. You could then read someone else's message and display it over the serial connection. (sorry, no example code yet!)