My First Project - Morse Code

I know ... a lot of people do a morse code project. This is my version. I won't go deep into description, and allow the code to speak for itself.

I welcome comments and critiques.

String sentence = "Hello!  Welcome to my morse code project!"; // word, sentence or special message to be displayed
String morseCode;

bool repeat = true;

// durations
int morseDitDurationMs = 50;
int morseDahDurationMs = 500;
int morseSpaceDurationMs = 1500; // ms to pause between words
int morseDelayBetweenCharsMs = 500; // ms to pause between each symbol
int repeatDelayMs = 5000; // ms to pause before repeating

int ledPin = LED_BUILTIN;

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

  sentence.toUpperCase();
  morseCode = getMorseCode(sentence);
  Serial.println("\"" + sentence + "\" = \"" + morseCode + "\"");
}

void loop() {
  for (int i = 0; i < morseCode.length(); i++) {
    char morseSymbol = morseCode[i];
    //Serial.print(morseSymbol);
    if (morseSymbol == ' ') {
      delay(morseSpaceDurationMs);
    } else {
      flashMorseSymbol(morseSymbol);
      delay(morseDelayBetweenCharsMs);      
    }
  }
  
  if (!repeat) exit(0);

  delay(repeatDelayMs);
}

// convert sentence to morse
String getMorseCode(String sentence) {
  if (sentence == "") return "";

  String morse = "";
  if (sentence[0] != '[' || sentence[sentence.length()-1] != ']') { 
    // convert each character
    for (int i = 0; i < sentence.length(); i++) {
      morse += getMorseCodeFromChar(sentence[i]);
    }
  } else {
    // convert special messages
    if (sentence == "[SOS]") morse = "...---...";
    if (sentence == "[NEW_LINE]") morse = ".-.-"; 
    if (sentence == "[NEW_PAGE]") morse = ".-.-.";
    if (sentence == "[NEW_PARAGRAPH]") morse = "-...-";
    if (sentence == "[ATTENTION]") morse = "-.-.-";
    if (sentence == "[ERROR]") morse = "........";
    if (sentence == "[WAIT]") morse = ".-...";
    if (sentence == "[BREAK]") morse = "-... -.-";
    if (sentence == "[CLOSING]") morse = "-.-. .-..";
    if (sentence == "[SHIFT_TO_WABAN]") morse = "-..---";
    if (sentence == "[END_OF_CONTACT]") morse = "...";
    if (sentence == "[UNDERSTOOD]") morse = "...-.";
    if (sentence == "[INVITE_NAMED_STATION_TO_TRANSMIT]") morse = "-.--.";
    if (sentence == "[INVITE_ANY_STATION_TO_TRANSMIT]") morse = "-.-";
  }
  return morse;
}

// convert single character to morse (international)
String getMorseCodeFromChar(char character) {
  String charMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,?!'():;/_=+-$@";
  String morseMap[] = {".-.","-...","-.-.","-..","."                  // A-E
                      ,"..-.","..-","....","..",".---"                // F-J
                      ,"-.-",".-..","--","-.","---"                   // K-O
                      ,".--.","--.-",".-.","...","-"                  // P-T
                      ,"..-","...-",".--","-..-","-.--","--.."        // U-Z
                      ,".----","..---","...--","....-","....."        // 1-5
                      ,"-....","--...","---..","----.","-----"        // 6-0
                      ," ",".-.-.-","--..--","..--..","-.-.--"        // [space].,?!
                      ,".----.","-.--.","-.--.-","---...","-.-.-."    // '():;
                      ,"-..-.","..--.-","-...-",".-.-.","-....-"      // /_=+-
                      ,"...-..-",".--.-."                             // $@
                    };

  int charPos = charMap.indexOf(character);
  return morseMap[charPos];
}

// flash dit or dah symbol
void flashMorseSymbol(char morseSymbol) {
  int ledOnDuration = 0;
  switch (morseSymbol) {
    case '.': ledOnDuration = morseDitDurationMs; break;
    case '-': ledOnDuration = morseDahDurationMs; break;
    default: ledOnDuration = 0;
  };

  digitalWrite(ledPin,HIGH);
  delay(ledOnDuration);
  digitalWrite(ledPin,LOW);
}

A dit element is one time unit
A dah element is three time units
Inter-element gap is one time unit
Inter-letters gap is three time units
Inter-word gap is seven time units.

Excellent! I can update my timing accordingly.

Good work. 73

In Magic Morse, it looks like:

void setspeed(byte value)
{
  WPM        = value;
  DITmS      = 1200 / WPM;
  DAHmS      = 3 * 1200 / WPM;
  wordSpace  = 7 * 1200 / WPM;
}