18 March 2010

Using Arduino software - user-defined functions

This little program is found in the tutorial on FUNCTIONS.
I've titivated it up a bit

/*
* Project: functions_Morse Code - SOS
* Author: Jane-Maree Howard
* Date: Thursday 18/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:
* Invented by Samuel Morse (back in the day!), this telegraph code was later used for
* wireless (radio) telegraphy in conditions where radio-telephony (speech) was unclear
* owing to weak signals, radio-interference etc. An operator with a good 'fist' could
* transmit 40-50 words per minute (or possibly more). 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.
* Here 'dit-dit-dit dah-dah-dah dit-dit-dit' encodes the international distress call 'SOS'
*/
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() */
void loop()
{
dot(); dot(); dot(); // Morse code: . . . = S
delay(2*dLay); // delay between characters
dash(); dash(); dash(); // Morse code: - - - = O
delay(2*dLay);
dot(); dot(); dot(); // Morse code: . . . = S
delay(2*dLay);
delay(12*dLay); // 3 seconds
}//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(dLay); // 250 millisec delay - delay between 'character elements'
}//end dot()

void dash()
{
digitalWrite(pin, HIGH);
delay(dLay*4); // 1 second delay - length of 'character element'
digitalWrite(pin, LOW);
delay(dLay); // 250 millisec delay - delay between 'character elements'
}//end dash()
/* end of user-defined functions */

This is taken directly (with my own additions) from 'Arduino - LibraryTutorial'

2 comments:

  1. Well, that was easy!!
    Now to make more functions..

    ReplyDelete
  2. What i want to find out now is
    - how to make functions that take PARAMETERS

    THAT would be very useful..

    ReplyDelete