Pages: [1]   Go Down
Author Topic: Your First Arduino Program/Project?  (Read 2827 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I decided to treat myself to an Arduino UNO and wrote my first little program, this morning. Nothing amazing and certainly not optimized but figured other n00bs might like it to play with or use it as an example.

In short, it is a Morse encoder. Currently, it only encodes the alphabet: A-Z. It would not take much effort to add in a whole bunch of other characters.

I personally used an LED for the output since my wife was getting annoyed by the buzzer I originally connected smiley-grin

Anyways, here is the code. I hope someone finds it useful.

Code:
//pin to use as output
int morseLED = 2;

//for converting characters to array index
int offsetInt = 65;

//delays
int dotDelay = 1;
int dashDelay = 3;
int characterDelay = 3;
int ddDelay = 1;
int timeMultiplier = 100;

//array containing morse patterns
String morseAlphabet[] = {
    ".-",      //A
    "-...",    //B
    "-.-.",    //C
    "-..",     //D
    ".",       //E
    "..-.",    //F
    "--.",     //G
    "....",    //H
    "..",      //I
    ".---",    //J
    "-.-",     //K
    ".-..",    //L
    "--",      //M
    "-.",      //N
    "---",     //O
    ".--.",    //P
    "--.-",    //Q
    ".-.",     //R
    "...",     //S
    "-",       //T
    "..-",     //U
    "...-",    //V
    ".--",     //W
    "-..-",    //X
    "-.--",    //Y
    "--.."     //Z
};
 
void setup(){
  pinMode(morseLED,OUTPUT); //set the pin for the led as an output
}

//main morse string encoding function
void EncodeMorseString(String morseString){
 
  //for each character of the input string
  for (int i = 0; i < morseString.length(); i++){
   
    //uppercase it for consistency/array lookup
    char upperChar = toupper(morseString[i]);
   
    //while there are characters to process...
    for (int j = 0; j < morseAlphabet[upperChar-offsetInt].length(); j++){
     
      //find out if it's a dot or a dash
      if(morseAlphabet[upperChar-offsetInt][j] == '.')
      {
        //output a dot
        digitalWrite(morseLED, HIGH);
        delay(dotDelay*timeMultiplier);
        digitalWrite(morseLED, LOW);
        delay(ddDelay*timeMultiplier);
      }
      else if (morseAlphabet[upperChar-offsetInt][j] == '-'){
        //output a dash
        digitalWrite(morseLED, HIGH);
        delay(dashDelay*timeMultiplier);
        digitalWrite(morseLED, LOW);
        delay(ddDelay*timeMultiplier);
      }
    }
    //wait a short while before outputting next character
    delay(characterDelay*timeMultiplier);
  }
}

void loop(){
  EncodeMorseString("SOS"); //output message
  while(true); //loop indefinitely
}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Morse sending is always a good starter project.

A  next step could be writing it without delay() functions and use the millis() and some var to do the timing. - check blink without delay.

Another next step could be a Morse receiving application.
Definitely more challenging but also more rewarding when it works!
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: