Arduino Morse Project Troubles

Hi there,

I'm relatively new to programming in general, I just started learning how to program on the arduino and I've ran into some troubles regarding a morse encoder/decoder project I've been working on.

Essentially, I've already been able to figure out how one might encode a string of characters into morse, but I'm now struggling to figure out how one might then decode morse back into characters. This is because the use of switch statements in my encoder relied on taking a single character, whereas morse is comprised of multiple characters.

Here is my code relative to the encoder:

void printMorse (String s) {
for ( int i = 0; i < s.length(); i++) {
String tempVar = char2morse(s.charAt(i));
Serial.print(tempVar);
if(addSlash(s, i)){
Serial.print("/");
}
}
}

(addSlash is the code I use to determine where "/" should go to differentiate separate characters within the morse, whilst char2morse is code that uses switch statements to replace the characters with the correct morse translation.)

I apologise in advance if the answer is glaringly obvious, I'm still fairly new to programming and would appreciate any advice you could give me :slight_smile:

Thanks in advance!

whereas morse is comprised of multiple characters.

But you are putting a separator between the bits representing each character, so you know where a character ends. Decoding is a simple matter of collecting the bits (as characters . and -) that represent a character, stopping when you find a slash, and then performing a match against the patterns for known characters.

I get what you mean, but the issue I find is how I would then put that into code. For example, in 'printMorse' it goes through and translates singular characters, whereas what I am now trying to do, like you said, is take multiple characters and switch that for a singular character. Just realised that I didn't post the whole script as I should have done, so I'll do that now, sorry.

// Here is where I create the library of morse code
char morse_a[] = ".-";
char morse_b[] = "-...";
char morse_c[] = "-.-.";
char morse_d[] = "-..";
char morse_e[] = ".";
char morse_f[] = "..-.";
char morse_g[] = "--.";
char morse_h[] = "....";
char morse_i[] = "..";
char morse_j[] = ".---";
char morse_k[] = "-.-";
char morse_l[] = ".-..";
char morse_m[] = "--";
char morse_n[] = "-.";
char morse_o[] = "---";
char morse_p[] = ".--.";
char morse_q[] = "--.-";
char morse_r[] = ".-.";
char morse_s[] = "...";
char morse_t[] = "-";
char morse_u[] = "..-";
char morse_v[] = "...-";
char morse_w[] = ".--";
char morse_x[] = "-..-";
char morse_y[] = "-.--";
char morse_z[] = "--..";

// Here is where I convert regular ASCII characters into morse code
char * char2morse(char c) {
  switch (c) {
    case 'a':
      return morse_a;
      break;

    case 'b':
      return morse_b;
      break;

    case 'c':
      return morse_c;
      break;

    case 'd':
      return morse_d;
      break;

    case 'e':
      return morse_e;
      break;

    case 'f':
      return morse_f;
      break;

    case 'g':
      return morse_g;
      break;

    case 'h':
      return morse_h;
      break;

    case 'i':
      return morse_i;
      break;

    case 'j':
      return morse_j;
      break;

    case 'k':
      return morse_k;
      break;

    case 'l':
      return morse_l;
      break;

    case 'm':
      return morse_m;
      break;

    case 'n':
      return morse_n;
      break;

    case 'o':
      return morse_o;
      break;

    case 'p':
      return morse_p;
      break;

    case 'q':
      return morse_q;
      break;

    case 'r':
      return morse_r;
      break;

    case 's':
      return morse_s;
      break;

    case 't':
      return morse_t;
      break;

    case 'u':
      return morse_u;
      break;

    case 'v':
      return morse_v;
      break;

    case 'w':
      return morse_w;
      break;

    case 'x':
      return morse_x;
      break;

    case 'y':
      return morse_y;
      break;

    case 'z':
      return morse_z;
      break;

    case ' ':
      return " ";
    default:
      return "#";
  }
}
//Here is where I convert morse code into regular ASCII characters
char morse2char(String m) {
  if (m.equals(morse_a)) {
    return 'a';
  }
  else if (m.equals(morse_b)) {
    return 'b';
  }
  else if (m.equals(morse_c)) {
    return 'c';
  }
  else if (m.equals(morse_d)) {
    return 'd';
  }
  else if (m.equals(morse_e)) {
    return 'e';
  }
  else if (m.equals(morse_f)) {
    return 'f';
  }
  else if (m.equals(morse_g)) {
    return 'g';
  }
  else if (m.equals(morse_h)) {
    return 'h';
  }
  else if (m.equals(morse_i)) {
    return 'i';
  }
  else if (m.equals(morse_j)) {
    return 'j';
  }
  else if (m.equals(morse_k)) {
    return 'k';
  }
  else if (m.equals(morse_l)) {
    return 'l';
  }
  else if (m.equals(morse_m)) {
    return 'm';
  }
  else if (m.equals(morse_o)) {
    return 'o';
  }
  else if (m.equals(morse_p)) {
    return 'p';
  }
  else if (m.equals(morse_q)) {
    return 'q';
  }
  else if (m.equals(morse_r)) {
    return 'r';
  }
  else if (m.equals(morse_s)) {
    return 's';
  }
  else if (m.equals(morse_t)) {
    return 't';
  }
  else if (m.equals(morse_u)) {
    return 'u';
  }
  else if (m.equals(morse_v)) {
    return 'v';
  }
  else if (m.equals(morse_w)) {
    return 'w';
  }
  else if (m.equals(morse_x)) {
    return 'x';
  }
  else if (m.equals(morse_y)) {
    return 'y';
  }
  else if (m.equals(morse_z)) {
    return 'z';
  }
  else if (m.equals(" ")) {
    return ' ';
  }
  else {
    return '#';
  }
}

/////////////////////////////////////

void setup() {
  Serial.begin(9600);

  char t [] = "--/---/.-._=/.../. -.-./---/-../.";
  String testString = "morse code";
  testString.toLowerCase();
  printMorse(testString);  

}


// Here is where slashes are put into returned morse where appropriate
bool addSlash (String s, int pos) {
  bool retVal = true;
  if (pos == s.length() -1 || s.charAt(pos+1) == ' ' || s.charAt(pos) == ' '){//check next char space or end of string
    retVal = false;
  }
  if(pos < 0){//check previous char space
    retVal = false;
  }
  return retVal;
}

// Here is where I make the code able to translate a string into morse
void printMorse (String s) {
  for ( int i = 0; i < s.length(); i++) {
   String tempVar = char2morse(s.charAt(i));
   Serial.print(tempVar);
   if(addSlash(s, i)){
     Serial.print("/");
   }
  }
}


void loop() {
}
// put your main code here, to run repeatedly:

Could you perhaps give me an example as to what the code should look like for the translation of morse back into characters, in the context of my current code, or give me some advice as to where I should start looking to learn how to do this? Thanks in advance :slight_smile: