jurs:
Try this code, it can translate from ASCII text to MORSE code and the other way round:
/* Serial Morse Encoder and Decoder
* written by ‘jurs’ for Arduino forum
* Usage:
* Use the serial monitor to send ASCII text ==> will be encoded to morse code
* Use the serial monitor to send dots and dashes ==> will be decoded to ASCII
*/
char morseCode={ // first bit set tells where morse encoding starts
B00000101, // A = .-
B00011000, // B = -…
B00011010, // C = -.-.
B00001100, // D = -…
B00000010, // E = .
B00010010, // F = …-.
B00001110, // G = --.
B00010000, // H = …
B00000100, // I = …
B00010111, // J = .—
B00001101, // K = -.-
B00010100, // L = .-…
B00000111, // M = –
B00000110, // N = -.
B00001111, // O = —
B00010110, // P = .–.
B00011101, // Q = --.-
B00001010, // R = .-.
B00001000, // S = …
B00000011, // T = -
B00001001, // U = …-
B00010001, // V = …-
B00001011, // W = .–
B00011001, // X = -…-
B00011011, // Y = -.–
B00011100, // Z = --…
B00111111, // 0 = -----
B00101111, // 1 = .----
B00100111, // 2 = …—
B00100011, // 3 = …–
B00100001, // 4 = …-
B00100000, // 5 = …
B00110000, // 6 = -…
B00111000, // 7 = --…
B00111100, // 8 = —…
B00111110, // 9 = ----.
};
#define NUMCHARS sizeof(morseCode)
char morseChars=“ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;
void sendMorseChar(char c)
{
// invalid ASCII chars will not be encoded into anything
// valid ASCII chars are encoded into morse dots/dashes
// then finish with sending a space character ’ ’
char* found= strchr(morseChars, c);
if (found==NULL) return;
else
{
byte morse= morseCode[found-morseChars];
boolean firstBitFound=false;
for (int i=7; i>= 0; i–)
{
byte thisBit= bitRead(morse,i);
if (firstBitFound)
{
if (thisBit) Serial.print(’-’);
else Serial.print(’.’);
}
else if (thisBit) firstBitFound=true;
}
}
Serial.print(" ");
}
char handleMorseInput(char c)
{ // char c==0 is for clearing internal receiveBuf
// char c==1 is for evaluating internal receiveBuf
static byte receiveBuf;
if (c==0)
{
receiveBuf=1; // set 0-bit
return 0;
}
else if (c==1)
{
for (int i=0;i<NUMCHARS;i++)
{
if (receiveBuf==morseCode[i])
{
return morseChars[i];
}
}
return(’*’);
}
else if (c==’.’) receiveBuf= receiveBuf<<1;
else if (c==’-’) receiveBuf= (receiveBuf<<1) | 1;
return 0;
}
void handleSerialInput()
{
static boolean isMorseInput=false;
if (!Serial.available()) return;
char c=Serial.read();
// automatic detection of morse or ASCII input
if (c==’.’ || c==’-’)
{
if (!isMorseInput)
{ // switch to morse input and clear input buffer
isMorseInput= true;
handleMorseInput(0);
}
handleMorseInput(c);
}
else
{
if (isMorseInput)
{
char dc= handleMorseInput(1); // retrieve decoded char from input buffer
if (dc>32) Serial.print(dc);
isMorseInput=false;
}
else if (c==’\n’) Serial.println();
else if (c==’ ‘) Serial.print(’ ');
else if (c>32) // filter out control characters and international special characters
{
sendMorseChar(toupper(c));
}
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
handleSerialInput();
}
Input is taken from the serial monitor.
If you want to see additional blinking LED output instead of output on Serial only, and if you want to set a busy/timeout flag/LED while sending, I think this can be added easily.
Which pins are the speaker and LED’s connected too?