Archive for the 'Arduino' Category

Unimaginably Pointless Arduino Projects

Saturday, January 2nd, 2010

One of the best presents I received for Christmas this year was an Arduino starter kit. It’s been about 20 years since I did anything involving electronics; back in school I was fascinated with the subject, and the Arduino has suddenly reignited my interest.

The starter kit contains quite an interesting array of sensors (LDR, piezzo vibration sensor, thermistor), but a very limited number of basic components (such as resistors) in order to hook them up. This unfortunately has reduced what I can build for the moment to flashing LEDs. The first project I tackled was a nice colour-shifting effect with a tri-colour LED. Quite pointless, you might agree, but certainly not unimaginably pointless. I needed more pointlessness…

The unimaginably pointless project I decided on was more of a software project than an electronics one. Here’s the circuit I built:
Morse Code Circuit

I’ve attached the anode of an LED to the Arduino’s digital pin 13, and it’s cathode to ground. The Arduino has a built in resistor on pin 13, so this circuit is really the “Hello, World” of electronics. Not particularly complicated, I’m sure you’ll agree.

Arduino

So what will this solitary LED do for us? Well my unimaginably pointless idea was to convert stuff into Morse code. There are several examples of this on the web already. For the most part, I’ve blatantly copied them, adding a few little modifications and enhancements along the way.

First up, I’ll create a Morse library for the Arduino board. This is based heavily on code from two sources:

  1. David A. Mellis’s example code for creating a library
  2. Mark VandeWettering’s excellent morse implementation

The Morse Code library looks like this:

/*
  MorseCode.cpp - Morse code library
  Created by Rob Poyntz 2nd Jan 2010
  Based on code by:
    1) David Mellis: http://www.arduino.cc/en/Hacking/LibraryTutorial
    2) Mark VandeWettering: http://brainwagon.org/2009/11/14/another-try-at-an-arduino-based-morse-beacon/
*/
#include "MorseCode.h"

struct t_mtab { char c, pat; };
struct t_mtab morsetab[] = {
   {'.', 106}, {',', 115}, {'?', 76}, {'/', 41},
   {'A', 6},   {'B', 17},  {'C', 21}, {'D', 9},
   {'E', 2},   {'F', 20},  {'G', 11}, {'H', 16},
   {'I', 4},   {'J', 30},  {'K', 13}, {'L', 18},
   {'M', 7},   {'N', 5},   {'O', 15}, {'P', 22},
   {'Q', 27},  {'R', 10},  {'S', 8},  {'T', 3},
   {'U', 12},  {'V', 24},  {'W', 14}, {'X', 25},
   {'Y', 29},  {'Z', 19},  {'1', 62}, {'2', 60},
   {'3', 56},  {'4', 48},  {'5', 32}, {'6', 33},
   {'7', 35},  {'8', 39},  {'9', 47}, {'0', 63}};

#define N_MORSE  (sizeof(morsetab) / sizeof(morsetab[0]))
#define SPEED  (12)
#define DOTLEN  (1200 / SPEED)
#define DASHLEN  (3 * (1200 / SPEED))
#define LOWERCASE_A (97)
#define LOWERCASE_Z (122)
#define CASE_SHIFT (32)

MorseCode::MorseCode(int pin)
{
  _pin = pin;
  _Unknown = '?';
  pinMode(_pin, OUTPUT);
}

void MorseCode::dash()
{
  digitalWrite(_pin, HIGH);
  delay(DASHLEN);
  digitalWrite(_pin, LOW);
  delay(DOTLEN);
}

void MorseCode::dot()
{
  digitalWrite(_pin, HIGH);
  delay(DOTLEN);
    digitalWrite(_pin, LOW);
  delay(DOTLEN);
}

void MorseCode::Signal(char c)
{
  int i;

  if (c == ' ')
  {
    Serial.print(c);
    delay(7 * DOTLEN);
    return;
  }

  if ((byte(c) >= LOWERCASE_A) && (byte(c) < = LOWERCASE_Z))
    c = char(byte(c) - CASE_SHIFT);

  // Loop through the Morse Code table, and try to find the requested character
  for (i = 0; i < N_MORSE; i++)
  {
    if (morsetab[i].c == c)
    {
      unsigned char p = morsetab[i].pat;

      while (p != 1)
      {
        if (p & 1)
          dash();
        else
          dot();
        p = p / 2;
      }
      delay(2 * DOTLEN);
      return;
    }
  }

  /* If we can't find the requested character, signal an 'unknown' */
  Signal(_Unknown);
}

And the header file:

/*
  MorseCode.h - Morse code library
  Created by Rob Poyntz 2nd Jan 2010
  Based on code by:
    1) David Mellis: http://www.arduino.cc/en/Hacking/LibraryTutorial
    2) Mark VandeWettering: http://brainwagon.org/2009/11/14/another-try-at-an-arduino-based-morse-beacon/
*/
#ifndef MorseCode_h
#define MorseCode_h

#include "WProgram.h"

class MorseCode
{
  public:
    MorseCode(int pin);
    void Signal(char c);

  private:
    int _pin;
    char _Unknown;
    void dot();
    void dash();
};

#endif

Mark’s project turned a hard-coded string into Morse code, but this seemed a little inflexible. I decided that I’d read content from the serial port and convert that – it’s pretty trivial to acheive. Here’s the Arduino sketch I used:

#include <MorseCode.h>

const int OutputPin = 13;

MorseCode morse(OutputPin);

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  char c;
  if (Serial.available())
  {
    c = Serial.read();
    morse.Signal(c);
  }
}

And finally, finding stuff to turn into Morse code… I wrote a little bash script to grab the title from the latest headline in Slashdot’s RSS feed:


wget -O - http://rss.slashdot.org/Slashdot/slashdot 2>/dev/null | \
sed -r -e '/<title>/!d' -e '/Slashdot/d' -e 's/<title>(.*)<\/title>/\1/' | head -n 1 | tee /dev/ttyUSB0

Brilliant – converting readable text into an annoying flashing light :)