vivekvs97:
So these are my queries.
In addition, I want to know if there is any way I can make the whole project a little more efficient. I know there are somethings I can compress with regard to the Morse to English bit. I'd be really happy if anyone guided me on this here.
About two years ago I posted this small little program in this forum for another guy who asked about Morse encoding/decoding.
This piece of code is for the serial monitor only (no button, no piezo/speaker, no external hardware):
The Morse code table is stored in a very efficient way: Each character encoding is stored in a single byte, so the whole table of 26 letters and 10 digits is 36 bytes in array size only.
/* 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)
{
c= handleMorseInput(1); // retrieve decoded char from input buffer
Serial.print(c);
isMorseInput=false;
}
else if (c==' ') Serial.print(' ');
else if (c=='\n') Serial.println();
else if (c>32) // filter out control characters and international special characters
{
sendMorseChar(toupper(c));
}
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
handleSerialInput();
}
This is the inner working: When sending plain English text from the serial monitor, it is translated to dots and dashes in Morse code, which aredisplayed in the serial monitor.
Or it can work the other way round: When sending dots and dashes in Morse code, they are translated back to plain English text, which is displayed in the serial monitor.
Is this a start for you?
Please let me know what you think!
Please feel free to ask in case you have questions about the code, or possible extending the code with handling piezo speaker for tone output or a push button for Morse input.