So I made a few changes to my previous Morse Code project. I added the option to send a message via serial. I am still waiting on my bluetooth module so I can send a message from my phone.
Syntax for the serial commands.
<message>
/wpm <speed>
/start
/stop
/restart
// Morse Code Converter v2.0 // By Ben Hovinga // http://ben.hovinga.me/ // Inital Message String message = "abcdefghijklmnopqrstuvwxyz 1234567890 "; // LED pin #define LEDPin 13 // Speed int wpm = 4; // Speed in Words Per Minute // Unit Sizes int unitDit = 1; int unitDa = 3; int unitSymb = 1; int unitLetter = 3; int unitWord = 7; // Calculate speed into ms. int timePerUnit = 1200/wpm; // 1200 for words per minute. Use 6000 for characters per minute int timeDit = timePerUnit * unitDit; int timeDa = timePerUnit * unitDa; int timeSymb = timePerUnit * unitSymb; int timeLetter = timePerUnit * unitLetter; int timeWord = timePerUnit * unitWord; // Morse Code and Referance int codeSize = 36; String code[] = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".----","..---","...--","....-",".....","-....","--...","---..","----.","-----"}; String coderef[] = { "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"}; // Other vars boolean running = true; void setup(){ // Make pin 13 output for LED (or later a relay). pinMode (LEDPin,OUTPUT); Serial.begin(9600); } void loop(){ int strlength = message.length(); for (int x = 0; x < strlength; x++){ if (running == true){ String ch = message.substring(x,x+1); String ch2 = message.substring(x+1,x+2); flashLetter(ch); if (ch == ""){ delay(timeWord); } else if (ch != "" && ch2 != "") { delay(timeLetter); } } if (checkSerial() == 1) { delay (2000); break; } } } int getInt(String text){ // Converts Strings into Ints char temp[20]; text.toCharArray(temp, 19); int x = atoi(temp); if (x == 0 && text != "0") { x = -1; } return x; } int checkSerial (){ // Check if there is data waiting from serial String input = ""; int count = Serial.available(); if (count > 0) { // Wait for buffer and recount delay (200); count = Serial.available(); // Get the buffer for (int x = 0; x < count; x++){ input += String(char(Serial.read())); } if (input.substring(0, 1) == "/"){ // Its a command int wherespace = input.indexOf(""); // find the space String comd; String value; if (wherespace == -1){ comd = input.substring(1); value = ""; } else { comd = input.substring(1,wherespace); comd.toLowerCase(); value = input.substring(wherespace + 1); value.toLowerCase(); } if (comd == "wpm"){ wpm = getInt (value); WPMcalc(); Serial.print ("Changed WPM to "); Serial.print (wpm); Serial.println ("."); } else if (comd == "restart"){ Serial.println ("Restarting message."); return 1; } else if (comd == "stop"){ Serial.println ("Stopping message."); running = false; } else if (comd == "start"){ Serial.println ("Starting message."); running = true; return 1; } else{ Serial.println("Command Not Found."); } } else { // Its a message message = input; Serial.print ("Message Changed to \""); Serial.print (message); Serial.println ("\"."); return 1; // Reset the loop } } return 0; } void WPMcalc (){ // Calculates the words per minute timePerUnit = 1200/wpm; // 1200 for words per minute. Use 6000 for characters per minute timeDit = timePerUnit * unitDit; timeDa = timePerUnit * unitDa; timeSymb = timePerUnit * unitSymb; timeLetter = timePerUnit * unitLetter; timeWord = timePerUnit * unitWord; } void flashLetter (String character){ // Flashes the letter String morseCode = searchLetter (character); int strlength = morseCode.length(); for (int x = 0; x < strlength; x++){ String ch = morseCode.substring(x,x+1); digitalWrite(LEDPin, HIGH); delay(timeDelayms(ch)); digitalWrite(LEDPin, LOW); if (x < strlength){ delay(timeSymb); } } } int timeDelayms (String character){ // Returns how long a dit or da is in ms. if (character == "."){ return timeDit; } else if (character == "-"){ return timeDa; } else { return 0; } } String searchLetter (String character){ // Returns the morse code for a character. for (int x = 0; x < codeSize; x++){ character.toLowerCase(); if (character == coderef[x]){ return code[x]; } } return ""; }