I have a functioning Morse code translator, that me an some colleagues put together. It works great, but you have to input the text into the sketch through a function.
Now we are trying to be able to input text through the serial monitor, then get it to translate.
In the original one we used strings for the dots and dashes. Ill just post the sketch, so you can get a look at it.
Im looking for a little advice on whether I should just start from scratch. or if it is possible to make some slight changes to the original.
And what changes I could consider trying. Thanks
This is the functional sketch:
/*
Convert Morse code from a character string
*/
#define debug true
int ledPin = 13;
int speakerPin = 9;
int pitch1 = 140; //tone frequency
int pitch2 = 140;
int dot = 50; // 250mS length of one dot
int dash = 3 * dot; // Dash is equal to 3 dots in duration
// Morse code for Alphabet
String morseATable[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", // A-G
"....", "..", ".---", "_._", ".-..", "--", "-.", // H-N
"---", ".--.", "--.-", ".-.", "...", "-", "..-", // O-U
"...-", ".--", "-..-", "-.--", "--.." // V-Z
};
// Morse code for Numbers
String morseNTable[] = { "-----", ".----", "..---", "...--", "....-", // 0-4
".....", "-....", "--...", "---..", "----." // 5-9
};
void setup()
{
pinMode(ledPin, OUTPUT); // set ledPin as OUTPUT
pinMode(speakerPin, OUTPUT); // set speakerPin to POUTPUT
Serial.begin(9600); // Initialze Serial Monitor
if (debug)
Serial.println("\nBegin of Morse Code"); // "\n" produces a "newline" character
convert2Morse("this is what it translates"); // String to be converted
}
void loop ()
{
}
/*
Description: Converts characters in a string to the Morse code sequence
Argument: String to be converted
Returns: nul
Calls: morseLED
*/
void convert2Morse(String message)
{
for (int pos = 0; pos < message.length(); pos++)
{
if (debug)
{
Serial.print(message.charAt(pos));
Serial.print(" ");
}
if ((message.charAt(pos) >= 97) && (message.charAt(pos) <= 122)) // Check for a lower case character
morseLED(morseATable[message.charAt(pos)-97]);
if ((message.charAt(pos) >= 48) && (message.charAt(pos) <= 57)) // Check for a number
morseLED(morseNTable[message[pos]-48]);
if (message.charAt(pos) == 32) // Check for a space
morseLED(" "); // A space is equal to seven dots
if (debug)
Serial.println();
}
}
/*
Description: Passes individual dots and dashes (and Spaces) to flashLED handler
Argument: Morse Code sequence
Returns: nul
Calls: flashLED
*/
void morseLED(String sequence)
{
for (int pos = 0; pos < sequence.length(); pos++)
flashLED(sequence.charAt(pos));
delay(2 * dot); // Space between two letters is equal to three dots (one dash)
}
/*
Description: Produces a dot, dash or space flash on the LED
Argument: Individual dot or dash
Returns: nul
Calls: none
*/
void flashLED(char dotOrDash)
{
if (debug)
Serial.print(dotOrDash);
if (dotOrDash == 46) // ASCII code for a dot is 46
{
digitalWrite(ledPin, HIGH); // turn the LED on
tone (speakerPin, pitch1);
delay(dot); // wait for period of the dot
digitalWrite(ledPin, LOW); // turn the LED off
noTone(speakerPin);
delay(dot);
}
else
if (dotOrDash == 45)
{ // ASCII code for a dash is 45
digitalWrite(ledPin, HIGH); // turn the LED on
tone (speakerPin, pitch2);
delay(dash); // wait for period of the dash
digitalWrite(ledPin, LOW); // turn the LED off
noTone(speakerPin);
delay(dot);
}
else
if (dotOrDash == 32) // ASCII code for a space is 32
delay(dot); // No flashes, just a time delay
}