Morse on Arduino

A few months ago, my seven-year-old son and I took a long car trip together. To redeem that time of extended idleness, we listened to The Mysterious Benedict Society, a novel about four orphans who team up against a mad scientist trying to take over the world. They regularly communicate with Morse code.

My son and I decided that we’d learn Morse code this summer. We’ve been sending each other messages to decode, and we practice recognizing letters using an app that I wrote for my phone. This past weekend, we decided to write a Morse code flasher. Given a message, we’d flash an LED to emit the dots and dashes of the message’s Morse code translation. I had my son sit at the computer, and I plugged in an Arduino. I told him what code to write, and he typed it in. He picked the message to broadcast. Can you tell what it is? This is the Arduino sketch that we wrote:
#define LETTER_DELAY 4000
#define WORD_DELAY 3000

void dash() {
  digitalWrite(13, HIGH);
  delay(3000);
  digitalWrite(13, LOW);
  delay(500);
}

void dot() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(500);
}

void t() {
  dash();
  delay(900);
}

void h() {
  dot();
  dot();
  dot();
  dot();
  delay(900);
}

void e() {
  dot();
  delay(900);
}

void c() {
  dash();
  dot();
  dash();
  dot();
  delay(900);
}

void a() {
  dot();
  dash();
  delay(900);
}

void i() {
  dot();
  dot();
  delay(900);
}

void s() {
  dot();
  dot();
  dot();
  delay(900);
}

void n() {
  dash();
  dot();
  delay(900);
}

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  t();
  h();
  e();
  delay(WORD_DELAY);

  c();
  a();
  t();
  delay(WORD_DELAY);

  i();
  s();
  delay(WORD_DELAY);

  n();
  i();
  c();
  e();
  delay(WORD_DELAY);
}

We started with functions dot and dash. Then we wrote functions t, h, e, c, a, i, s, and n. Though there are shorter ways to write this (make a table of each letter and its encoding, and then loop through the message, looking up the Morse sequence in the table and calling dash and dot appropriately), this approach was one that my son understood pretty clearly. Honestly, I really like this example for motivating procedural decomposition and code reuse. I may have to use it in our introductory programming course! My son and I haven’t written a lot of code together, but he’s often watched me. He took to it pretty well and caught on to several patterns and rules: parentheses must balance (he marveled at the highlighting the IDE did when we closed off a pair), lines must end in semicolons, and durations had to be expressed in milliseconds (he corrected a place where we had entered seconds). He wondered why the words were colored differently, what case should be used for different strings of text (OUTPUT vs. digitalWrite), and what void meant. I let my son pick all the durations, and I intended for us to use constants to make them easy to change, but our time was cut short by a birthday party! When I code with my son, I observe what I think is one of the primary benefits of blocks programming languages like Scratch: typing is excruciatingly slow at this age. My son does not know where the letters are. He did catch on to the Shift key pretty quickly, but we still saw a number of 90s instead of ()s, which he mostly self-corrected. Copy and paste came in handy! He asked why the keyboard had two commas. I asked him to type both, and he pointed out that one was an apostrophe. I’m excited for the fall so I can send my son secret messages in his lunch.

Comments

Leave a Reply

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