Morse Code v2.1 [Arduino Project]

I am having a lot of fun with this project. In this update I added the ability to use prowords. But what are prowords? Prowords are symbols and letters with special meanings, they are a combination of letters into one letter. In morse code [AAA] together is Full Stop or Period, or [IMI] is Say Again. The list goes on and on. Basically by adding letters together you will create prowords. In this program to send a proword enclose the letters in square brackets [] so the program knows its a proword and not just each letter on its own.
Example message
“HELLO[AAA] HOW ARE YOU[INT]” would be
“HELLO WORLD. HOW ARE YOU?”
The [AAA] would be morsed as .-.-.- instead of .-/.-/.-
The [INT] would be morsed as ..-.- instead of ../-./-

Another little thing I added was a delay after the message to show that the message was over. Now there is no need to add a space at the end of the message to do this.

// Morse Code Converter v2.1
// By Ben Hovinga
// http://ben.hovinga.me/ 
// All Rights Reservered.
// Copyright (C) 2012

// ChangeLog at bottom. :) 

// Inital Message
String message = "abcdefghijklmnopqrstuvwxyz[aaa] 1234567890[aaa]";

// 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;
boolean proword = false;

void setup(){
  // Make pin 13 output for LED (or later a relay).
  pinMode (LEDPin,OUTPUT);
  // I wanna talk with things.
  Serial.begin(115200);
}

void loop(){
  int strlength = message.length(); // How big is this message anyways?
  for (int x = 0; x < strlength; x++){
    if (running == true){
      // Find out what this letter is and the next one.
      String ch = message.substring(x,x+1);
      String ch2 = message.substring(x+1,x+2);

      if (ch == "["){
        // Skip this pass.
        proword = true;
      }
      else if (ch == "]"){
        // Skip this pass.
        proword = false;
      }
      else {
        // Flash the letter.
        flashLetter(ch);
      }

      if (proword == false){
        // Do delays if not a proword.
        if (ch == " "){
          // New word delay.
          delay(timeWord);
        }
        else if (ch != " " && ch2 != " ") {
          // New letter delay.
          delay(timeLetter);
        }
      }
    }
    if (checkSerial() == 1) {
      // We got some sort of command.
      // Delay a bit then start the message over.
      delay (2000);
      break;
    }
  }
  delay(timeWord); // Hold on and wait to show the message is done.
}

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

/*
CHANGELOG
------------------------------------------------
2012-02-12 (2.1)
- Upgraded to version 2.1
- Added ability to program prowords into message:
- - Eg. "[aaa]" is Full Stop (.) and will flash .-.-.-
- - Eg. "[int]" is Question Mark (?) and will flash ..-.-
- Added delay after message so last space will not be necessary to show end if message.
- Added a few comments (more to come).
------------------------------------------------
2012-01-23 (2.0)
- Started Change Logs.
- Upgraded to version 2.0
- Added Serial Communications.
- Added Serial Commands:
- - /start
- - /stop
- - /restart
- - /wpm <value>
- - <message>
- Corrected timing between words.
------------------------------------------------
*/

Morse Code v2 [Arduino Project]

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/ 
// All Rights Reservered.
// Copyright (C) 2012

// ChangeLog at bottom. :) 

// 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(115200);
}

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

/*
CHANGELOG
------------------------------------------------
2012-01-23 (2.0)
- Started Change Logs.
- Upgraded to version 2.0
- Added Serial Communications.
- Added Serial Commands:
- - /start
- - /stop
- - /restart
- - /wpm <value>
- - <message>
------------------------------------------------
*/

Morse Code [Arduino Project]

I just got an arduino because I wanted to learn how electronics work. And so far I like playing with it. As some of you have already seen, I made a traffic light on the weekend and made an easy to follow tutorial on instructables for anyone else wanting to make the project.

In this project I am focusing less on the electronics and more on the programming. In my job we are required to know Morse Code, and sometimes learning it can be challenging. I programmed my arduino to flash the led on pin 13 in relation to the Morse Code.

I only have letters and numbers programmed into it, no prowords yet. My next step is to add serial communications so I can change the message or change the words per minute on the fly. I ordered at bluetooth module and I will be able use my phone to control it too. Maybe some more things later but that’s the plan for now.

Here is the code if you want to try it out.

// Morse Code Converter v1.0
// By Ben Hovinga
// http://ben.hovinga.me/ 
// All Rights Reservered.
// Copyright (C) 2012

// Inital Message
String message = "Hello World  ";

#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
int messageCount = 0;

void setup(){
  // Make pin 13 output for LED (or later a relay).
  pinMode (LEDPin,OUTPUT);
}

void loop(){
  int strlength = message.length();
  for (int x = 0; x < strlength; x++){
    String ch = message.substring(x,x+1);
    flashLetter(ch);
    if (ch == " "){
      delay(timeWord);
    }
    else {
      delay(timeLetter);
    }
  }
}

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