converting a morse string to an array of chars...

So as the title states the purpose of this program is to take in a string of morse, for example .-/.-/.-, into an array of characters. I believe I am pretty much there however I need to be able to set morsecat to be empty as I believe why the program doesn't run correctly is because morsecat is continually recieving characters and, when it comes to converting what is held within morsecat into a character, it sees that morsecat is far too long and cannot convert it as it doesnt match any morse character because it contains two or more morse characters instead of one (which is why the do while loop is there as "/" separates each morse character).
I have attached my code but please feel free to overhaul it if you think thats what it needs.
Any help would be greatly appreciated as this has had me stumped for a little while now.

Note: I am aware that when you try to verify the code it gives the error "empty character constant", as I am not aware of how to reset the value of morsecat so that it contains nothing.

Not only have I attached my code but I have also pasted it below:

char * ip = ".-/.-/";
char buf[50];
int buflen;

char morse2char (char * m) {
if (strcmp(m, ".-") == 0) {
Serial.println("a");
}
else if (strcmp(m, "-...") == 0) {
Serial.println("b");
}
else if (strcmp(m, "-.-.") == 0) {
Serial.println("c");
}
else if (strcmp(m, "-..") == 0) {
Serial.println("d");
}
else if (strcmp(m, ".") == 0) {
Serial.println("e");
}
else if (strcmp(m, "..-.") == 0) {
Serial.println("f");
}
else if (strcmp(m, "--.") == 0) {
Serial.println("g");
}
else if (strcmp(m, "....") == 0) {
Serial.println("h");
}
else if (strcmp(m, "..") == 0) {
Serial.println("i");
}
else if (strcmp(m, ".---") == 0) {
Serial.println("j");
}
else if (strcmp(m, "-.-") == 0) {
Serial.println("k");
}
else if (strcmp(m, ".-..") == 0) {
Serial.println("l");
}
else if (strcmp(m, "--") == 0) {
Serial.println("m");
}
else if (strcmp(m, "-.") == 0) {
Serial.println("n");
}
else if (strcmp(m, "---") == 0) {
Serial.println("o");
}
else if (strcmp(m, ".--.") == 0) {
Serial.println("p");
}
else if (strcmp(m, "--.-") == 0) {
Serial.println("q");
}
else if (strcmp(m, ".-.") == 0) {
Serial.println("r");
}
else if (strcmp(m, "...") == 0) {
Serial.println("s");
}
else if (strcmp(m, "-") == 0) {
Serial.println("t");
}
else if (strcmp(m, "..-") == 0) {
Serial.println("u");
}
else if (strcmp(m, "...-") == 0) {
Serial.println("v");
}
else if (strcmp(m, ".--") == 0) {
Serial.println("w");
}
else if (strcmp(m, "-..-") == 0) {
Serial.println("x");
}
else if (strcmp(m, "-.--") == 0) {
Serial.println("y");
}
else if (strcmp(m, "--..") == 0) {
Serial.println("z");
}
else if (strcmp(m, "/") == 0) {
Serial.println(" ");
}
}
char * charstring2morsestring(char * ip, char * buf, int buflen){
char backslash = '/';
char * currentchar;
int i;
char morsecat[buflen];
//morsecat[0] = '\0';
char * temp;
for(i=0;i<strlen(ip);i++){

do {

temp = ip;*

_ } while (ip != '/');_

_ if (ip == '/') {
currentchar = morse2char(morsecat);
strcat(buf, currentchar);
morsecat[buflen] = ' ';
}*_

* }*

*Serial.println(buf); *
* }*

void setup(){
* Serial.begin(9600);*
* //Serial.println(morse2char(".-"));*
* //Serial.println(morse2char("-..."));*
* //Serial.println(morse2char("--.."));*
* charstring2morsestring(".-/-.../--../.", buf, 50);*

}
void loop(){

}
Morse_to_char.ino (2.58 KB)

strcat(buf, currentchar);
    morsecat[buflen] = ' ';

When the character is added to buf, buf is NULL terminated. It does not make sense to add a space somewhere in the array after the NULL.

do {
     
    *temp = ip;
    strcat(morsecat, temp);
    //Serial.println(morsecat);
   
   
    } while (ip != '/');

This doesn't make sense. The code that calls the function that this is in should terminate the array with a NULL, not a '/'. Then, there is no need for this nonsense.

Not only have I attached my code but I have also pasted it below:

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

How to use this forum

All these strcmps look a bit unnecessary to me. How about an array or two?

char * letters [26] = {
   ".-",     // 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
};

char * numbers [10] = 
  {
  "-----",  // 0
  ".----",  // 1
  "..---",  // 2
  "...--",  // 3
  "....-",  // 4
  ".....",  // 5
  "-....",  // 6
  "--...",  // 7
  "---..",  // 8
  "----.",  // 9
  };
  
// get a line from serial
void getline (char * buf, size_t bufsize)
{
byte i;
  
  // discard any old junk
  while (Serial.available ())
    Serial.read ();
    
  for (i = 0; i < bufsize - 1; )
    {
    if (Serial.available ())
      {
      int c = Serial.read ();
      
      if (c == '\n')  // newline terminates
        break;
      
      if (!isspace (c))  // ignore spaces, carriage-return etc.
        buf [i++] = c;
      } // end if available
    }  // end of for
  buf [i] = 0;  // terminator
  Serial.println (buf);  // echo what they typed
  }     // end of getline
       
         
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  }  // end of setup

void loop ()
  {
  char userInput [15];
  Serial.println ("Enter your Morse code: ");
  getline (userInput, sizeof userInput);
  char i;
  
  for (i = 'A'; i <= 'Z'; i++)
    if (strcmp (letters [i - 'A'], userInput) == 0)
      {
      Serial.print ("Found letter: ");
      Serial.println (i);
      return;
      }
      
  for (i = '0'; i <= '9'; i++)
    if (strcmp (numbers [i - '0'], userInput) == 0)
      {
      Serial.print ("Found number: ");
      Serial.println (i);
      return;
      }
  
  Serial.println ("Unrecognized code.");
  }  // end of loop

char t[64]= '%TEMNAIOGKDWRUS-.QZYCXBJP%L-FVH '
c=next()
i=0
while c != ' ' 
    if c == '-' 
         i=2*i+1
    else 
         i=2*i+2
    c=next()

Nice.

@Nick: What happens to your code if the incoming input is lower case? Doesn't this need a toupper() call somewhere? Also, if you want to see a nice implementation of the binary search approach to Morse code, look at:

http://raronoff.wordpress.com/2010/12/16/morse-endecoder/

W8TEE

xitami:

char t[64]= '%TEMNAIOGKDWRUS-.QZYCXBJP%L-FVH '

c=next()
i=0
while c != ' '
if c == '-'
i=2i+1
else
i=2
i+2
c=next()

I like this method. I take it you have experience with compression algorithms. (huffmann tables?)

econjack:
@Nick: What happens to your code if the incoming input is lower case? Doesn't this need a toupper() call somewhere?

This decodes morse, so it only looks at dots and dashes. My encoder ( Morse Code Translator - #7 by nickgammon - Project Guidance - Arduino Forum ) has a toupper in it.

Opps...I got the next() in my head the wrong way!