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);
}