Chipkit Uno morse code flasher

In this simple project we will create a simple morse code flasher, you will use the serial monitor to type in a message and this will then be flashed in morse code on an LED. If you wish to learn more about morse code I would advise you to visit wikipedia – https://en.wikipedia.org/wiki/Morse_code , this will show you all you need to know.

You will need the following parts

An Chipkit Uno
An LED and 330 ohm resistor

The LED is connected to D7 in this example

 

Code

[codesyntax lang=”cpp”]

const int ledPin = 7;

// constants for tone and rest durations
const int dotlength = 100;
const int dashlength = dotlength * 3;
const int inter = dotlength; 
const int lgap = dotlength * 2; // inter-letter gap
const int wgap = dotlength * 4; //inter-word gap


void setup() 
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  char thischar;
  if (Serial.available())
  {
    // read a single character
    thischar = Serial.read();
    if (thischar>='a' && thischar<='z')
    {
      // convert to upper case
      thischar = thischar -32; 
    }
    // swap for a space if not in A-Z
    if(thischar<65 || thischar>90)
    {
      thischar=' '; 
    }
    ConvertToMorse(thischar);
    delay(lgap);
  }
}


void dot()
{
  // play a dot
  digitalWrite(ledPin, HIGH);
  delay(dotlength);
  digitalWrite(ledPin, LOW);
  delay(inter);
}

void dash()
{
  // play a dash
  digitalWrite(ledPin, HIGH);
  delay(dashlength);
  digitalWrite(ledPin, LOW);
  delay(inter);
}

void ConvertToMorse(char letter)
{
  // letters are in order of frequency
  switch(letter)
  {
  case 'E':
    dot();
    return; 
  case 'T':
    dash();
    return; 
  case 'A':
    dot();
    dash();
    return;
  case 'O':
    dash();
    dash();
    dash();
    return; 
  case 'I':
    dot();
    dot();
    return;
  case 'N':
    dash();
    dot();
    return;
  case 'S':
    dot();
    dot();
    dot();
    return;
  case 'H':
    dot();
    dot();
    dot();
    dot();
    return;
  case 'R':
    dot();
    dash();
    dot();
    return;
  case 'D':
    dash();
    dot();
    dot();
    return;
  case 'L':
    dot();
    dash();
    dot();
    dot();
    return;
  case 'C':
    dash();
    dot();
    dash();
    dot();
    return;
  case 'U':
    dot();
    dot();
    dash();
    return;
  case 'M':
    dash();
    dash();
    return;
  case 'W':
    dot();
    dash();
    dash();
    return;
  case 'F':
    dot();
    dot();
    dash();
    dot();
    return;
  case 'G':
    dash();
    dash();
    dot();
    return;
  case 'Y':
    dash();
    dot();
    dash();
    dash();
    return;
  case 'P':
    dot();
    dash();
    dash();
    dot();
    return;
  case 'B':
    dash();
    dot();
    dot();
    dot();
    return;
  case 'V':
    dot();
    dot();
    dot();
    dash();
    return;
  case 'K':
    dash();
    dot();
    dash();
    return;
  case 'J':
    dot();
    dash();
    dash();
    dash();
    return;
  case 'X':
    dash();
    dot();
    dot();
    dash();
    return;
  case 'Q':
    dash();
    dash();
    dot();
    dash();
    return;
  case 'Z':
    dash();
    dash();
    dot();
    dot();
    return;
  case ' ':
    delay(wgap);
    return; 
  }
}

[/codesyntax]

Related posts

Chipkit Max32 and ADXL335 accelerometer example

Chipkit Max32 and LSM303 Accelerometer example

Chipkit Max32 and LSM303 Magnetometer example

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More