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

One thought on “Morse Code [Arduino Project]

  1. Pingback: Morse Code v2 [Arduino Project] | Ben Hovinga

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>