Morse Code Christmas Greetings: Part One

This will be the first thing I  build for Yago. He’s interested in science and maths but because he’s 8 years old he’s also interested in SPYING and SECRET CODES. That’s why I decided to build him a simple device that flashes a LED and says “Merry Christmas Yago and Cecilia”. The purpose of this device is to:

  • Help Yago realize what can be done with electronics
  • Help  him realize that you can do it yourself and not just buy something from a shop
  • Help him get started with the basics of simple encryption
  • Actively involve him with this (he has to solve the code)

So let’s get to work!

The Basics

Main materials used in this project:

  • The Attiny85 microcontroller
  • The Arduino IDE for programming the Attiny
  • Misc electronics for putting everything together

The Morse code

First of all we need to get our Christmas greeting into Morse code. I used this site to translate plain text to Morse but the problem is that we end up with something that looks like this . -..- .- — .–. .-.. . / .. -. .–. ..- – and we need to get that translated into something more useful. At first I thought It’s just as easy as converting the dots to ones, the dashes to twos and the spaces between words (represented by a slash in the Morse translation) into zeros. After the conversion I just have to convert the numbers into the amount of time the LED has to be on and I’m done, right? Wrong. If I do that there’s no way distinguish between the end of one flash and the start of the next. Each letter would just be the LED being on for various intervals of time. Not very easy to decode, so something has to be done with that. For clarity’s sake I wrote this small java programme to convert the dash and dots to ones, twos and zeros:

public class Translator { public static void main(String[] args) {

String code = “…. – – .–. -.-. — .-.. — -. … .-.. .- … …. … .-.. .- … …. …. .- -.-. -.- .- -.. .- -.– -.. — – -.-. — –“;

String result = “”; for (int i = 0; i < code.length(); i++){

String a = code.substring(i, i+1);

switch (a) {

case ” “: result += “0”; break;

case “.”: result += “1”; break;

case “-“: result += “2”; break;

default: result += “0”; break; }}

System.out.println(result); }}

This programme gives us the output of 1111020201221021210222012110222021011101211012011101111011101211012011101111011110120212102120120211012021220211022202021210222022 which we can use in our Arduino IDE to programme the Attiny.

Programming the Microcontroller

For programming I’m using the Arduino IDE and the Pololu programmer. I got the programmer for another project (that has been on a backburner for almost half a year now) so now I finally get to do something useful with it. I had to add the Pololu programmer to the programmers.txt in the Arduino files and use the tweaks found in this link to get the Arduino IDE to programme bare AVRs, but both of these steps were quite easy so it wasn’t a hassle. If you’re interested in making a similar setup this e-book does a great job in explaining everything. Here’s a picture of the final setup with the LED attached to pin 0. IMG_20131207_112944 And now to write some code. Here’s the first version: int led = 0;

String code = “3001021120201210120002110110212021201”;
int length = code.length();
 
void setup() {                
 
  pinMode(led, OUTPUT);
  digitalWrite(led,LOW);
 
}
 
 
void loop() {
 
  for (int i = 0; i <= length; i++){
    int a = code.substring(i,i+1).toInt(); 
    switch(a){
    case 0:
      delay(1000);
      break;
    case 1:  
      digitalWrite(led, HIGH);
      delay(300);
      digitalWrite(led, LOW);
      delay(200);
      break;
    case 2:
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      delay(200);
      break;
    case 3:
      for (int j = 0; j < 10; j++){
        digitalWrite(led, HIGH);
        delay(100);
        digitalWrite(led, LOW);
        delay(100);
 
      }
      break;
    }
  }
}

So what does this programme do? It takes the string labelled code and goes over it letter by letter, which in turn is converted to an integer. The switch statement checks the integer and does the following things:

  1. Integer = 1 –> LED on for 300 milliseconds and off for 200 milliseconds.
  2. Integer = 2 –> LED on for 1000 milliseconds and off for 200 milliseconds.
  3. Integer = 0 –> wait with the LED off for 1000 milliseconds.
  4. Integer = 3 –>flash the LED on and off every 100 milliseconds 10 times to mark the beginning and end of the message.

It works really well with the example code so I thought I was ready but then I saw this contest on Hackaday: I only have to hide http://hackaday.com in my microcontroller project to qualify for an entry and get a chance to win a Fubarino board that I can use in another cool project. After modifying the code for a bit I ended up with this:

int led = 0;
int speed = 100;
String message = “300122202220212201011202112000021022201012110000212201202210222000010200002121010212101101211011012”;
 
String secretMessage = “1111020201221021210222012110222021011101211012011101111011101211012011101111011110120212102120120211012021220211022202021210222022”;
 
 
void setup() {                
 
  pinMode(led, OUTPUT);
  digitalWrite(led,LOW);
 
}
 
 
void loop() {
 
 morseCode(message);
}
 
