Morse code blinker

The microcontroller programming equivalent of "Hello World" seems to be "Blink the LED".

After going through some of the example code for my Netduino, I decided to expand a bit on the LED examples and come up with a simple little program that would make the onboard LED blink out Morse code.

After a few hours of futzing around trying to figure out the C# syntax and the relevant parts of the .NET Micro Framework, I finally came up with something that works pretty well. I called it MorseBlinker.

One of the problems I ran into was .NET Micro Framework API changes between 4.2 (which is what I had installed) and 4.1 which is what the examples in the Getting Started with Netduino book are written for. Also, apparently there's a big difference between "a" and 'a' in C# (although I imagine it's probably the same in C too). 

The string is hard coded into the program, so if i want to change what the Netduino blinks, I have to change the string and redeploy the app, which isn't too difficult. Timing is also hard coded too (in the dit variable which specifies the duration of a dit in milliseconds), so changing the speed it blinks at also requires the app to be rebuilt and redeployed.


using System;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace MorseBlinker
{
  public class Program
  {
    private static void blinker(OutputPort p, string a)
    {
      // Make the LED blink a morse code string provided by a
      // Change the duration of the blinking by adjusting the dit variable

      int dit = 50;                    // dit length in milliseconds
      int dah = dit * 3;                // dah length in milliseconds
      int space = dit * 7;              // space length in milliseconds

      foreach (char sym in a)
      {
        switch (sym)
        {
          case '.':
            p.Write(true);
            Thread.Sleep(dit);
            p.Write(false);
            Thread.Sleep(dit);
            break;
          case '-':
            p.Write(true);
            Thread.Sleep(dah);
            p.Write(false);
            Thread.Sleep(dit);
            break;
          case ' ':
            p.Write(false);
            Thread.Sleep(space);
            break;
          default:
            break;
        }
      }
      p.Write(false);
      Thread.Sleep(dit * 3);    // space between characters

    }
    public static void Main()
    {
      // Text to Morse code conversion table
      Hashtable morse = new Hashtable();

      morse.Add('a', ".-");
      morse.Add('b', "-...");
      morse.Add('c', "-.-.");
      morse.Add('d', "-..");
      morse.Add('e', ".");
      morse.Add('f', "..-.");
      morse.Add('g', "--.");
      morse.Add('h', "....");
      morse.Add('i', "..");
      morse.Add('j', ".---");
      morse.Add('k', "-.-");
      morse.Add('l', ".-..");
      morse.Add('m', "--");
      morse.Add('n', "-.");
      morse.Add('o', "---");
      morse.Add('p', ".--.");
      morse.Add('q', "--.-");
      morse.Add('r', ".-.");
      morse.Add('s', "...");
      morse.Add('t', "-");
      morse.Add('u', "..-");
      morse.Add('v', "...-");
      morse.Add('w', ".--");
      morse.Add('x', "-..-");
      morse.Add('y', "-.--");
      morse.Add('z', "--..");
      morse.Add('0', "-----");
      morse.Add('1', ".----");
      morse.Add('2', "..---");
      morse.Add('3', "...--");
      morse.Add('4', "....-");
      morse.Add('5', ".....");
      morse.Add('6', "-....");
      morse.Add('7', "--...");
      morse.Add('8', "---..");
      morse.Add('9', "----.");
      morse.Add(' ', " ");
      morse.Add('.', "·-·-·-");
      morse.Add(',', "--..--");
      morse.Add('?', "..--..");
      morse.Add('!', "-.-.--");
      morse.Add('/', "-..-.");

      // Text to play in Morse code. Change this to whatever you want
      string morseText = "ab4ug";

      OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

      while (true) {
        // go through all the characters in morseText
        foreach (char c in morseText)
        {
          // Blink the character in Morse code
          blinker(led, (string)morse[c.ToLower()]);
        }

        // Delay 1s before repeating the text
        led.Write(false);
        Thread.Sleep(1000);
      }
    }
  }
}

You can check out the latest version over at Github.

For my next trick, I think I'll try to make a speaker buzz at the same time as the light flashes.