Receiving and Cross-checking Morse code

Hi everyone…

I am a mechanical engineering student who has taken up a project involving arduino and i need help…!!

What i am working on requires to receive Morse code (using Photodiode take analogue input), process that input to get the words that are transmitted. Once the words are processed, cross verify with a given set of words.

The processing part is not necessary, but i want to receive the input and verify.

i was able to write down a code for transmitting the morse, but have a lot of trouble receiving and cross-checking the code.

i would really appreciate the help.

Thank You.

Final_morse_code.ino (7.27 KB)

You say you had a lot of trouble coding the receive part? Where is it? You only posted transmit code. It came from this site. Do you want us to assume that you wrote it? Since you claim to have written your own code, please post it, or you will appear to be attempting to deceive us.

Thanks

Here is the code

/*
  Morse Code Transmit and Receive
  
  This code will loop through a string of characters and convert these to morse code.  
  It will blink two LED lights and play audio on a speaker.  
 */
 
 
//**************************************************//
//   Type the String to Convert to Morse Code Here  //
//**************************************************//
char stringToMorseCode[] = "Arduino Transmit and Receive";

// Create variable to define the output pins
int led12 = 12;      // blink an led on output 12
int led6 = 6;        // blink an led on output 6
int audio8 = 8;      // output audio on pin 8
int note = 1200;      // music note/pitch

/*
  Set the speed of your morse code
  Adjust the 'dotlen' length to speed up or slow down your morse code
    (all of the other lengths are based on the dotlen)

  Here are the ratios code elements:
    Dash length = Dot length x 3
    Pause between elements = Dot length
      (pause between dots and dashes within the character)
    Pause between characters = Dot length x 3
    Pause between words = Dot length x 7
  
*/
int dotLen = 50;     // length of the morse code 'dot'
int dashLen = dotLen * 3;    // length of the morse code 'dash'
int elemPause = dotLen;  // length of the pause between elements of a character
int Spaces = dotLen * 3;     // length of the spaces between characters
int wordPause = dotLen * 7;  // length of the pause between words

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output for LED lights.
  pinMode(led12, OUTPUT); 
  pinMode(led6, OUTPUT); 
}

// Create a loop of the letters/words you want to output in morse code (defined in string at top of code)
void loop()
{ 
  // Loop through the string and get each character one at a time until the end is reached
  for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
  {
    // Get the character in the current position
  char tmpChar = stringToMorseCode[i];
  // Set the case to lower case
  tmpChar = toLowerCase(tmpChar);
  // Call the subroutine to get the morse code equivalent for this character
  GetChar(tmpChar);
  }
  
  // At the end of the string long pause before looping and starting again
  LightsOff(8000);      
}

// DOT
void MorseDot()
{
  digitalWrite(led12, HIGH);    // turn the LED on 
  digitalWrite(led6, HIGH); 
  tone(audio8, note, dotLen); // start playing a tone
  delay(dotLen);              // hold in this position
}

// DASH
void MorseDash()
{
  digitalWrite(led12, HIGH);    // turn the LED on 
  digitalWrite(led6, HIGH);
  tone(audio8, note, dashLen);  // start playing a tone
  delay(dashLen);               // hold in this position
}

// Turn Off
void LightsOff(int delayTime)
{
  digitalWrite(led12, LOW);     // turn the LED off   
  digitalWrite(led6, LOW);
  noTone(audio8);             // stop playing a tone
  delay(delayTime);             // hold in this position
}

// *** Characters to Morse Code Conversion *** //
void GetChar(char tmpChar)
{
  // Take the passed character and use a switch case to find the morse code for that character
  switch (tmpChar) {
    case 'a': 
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'b':
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'c':
      MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'd':
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'e':
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'f':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'g':
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'h':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'i':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'j':
      MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
      case 'k':
      MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'l':
      MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
      case 'm':
      MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'n':
      MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'o':
      MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'p':
      MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 'q':
      MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'r':
      MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 's':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    case 't':
      MorseDash();
    LightsOff(elemPause);
    break;
    case 'u':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'v':
      MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'w':
      MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'x':
      MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'y':
      MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    break;
    case 'z':
      MorseDash();
    LightsOff(elemPause);
    MorseDash();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    MorseDot();
    LightsOff(elemPause);
    break;
    default: 
    // If a matching character was not found it will default to a blank space
    LightsOff(Spaces);      
  }
}

Sorry about that…

Thanks for the code tags. Now, it seems that you haven't written a single line of code so far... Once again, here is the exact link..https://github.com/dsigma/hamio/blob/master/hamio/morse.cpp

Edit - okay, it appears that you have changed a few lines. But no significant work to it. There is also no code to receive.

Yes…

the code that i have posted is for transmitting the morse…

i still dont know how to receive the morse and cross check the information…

But you said, "i was able to write down a code for transmitting the morse". I think that needs some clarification. Is that what you will tell your professors?

that is not my code...obviously..

i am a mechanical engineer..i cannot do proper coding..

i have made some changes to the original code.. i do not have any intention to deceive anyone, i just want help with my project...i am sorry if i have caused any kind of misunderstanding..

saurabhp: i am sorry if i have caused any kind of misunderstanding..

I'm only responding to the words that you used. The really obvious way to ethically use other peoples code when it is permitted, is to include a proper attribution in the code itself, as a comment line. I always do this. You did not.

Never the less, it seems that you have not made any attempt to begin the receive part. It is difficult to help in those circumstances.

Can you not begin to develop a concept of how it might be done?

my professors know that i will not be doing the coding..

i have chosen the arduino platform for that soul reason...so that i can receive help from others with the coding..

and my final project does not just involve the transmitting and receiving of morse code.

and i believe the code for sending morse will be same for anyone..

and about the receiving part..what i was planning to use was to use if & else commands for receiving the dots and dash.(i.e. if a dot and dash is received with a gap of 3 units=a and so on).

but i just cannot sketch it.. thats why i was asking for help..

if some one could give me some clue as to how i can approach the problem..

saurabhp:
but i just cannot sketch it…

You started to do it, above. Just keep going. You’re an engineering student. It’s what they do. Don’t think about if’s or else’s or programming. Just define the problem first. Define it completely.

When you’re done with that, look up “state machine”.

so…

while writing the code…i have defined a dot=1 unit dash=3* dot and a also define each letter with the respective dots and dash’s…

i give a header for the arduino to understand when to start reading…

the input is received using the analogue pin…

after identifying the letter being received, how do i cross check it with a given string of letters…?

What do you mean by "cross check"?

saurabhp: Hi everyone..

I am a mechanical engineering student who has taken up a project involving arduino and i need help..!!

What i am working on requires to receive Morse code (using Photodiode take analogue input), process that input to get the words that are transmitted. Once the words are processed, cross verify with a given set of words.

The processing part is not necessary, but i want to receive the input and verify.

i was able to write down a code for transmitting the morse, but have a lot of trouble receiving and cross-checking the code.

i would really appreciate the help.

Thank You.

The first challenge is to determine if the code you will receive is machine sent or human sent. In either case the first step is to determine the length of time for both a "dot" and a "dash". From that you can determine the length, in time, for a "space" between a dot and a dash, and the correct time for a space between words.

If your code is machine or computer sent, the times for the elements will be consistent. If the code is human sent, then all the times will be approximates. Computer sent code will be the easiest for you to program for.

Can you write a program to determine the time, in milliseconds, for each code part?

Paul

jremington: What do you mean by "cross check"?

By cross check i mean...i have already defined a word to be received.

if that word i received, only they switch on the LED..or something like that...

Paul_KD7HB: The first challenge is to determine if the code you will receive is machine sent or human sent. In either case the first step is to determine the length of time for both a "dot" and a "dash". From that you can determine the length, in time, for a "space" between a dot and a dash, and the correct time for a space between words.

If your code is machine or computer sent, the times for the elements will be consistent. If the code is human sent, then all the times will be approximates. Computer sent code will be the easiest for you to program for.

Can you write a program to determine the time, in milliseconds, for each code part?

Paul

and the code is pre-programmed in a different arduino, and another arduino is suppose to receive it... i found a code to receive the morse and convert it to english...but i dont know how am i suppose to cross check it... I understand i need to use array function here...but i dont know how..

please help

saurabhp: i found a code to receive the morse and convert it to english...but i dont know how am i suppose to cross check it... I understand i need to use array function here...but i dont know how..

Hint: Google "c++ strcmp()"

saurabhp: my professors know that i will not be doing the coding..

.....

but i just cannot sketch it.. thats why i was asking for help..

As an ex professor I am puzzled as to what your project actually is if it is not to write the code. Is it just gathering bits and pieces you find in the net? It seems to me that there is very little intellectual content in this project.

To check a word is one you know you comparing it to an array of known words one at a time. Should be about three lines of code. Wow education these days.

OldSteve:
Hint: Google “c++ strcmp()”

Thanks for that…

here is the code i am using…i found it here : Decoding Morse Code – Marginally Clever Robots

#define THRESHOLD    (600)
#define MAX_SAMPLES  (5)

#define BAUD         (100.0)
#define WAIT         (5.0)
#define AVG_LONG     (BAUD*3.0/WAIT)
#define AVG_SHORT    (BAUD*1.0/WAIT)
#define AVG_NEWWORD  (BAUD*7.0/WAIT)
#define MINIMUM      (AVG_SHORT/4.0)

#define MAX_PATTERN  (64)

#define NUM_CODES    (54)

//                            0        10        20        30        40        50
//                            0123456789012345678901234567890123456789012345678901234
static const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?'!/()&:;=+-_\"$@";

// 1 represents a dot and 0 represents a dash.

static const char *codes[NUM_CODES] = {
  "10",      // A, codes[0]
  "0111",    // B, codes[1]
  "0101",    // C
  "011",     // D
  "1",       // E
  "1101",    // F
  "001",     // G
  "1111",    // H
  "11",      // I
  "1000",    // J
  "010",     // K
  "1011",    // L
  "00",      // M
  "01",      // N
  "000",     // O
  "1001",    // P
  "0010",    // Q
  "101",     // R
  "111",     // S
  "0",       // T
  "110",     // U
  "1110",    // V
  "100",     // w
  "0110",    // x
  "0100",    // y
  "0011",    // z
  "00000",   // 0
  "10000",   // 1
  "11000",   // 2
  "11100",   // 3
  "11110",   // 4
  "11111",   // 5
  "01111",   // 6
  "00111",   // 7
  "00011",   // 8
  "00001",   // 9
  "101010",  // .
  "001100",  // ,
  "110011",  // ?
  "100001",  // '
  "010100",  // !
  "01101",   // /
  "01001",   // (
  "010010",  // )
  "10111",   // &
  "000111",  // :
  "010101",  // ;
  "01110",   // =
  "10101",   // +
  "01110",   // -
  "110010",  // _
  "101101",  // "
  "1110110", // $
  "100101",  // @, codes[54]
};


int top=0;

int samples[MAX_SAMPLES];
int si=0;
int mi=0;
int total=0;

int c=0;
int is_on=0;


char pattern[MAX_PATTERN];
int pi=0;


void setup() {
  Serial.begin(9600);
  
  for(int i=0;i<MAX_SAMPLES;++i) {
    samples[i]=0;
  }
  for(int i=0;i<MAX_PATTERN;++i) {
    pattern[i]=0;
  }
}


void loop() {
  int volume=analogRead(0);
  
  total -= samples[si];
  samples[si] = volume;
  total += samples[si];
  if( mi < MAX_SAMPLES ) mi++;
  si = (si+1) % MAX_SAMPLES;
  int average = total / mi;
  
  if( top < average ) top = average;
  
  
  int x = 10.0 * (float)(average-THRESHOLD)/(float)(top-THRESHOLD);
  if(x<0) x=0;
  if(x>10) x=10;
  
  if(x>1) {
    // noise!
    if(is_on==0) {
      // noise has just started.
      if( c > MINIMUM ) {
        
        // Was the silence a new word or a new letter?
        if( c > (AVG_NEWWORD+AVG_SHORT)/2.0 ) {
          pattern[pi]=0;
          findLetter();
          // new word, extra \n
          Serial.println();
          // start counting - and . all over again.
          pi=0;
        } else if( c > (AVG_LONG+AVG_SHORT)/2.0 ) {
          pattern[pi]=0;
          findLetter();
          // start counting - and . all over again.
          pi=0;
        }

      }
      // remember noise started
      is_on=1;
      c=0;
    }
  } else {
    // silence!
    if(is_on==1) {
      // silence is new
      if( c > MINIMUM ) {
        
        if( c > (AVG_LONG + AVG_SHORT)/2.0 ) {
          Serial.print('-');
          pattern[pi++]='0';
        } else {
          Serial.print('.');
          pattern[pi++]='1';
        }
      }
      // remember silence started
      is_on=0;
      c=0;
    }
  }

  c++;
  
  delay(WAIT);
}



// saved as 1s and 0s.  Find the matching code in the list
// then find the matching printable character.
// print '?' if nothing is found
void findLetter() {
  int i,j;
  
  // go through all the codes  
  for(i=0;i<NUM_CODES;i++) {
    
    if(strlen(pattern) == strlen(codes[i]) && strcmp(pattern,codes[i])==0) {
      // match!
      Serial.print(' ');
      Serial.println(letters[i]);
      return;
    }
  }
  Serial.print('?');
}

this code works best for me to receive the morse.
i receive it one letter at a time…i need to compare an multiple letters with pre-defined sequence of letters…

is there a better way to do that…?

Grumpy_Mike:
As an ex professor I am puzzled as to what your project actually is if it is not to write the code. Is it just gathering bits and pieces you find in the net? It seems to me that there is very little intellectual content in this project.

To check a word is one you know you comparing it to an array of known words one at a time. Should be about three lines of code. Wow education these days.

and about my project…
i am to model a mechanism that performs a specific action after the dedicated morse is received…
its not my project to write the code…my project is to model…i am collecting bits and pieces of information about coding because i never studied it…i am trying my best to understand it…

this code works best for me to receive the morse........is there a better way to do that...?

If that code works best, then no, there mustn't be a better way.

If you're modeling a process that only operates on the output from a Morse decoder, you could just feed it a text file. Really, you wouldn't have to worry about how it's decoded.

OldSteve: If that code works best, then no, there mustn't be a better way.

but with that code i am only able to receive the morse...i cant process/cross check the received morse...

help me here....what i was thinking of doing was, after receiving the morse, i will save those received letters in an array. then i just have to compare one array with the pre-programmed array.

can i actually save variables into an array..?

aarg: If you're modeling a process that only operates on the output from a Morse decoder, you could just feed it a text file. Really, you wouldn't have to worry about how it's decoded.

can you give me a idea how to do that...?