20 March 2010

Using Arduino software - user-defined functions (3)

/*
* Project: functions_Morse Code - MyName
* Author: Jane-Maree Howard
* Date: Saturday 20/03/2010
* Platform: Arduino 18
* Purpose: (1)To demonstrate a sketch for flashing Morse code
(2)This document explains how to create a library for Arduino.
* Operation: It starts with a sketch and explains how to convert its functions into a library.
* This allows other people to easily use the code that you've written
* and to easily update it as you improve the library.
* We start with a sketch that does simple Morse code:
* The code is simple & ideal for telegraphy: a sequence of 'dots' & 'dashes'
* (or 'dit' & 'dah') encoding alpha-numeric characters,
* beginning with 'e'='dit', & 't'='dah' (the most commonly used letters in
* English text, & progressing to the less common characters.
* Wikipedia gives the 'mark-space' intervals for Morse code as follows:
* 1/ A 'dash' is equal to 3 'dots'
* 2/ The space between parts of the same letter is equal to 1 'dot'
* 3/ The space between two letters is equal to 3 'dots'
* 4/ The space between two words is equal to 7 'dots'
* Here I spell out my name: Jane-Maree, using two levels of function call
*/

int pin = 13; //on-board LED
int dLay = 250; //basic delay

void setup()
{
pinMode(pin, OUTPUT);
}//end setup()

/* This loop makes use of user-defined functions called dot() and dash()
which in turn make up the Morse letters e.g. Juliet()
Here I spell out my name: 'Jane-Maree'
*/
void loop()
{
Juliet(); //J
Alpha(); //A
November(); //N
Echo(); //E
Hyphen(); //-
Mike(); //M
Alpha(); //A
Romeo(); //R
Echo(); //E
Echo(); //E
delay(7*dLay); // 1.75 sec delay - delay between words
delay(7*dLay); // 1.75 sec delay - delay between words
}//end loop() // Main body of program


/* Now we define the functions dot() & dash()
These represent the 'character elements' of each alpha-numeric character
As can be seen above, calling these functions is quite easy */
void dot()
{
digitalWrite(pin, HIGH);
delay(dLay); // 250 millisec delay - length of 'character element'
digitalWrite(pin, LOW);
delay(2*dLay); // 500 millisec delay - delay between 'character elements'
}//end dot()

void dash()
{
digitalWrite(pin, HIGH);
delay(dLay*3); // 750 millisec delay - length of 'character element'
digitalWrite(pin, LOW);
delay(2*dLay); // 500 millisec delay - delay between 'character elements'
}//end dash()

/* Now we make functions using functions: in this case the Morse alphabet of
various characters; named using the military alpha-word-code - Alpha Bravo etc
*/

//Juliet di-dah-dah-dah
void Juliet()
{
dot();
dash();
dash();
dash();
delay(2*dLay);
}//end juliet()

//Alpha di-dah
void Alpha()
{
dot();
dash();
delay(2*dLay);
}//end Alpha()

//November dah-dit
void November()
{
dash();
dot();
delay(2*dLay);
}//end November()

//Echo dit
void Echo()
{
dot();
delay(2*dLay);
}//end Echo()

//Mike dah-dah
void Mike()
{
dash();
dash();
delay(2*dLay);
}//end Mike()

//Romeo di-dah-dit
void Romeo()
{
dot();
dash();
dot();
delay(2*dLay);
}//end Romeo()

//Hyphen dah-di-di-di-di-dah
void Hyphen()
{
dash();
dot();
dot();
dot();
dot();
dash();
delay(2*dLay);
}//end Hyphen()

/* end of user-defined functions */


3 comments:

  1. In contrast to the SOS program, i've looked up the official code timing intervals between letters, words, etc

    Now i want to hook up a buzzer so i can listen to the code directly - after all, that's what was originally intended: to be listened to!!

    It has a rhythm of its own, which you can latch onto - every Morse operator has their own unique rhythm, called a 'fist'..

    ReplyDelete
  2. ..which i just did by incorporating delays into the functions rather than writing them out separately, as they were rather repetitive.

    Saved ~300 bytes!!

    ReplyDelete