Try Embedded

"C basics: a Morse code beacon from a string"

Turn any text into Morse code on an LED and a buzzer, with a lookup table of strings and one small function per job.

Basic · Beginner

In a real product: Emergency locator beacon (Aerospace and drones): A beacon flashes and beeps its identifier in Morse so rescuers can find it.

Morse code is a fine first exercise in strings. Every letter is itself a short string of dots and dashes, so a whole message is a string you walk through one character at a time, looking each one up in a table. This sketch sends SOS TE again and again, on the LED on pin 12 and the buzzer on pin 8, and prints each letter's code.

What you'll learn

  • A C string is an array of char ending in '\0', and a loop that stops there
  • An array of strings (const char *const CODES[26]) as a lookup table, indexed with c - 'A'
  • toupper(), and checking that a character is a letter before using it as an index
  • Breaking a job into small functions (dot, dash, sendLetter, sendText), each with one purpose

How it works

The timing comes from one unit, UNIT = 120 ms. A dot is 1 unit on, a dash 3. The gap inside a letter is 1 unit, between letters 3, and between words 7.

sendText() walks the message with a pointer, for (const char *p = text; *p; p++), and the loop ends at the terminating '\0'. A space waits out the word gap. A letter is turned into an index into CODES: 'A' is 0 and 'Z' is 25, because letters are consecutive in ASCII. sendLetter() then walks that letter's own string of . and - the same way.

Anything that isn't a letter is skipped. Without that check, a digit would give a negative index and read memory outside the table. That is a classic C bug, and the compiler won't warn you.

Try this

  • Change the message to your name.
  • Add the digits 0 to 9 with a second table (-----, .----, …) and index it with c - '0'.
  • Count the dots and dashes in the message with strlen() on each code, and print the total time it takes to send.

sketch.ino

// A Morse code beacon: the text becomes dots and dashes on an LED (pin 12) and a buzzer (pin 8).
#include <ctype.h>

const int LED = 12, BUZZER = 8;
const unsigned UNIT = 120;              // ms: a dot; a dash is 3, the gaps 1, 3 and 7
const char *const CODES[26] = {
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
  "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
};

void signal(unsigned units) {
  digitalWrite(LED, HIGH);
  tone(BUZZER, 700);
  delay(units * UNIT);
  digitalWrite(LED, LOW);
  noTone(BUZZER);
  delay(UNIT);                          // the gap inside a letter
}

void dot() { signal(1); }
void dash() { signal(3); }

void sendLetter(char c) {
  const char *code = CODES[c - 'A'];
  Serial.print(c);
  Serial.print(' ');
  Serial.println(code);
  for (const char *p = code; *p != '\0'; p++) {
    if (*p == '.') dot();
    else dash();
  }
  delay(2 * UNIT);                      // 3 units between letters (1 already waited)
}

void sendText(const char *text) {
  for (const char *p = text; *p; p++) {
    char c = toupper(*p);
    if (c == ' ') delay(4 * UNIT);      // 7 between words
    else if (c >= 'A' && c <= 'Z') sendLetter(c);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  sendText("SOS TE");
  Serial.println("--");
  delay(7 * UNIT);
}

Lessons behind it