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;*
- strcat(morsecat, temp);*
- //Serial.println(morsecat);*
_ } 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)