Go Down

Topic: Your First Arduino Program/Project? (Read 2 times) previous topic - next topic

gastrogeek

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 :D

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

Code: [Select]

//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
}

robtillaart

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!
Rob Tillaart

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

Go Up