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[] = "--..";
void setup() {
Serial.begin(9600);
String s;
char t[] = ".";
char u[] = "-";
char v[] = ".-/-... -.-./-..";
char w[] = "--/---/.-._=/.../. -.-./---/-../.";
char x[] = ".- -.../---/.--/.-.. ---/..-. .--././-/..-/-./../.-/...";
char y[] = "--/---/.-./.../......... -.-./---/-../.";
char z[] = "--/---/.-./.../. -.-./---/-../..........";
s = morse2ascii(t); Serial.println (s);
s = morse2ascii(u); Serial.println (s);
s = morse2ascii(v); Serial.println (s);
s = morse2ascii(w); Serial.println (s);
s = morse2ascii(x); Serial.println (s);
s = morse2ascii(y); Serial.println (s);
s = morse2ascii(z); Serial.println (s);
}
char morse2char (String s) {
if (s.equals(morse_a)) {
return 'a';
}
else if (s.equals(morse_b)) {
return 'b';
}
else if (s.equals(morse_c)) {
return 'c';
}
else if (s.equals(morse_b)) {
return 'd';
}
else if (s.equals(morse_e)) {
return 'e';
}
else if (s.equals(morse_f)) {
return 'f';
}
else if (s.equals(morse_g)) {
return 'g';
}
else if (s.equals(morse_h)) {
return 'h';
}
else if (s.equals(morse_i)) {
return 'i';
}
else if (s.equals(morse_j)) {
return 'j';
}
else if (s.equals(morse_k)) {
return 'k';
}
else if (s.equals(morse_l)) {
return 'l';
}
else if (s.equals(morse_m)) {
return 'm';
}
else if (s.equals(morse_n)) {
return 'n';
}
else if (s.equals(morse_o)) {
return 'o';
}
else if (s.equals(morse_p)) {
return 'p';
}
else if (s.equals(morse_q)) {
return 'q';
}
else if (s.equals(morse_r)) {
return 'r';
}
else if (s.equals(morse_s)) {
return 's';
}
else if (s.equals(morse_t)) {
return 't';
}
else if (s.equals(morse_u)) {
return 'u';
}
else if (s.equals(morse_v)) {
return 'v';
}
else if (s.equals(morse_w)) {
return 'w';
}
else if (s.equals(morse_x)) {
return 'x';
}
else if (s.equals(morse_y)) {
return 'y';
}
else if (s.equals(morse_z)) {
return 'z';
}
else {
return '#';
}
}
String morse2ascii (String m) {
String next = "";
String result = "";
for (int i = 0; i < m.length(); i++) {
if (m[i] == ".") {
next += m[i];
}
else (m[i] == "-"); {
next += m[i];
}
if (m[i] == "/") {
result += morse2char (next);
next = "";
}
else (m[i] == " "); {
result += morse2char (next);
result += ' ';
next = "";
}
return result += morse2char (next);
}
}
void loop() {
}
This is hard for me to explain so please be patient with me and I'll try my best.
My goal here is to get the program to print out a string of Morse into characters e.g
" char x[] = ".- -.../---/.--/.-.. ---/..-. .--././-/..-/-./../.-/..."; "
So this should print : "A BOWL OF PETUNIAS"
A space symbols a new word
A "/" symbols a new character
As you can see I've already wrote the code to change the Morse into characters
"char morse2char (String s)"
and now I'm trying to get the program to take each character and build them into a string using
"String morse2ascii (String m)"
So as best as I can explain it
If its a "."
hold the character and move onto next
if its a "-"
hold the character and move onto next
if its a "/"
Store previous characters in String next = "";
if its a " "
Store previous characters in String next = "";
if no more characters return the string
but it's only printing the first Morse character
String morse2ascii (String m) {
String next = "";
String result = "";
for (int i = 0; i < m.length(); i++) {
if (m[i] == ".") {
next += m[i];
}
else (m[i] == "-"); {
next += m[i];
}
if (m[i] == "/") {
result += morse2char (next);
next = "";
}
else (m[i] == " "); {
result += morse2char (next);
result += ' ';
next = "";
}
return result += morse2char (next);
}
}
It's this piece of code here that is causing the problems.
Can you see any obvious mistakes I've made?
I'm sorry if this makes no sense, please do ask if something needs clearing up more.