Morse Code Christmas Greetings: Part Two

Making things is not my forte. I love programming because if something gives it doesn’t destroy everything and make you start over from scratch. With making things… well. Let’s say I’ve learned my lesson. Actually several of them. On several different occasions. So this time loads of planning, no errors! Yago1_schem

First I made a prototype on a breadboard because the setup is a bit different than the programming rig so I wanted to be sure it worked. I started by designing the entire thing on Fritzing. The final version will have less connections than when programming the attiny85 so it’s a good idea to make a schematic first so I don’t miss anything. I played around with all the components on a perfboard and I got them to fit in quite a tiny space. Now I was finally ready for some soldering!

IMG_20131210_151904My favorite method for soldering lots of little components is to put them all in their correct place, put another perfboard on top of that, attach it together with a rubber band or something, turn that around and then solder all the components in one go. Even such a noob as myself soldered everything together in under five minutes! (And this was my fourth time with the soldering iron.) No endless turning the board round and around and putting components in the wrong places! After that I just have to trim off the components and solder the connections and that’s it.

IMG_20131210_153603

Almost done!

I was going strong and was sure to finish today, but when I tried the device  didn’t work! After 1,5 hours of troubleshooting (Batteries checked, LED checked, connections checked, microcontroller retested in breadboard…) I realised that I had forgotten to solder pin 4 to ground. Noooo! But that will teach me to have my laptop in the other room and think that I can do without any visual aids while building something. Next time I’m soldering many components at the same time I’ll:

  1. calculate the exact number of connections I have to make
  2. write them each down on a piece of paper
  3. go through them one by one.

This circuit, the battery holder and the switch will all be encased in a Tic Tac box because I want Yago to

  • see all the electronics
  • see clearly that it is something build from not very fancy components

IMG_20131210_200615IMG_20131210_200833 Fortunately the Tic Tacs came in a set of two. I cut a hole in the first one for the switch like this (picture on the left) and intended to squeeze everything else inside the box. Unfortunately it would mean that they would rattle around, which is something we really don’t want. That’s why I cut out part of the other box that I could use as a surface to mount everything onto. This way I can slide everything out, change the batteries, slide everything back in again and it will all stay firmly in place. (It’s also a plus that I can send the device without batteries, I’ll save about 5 euros on postage.) After hot-gluing the battery holder and the electronics in place, soldering the power connections and gluing the switch to the side of the box the device is ready!

IMG_20131214_105730

Device open

IMG_20131214_105655

Device closed

And here’s a short video for the guys at Hackaday.

So that’s basically it! Taking into account that this is my first electronics project I’m quite happy with the way it turned out. The only setback happened right at the very end when I glued the switch on: apparently the glue and the plastic of the Tic Tac box weren’t compatible – the fumes from the over-generously applied glue ended up reacting with the plastic and forming a white, matte residue on the inside, but apart from that I‘m very satisfied with the way the device looks. 

Now Yago will be able to pass some time learning about Morse code and decoding the message. If he really, REALLY gets into electronics, in a couple of years I can tell him that the beginning/end signal of the message is code as well and he has to figure out how to decode that albeit its speed!

Thanks for reading this, and until next time!

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!