Morse Code


Sister

This page describes the Arduino implementation of Morse Code. The Cybord implementation can be found at MorseCompanion and related parts.


The pre-assembled ArduinoBoard has a great out-of-box experience. I've used the $35 ArduinoDiecimila which gets everything it needs from the on-board USB connector. The free software environment includes ready-to-run programs including Blink, which blinks an LED, the embedded version of hello world.

I took the Blink program and hacked it one subroutine at a time until it was sending The quick brown fox ..., telegraphy's own version of hello world. Try this code on your Arduino. Optionally, hook a speaker or headphone up to pin 12 to listen to the code.

        /*
         * Morse Code (based on Blink)
         * by Ward Cunningham
         *
         * http://www.arduino.cc/en/Tutorial/Blink
         * 
         * cat -u cu.usbserial-A7006z3S
         */

int ledPin = 13; // LED connected to digital pin 13 int spkrPin = 12; // speaker byte done = B10000000; // high bit only means done

void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(spkrPin, OUTPUT); Serial.begin(9600); }

void loop() // run over and over again { text("the quick brown fox jumped over the lazy dogs back \n"); }

void text(char* t) { while (char c = *t++) { Serial.print(c, BYTE); ascii(c); } }

void ascii(char c) { byte code[] = { B01100000,B10001000,B10101000,B10010000,B01000000,B00101000, // abcdef B11010000,B00001000,B00100000,B01111000,B10110000,B01001000, // ghijkl B11100000,B10100000,B11110000,B01101000,B11011000,B01010000, // mnopqr B00010000,B11000000,B00110000,B00011000,B01110000,B10011000, // stuvwx B10111000,B11001000 // yz }; morse((c >= 'a' && c <= 'z') ? code[c - 'a'] : done); }

void morse(byte m) { while (m != done) { mark(m & done ? 3 : 1); space(1); m = m << 1; } space(3); }

void mark(byte t) { digitalWrite(ledPin, HIGH); int cycles = 100 * t; while(cycles--) { digitalWrite(spkrPin, 1); delayMicroseconds(500); digitalWrite(spkrPin, 0); delayMicroseconds(500); } }

void space(byte t) { digitalWrite(ledPin, LOW); delay(100 * t); }

 

Last edited October 10, 2008
Return to WelcomeVisitors