Hi Im fairly new to Arduino and stuff but i have to encode and decode morse for an assignment. Basically the input and output both have to be on the serial port. I have managed to encode the text into morse but i have no idea how i would go about decoding morse to text?
I also have a function that detects whether the first character in the string is a morse character or not to be able to use the appropriate function.
#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[] = "--..";
String morseCode;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
while (Serial.available() == 0) {
}
Serial.println("This is your Encoded/Decoded output:");
morseCode = Serial.readString();
printmorse (morseCode);
}
//Detects whether the first character entered is either a morse or an Ascii
void morseorAscii (String s) {
if ((s.charAt(0) == ' ') && ((s.charAt(0) == '^') || (s.charAt(0) == '=') || (s.charAt(0) == '$'))) //If the start of the string has a space and ^ or = or $ then print ASCII
{
// printAscii(s); // Decode the morse by printing Ascii
}
else { // Anything else apart from those values then print morse
printmorse(s); // Encode the Ascii my printing morse
}
}
// Encodes the Ascii text to morse
void printmorse(String s) {
for (int i = 0; i < s.length(); i++) { // goes through each character one at a time
if (s.charAt(i) == ' ') { // if the string then has a space
Serial.print(" "); // then a space will be printed
}
else {
Serial.print(ascii2morse(s*)); // print out the translation from the ascii to morse*
- if (i == s.length() - 1) { // if we have arrived at out last character in the string we don't want anymore slashes*
- }*
- else if (s.charAt(i + 1) == ' ') { // if the next character is a space don't print a slash*
- ;*
- } else {*
- Serial.print('/'); // print the slash to divide each morse*
- }*
- }*
- }*
}
//void printAscii(String s) {
//
// if ((s.charAt(0) == '^') || (s.charAt(0) == '=') || (s.charAt(0) == '$')) {
// Serial.print(morse2ascii(printmorse));
// {
// if
// ((s.charAt(1) == '^') || (s.charAt(1) == '=') || (s.charAt(1) == '$'))
// Serial.print(morse2ascii(printmorse));
// {
// if
// ((s.charAt(2) == '^') || (s.charAt(2) == '=') || (s.charAt(2) == '$'))
// Serial.print(morse2ascii(printmorse));
// {
// if
// ((s.charAt(3) == '^') || (s.charAt(3) == '=') || (s.charAt(3) == '$'))
// Serial.print(morse2ascii(printmorse));
// {
// if
// ((s.charAt(4) == '^') || (s.charAt(4) == '=') || (s.charAt(4) == '$'))
// Serial.print(morse2ascii(printmorse));
// }
// }
// }
// }
// }
//}
// Returns the morse of the Ascii that is entered
char * ascii2morse(char c) { - c = tolower (c); // Lowers the letters to be read by the code*
- switch (c) {*
- case 'a': return morse_a; // If the letter A is entered then return morse code for A*
- case 'b': return morse_b; // If the letter B is entered then return morse code for B*
- case 'c': return morse_c; // etc*
- case 'd': return morse_d;*
- case 'e': return morse_e;*
- case 'f': return morse_f;*
- case 'g': return morse_g;*
- case 'h': return morse_h;*
- case 'i': return morse_i;*
- case 'j': return morse_j;*
- case 'k': return morse_k;*
- case 'l': return morse_l;*
- case 'm': return morse_m;*
- case 'n': return morse_n;*
- case 'o': return morse_o;*
- case 'p': return morse_p;*
- case 'q': return morse_q;*
- case 'r': return morse_r;*
- case 's': return morse_s;*
- case 't': return morse_t;*
- case 'u': return morse_u;*
- case 'v': return morse_v;*
- case 'x': return morse_x;*
- case 'y': return morse_y;*
- case 'z': return morse_z;*
}
}
// Prints each morse code as an Ascii character
void morse2ascii (String Stringmorse){ - if (Stringmorse == morse_a) { // If the morse code of A is entered, print the letter A*
- Serial.print("a");*
- }*
- if (Stringmorse == morse_b) { // If the morse code of B is entered, print the letter B*
- Serial.print("b");*
- }*
- if (Stringmorse == morse_c) { // etc*
- Serial.print("c");*
- }*
- if (Stringmorse == morse_d) {*
- Serial.print("d");*
- }*
- if (Stringmorse == morse_e) {*
- Serial.print("e");*
- }*
- if (Stringmorse == morse_f) {*
- Serial.print("f");*
- }*
- if (Stringmorse == morse_g) {*
- Serial.print("g");*
- }*
- if (Stringmorse == morse_h) {*
- Serial.print("h");*
- }*
- if (Stringmorse == morse_i) {*
- Serial.print("i");*
- }*
- if (Stringmorse == morse_j) {*
- Serial.print("j");*
- }*
- if (Stringmorse == morse_k) {*
- Serial.print("k");*
- }*
- if (Stringmorse == morse_l) {*
- Serial.print("l");*
- }*
- if (Stringmorse == morse_m) {*
- Serial.print("m");*
- }*
- if (Stringmorse == morse_n) {*
- Serial.print("n");*
- }*
- if (Stringmorse == morse_o) {*
- Serial.print("o");*
- }*
- if (Stringmorse == morse_p) {*
- Serial.print("p");*
- }*
- if (Stringmorse == morse_q) {*
- Serial.print("q");*
- }*
- if (Stringmorse == morse_r) {*
- Serial.print("r");*
- }*
- if (Stringmorse == morse_s) {*
- Serial.print("s");*
- }*
- if (Stringmorse == morse_t) {*
- Serial.print("t");*
- }*
- if (Stringmorse == morse_u) {*
- Serial.print("u");*
- }*
- if (Stringmorse == morse_v) {*
- Serial.print("v");*
- }*
- if (Stringmorse == morse_w) {*
- Serial.print("w");*
- }*
- if (Stringmorse == morse_x) {*
- Serial.print("x");*
- }*
- if (Stringmorse == morse_y) {*
- Serial.print("y");*
- }*
- if (Stringmorse == morse_z) {*
- Serial.print("z");*
- }*
}
void loop() { - // put your main code here, to run repeatedly:*
}