void morseCode(String code){  
  
  for (int i = 0; i <code.length(); i++){
    int a = code.substring(i,i+1).toInt(); 
   switch(a){
   case 0:
     delay(10*speed);
     break;
   case 1:  
     digitalWrite(led, HIGH);
     delay(3*speed);
     digitalWrite(led, LOW);
     delay(2*speed);
     break;
   case 2:
     digitalWrite(led, HIGH);
     delay(10*speed);
     digitalWrite(led, LOW);
     delay(2*speed);
     break;
    case 3:
    if (speed > 10){
    speed = 3;
    morseCode(secretMessage);
    speed = 100;
    break;    
    }
    break;
 
  }
}
}

This code has the message JOYEUX NOEL YAGO ET CECILIA (Yago is half-French and that’s the language I use with him) in Morse that it will communicate by flashing the LED at the same speed as in the previous example but instead of just quickly flashing the LED to mark the beginning and end of the message it now flashes a secret message 33 times the original signaling speed. The secret message is , of course, HTTPCOLONSLASHSLASHHACKADAYDOTCOM.

This is completely imperceptible and it just looks like the LED is flashing at random. Or at least it is supposed to do so. The trouble is that the LED isn’t flashing at all. After doing some googling I found out that you should avoid using strings with microcontrollers because they hog too much memory. Indeed, if I made the strings shorter, this version of the programme would work. The solution was to rewrite my original java programme to make it output an array instead of a string and rewrite the microcontroller programme to work with arrays. Here is the final version of the programme:

int led = 0;

int speed = 100;
byte message[] = {3, 0, 0, 1, 2, 2, 2, 0, 2, 2, 2, 0, 2, 1, 2, 2, 0, 1, 0, 1, 1, 2, 0, 2, 1, 1, 2, 0, 0, 0, 0, 2, 1, 0, 2, 2, 2, 0, 1, 0, 1, 2, 1, 1, 0, 0, 0, 0, 2, 1, 2, 2, 0, 1, 2, 0, 2, 2, 1, 0, 2, 2, 2, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 2, 1, 2, 1, 0, 1, 0, 2, 1, 2, 1, 0, 1, 1, 0, 1, 2, 1, 1, 0, 1, 1, 0, 1, 2};
int mLength = 99;
byte secretMessage[] = {1, 1, 1, 1, 0, 2, 0, 2, 0, 1, 2, 2, 1, 0, 2, 1, 2, 1, 0, 2, 2, 2, 0, 1, 2, 1, 1, 0, 2, 2, 2, 0, 2, 1, 0, 1, 1, 1, 0, 1, 2, 1, 1, 0, 1, 2, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 2, 1, 1, 0, 1, 2, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 2, 0, 2, 1, 2, 1, 0, 2, 1, 2, 0, 1, 2, 0, 2, 1, 1, 0, 1, 2, 0, 2, 1, 2, 2, 0, 2, 1, 1, 0, 2, 2, 2, 0, 2, 0, 2, 1, 2, 1, 0, 2, 2, 2, 0, 2, 2};
int sMLength = 130;
 
void setup() {                
 
  pinMode(led, OUTPUT);
  digitalWrite(led,LOW);
}
 
 
void loop() {
 
 morseCode(message, mLength);
   delay(2000);
}
 
void morseCode(byte code[], int length){  
  
  for (int i = 0; i <length; i++){
   int a = code[i];
   switch(a){
   case 0:
     delay(10*speed);
     break;
   case 1:  
     digitalWrite(led, HIGH);
     delay(3*speed);
     digitalWrite(led, LOW);
     delay(2*speed);
     break;
   case 2:
     digitalWrite(led, HIGH);
     delay(10*speed);
     digitalWrite(led, LOW);
     delay(2*speed);
     break;
    case 3:
    if (speed > 10){
    speed = 3;
    morseCode(secretMessage, sMLength);
    speed = 100;
    break;    
    }
    break;
 
  }
}
}
And that did the trick! The compiled programme is much shorter with the arrays (1 356 bytes vs 3 408 bytes in the version with strings) and it runs really smoothly!
Now I just have to build the thing!
About these ads

3 thoughts on “Morse Code Christmas Greetings: Part One

  1. Pingback: Fubarino Contest: Morse Code Christmas Baubles

  2. Pingback: Belgaum news | About Belgaum | Belgaum information | Belgaum district | Belgaum city | Belgaum Hotels | Belgaum People | Belgaum tourism | Belgaum entertainment | Belgaum students | Inside facebook | Hack | make use of | technical news | | Fubarino Contes

  3. Pingback: Fubarino Contest: Morse Code Christmas Baubles | Hack The Planet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s