int t = 150; // one unit of time in milliseconds void setup() { pinMode(13, OUTPUT); } void dot() { digitalWrite(13, HIGH); delay(1*t); digitalWrite(13, LOW); delay(1*t); } void dash() { digitalWrite(13, HIGH); delay(3*t); digitalWrite(13, LOW); delay(1*t); } void letter() { delay(2*t); // plus one unit from end of dot/dash = 3 units } void word() { delay(6*t); // plus one unit from end of dot/dash = 7 units } void sendMorsePattern(char *pattern) { int i = 0; while (0 != pattern[i]) { int c = pattern[i]; if ('.' == c) dot(); else if ('-' == c) dash(); i = i+1; } letter(); } // An array of pairs associating a character with its Morse pattern. struct { char letter, *pattern; } letters[] = { 'a', ".-" , 'b', "-..." , 'c', "-.-." , 'd', "-.." , 'e', "." , 'f', "..-." , 'g', "--." , 'h', "...." , 'i', ".." , 'j', ".---" , 'k', "-.-" , 'l', ".-.." , 'm', "--" , 'n', "-." , 'o', "---" , 'p', ".--." , 'q', "--.-" , 'r', ".-." , 's', "..." , 't', "-" , 'u', "..-" , 'v', "...-" , 'w', ".--" , 'x', "-..-" , 'y', "-.--" , 'z', "--.." , '1', ".----" , '2', "..---" , '3', "...--" , '4', "....-" , '5', "....." , '6', "-...." , '7', "--..." , '8', "---.." , '9', "----." , '0', "-----" , ',', "--..--", '.', ".-.-.-", '?', "..--..", ';', "-.-.-.", ':', "---...", '/', "-..-. ", '-', "-....-", '\'', ".----.", '"', ".-..-.", '(', "-.--." , ')', "-.--.-", '=', "-...-" , '+', ".-.-." , '*', "-..-" , '@', ".--.-.", 0, 0 // sentinel values to mark the end of the array }; void sendMorseLetter(int c) { int i = 0; while (0 != letters[i].letter) { // for each association in the array if (c == letters[i].letter) { // if the letter matches c sendMorsePattern(letters[i].pattern); // send the pattern return; // and leave } i = i + 1; } word(); // not found: generate inter-word pause } void sendMorse(char *message) { int i = 0; while (0 != message[i]) { // for each character in the message sendMorseLetter(message[i]); // send it in Morse code i = i + 1; } } void loop() { // put your main code here, to run repeatedly: sendMorse("introduction to design track 2"); delay(8*t); }