No problem. I need to grade my final exams too.
Hello, so I had some time recently and finished putting in all of the characters for the array. However, I saw that morse code does in fact have a space between words (1750 ms) and I don't know how to add that. Any help with this would be greatly appreciated. At first I tried to add a 27th letter, a space letter, to do it, but then nothing happens. The code is below.
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
}
byte letter[27][4]= {
{ 1, 2, 0, 0 }, //A
{ 2, 1, 1, 1 }, //B
{ 2, 1, 2, 1 }, //C
{ 2, 1, 1, 0 }, //D
{ 1, 0, 0, 0 }, //E
{ 1, 1, 2, 1 }, //F
{ 2, 2, 1, 0 }, //G
{ 1, 1, 1, 1 }, //H
{ 1, 1, 0, 0 }, //I
{ 1, 2, 2, 2 }, //J
{ 2, 1, 2, 0 }, //K
{ 1, 2, 1, 1 }, //L
{ 2, 2, 0, 0 }, //M
{ 2, 1, 0, 0 }, //N
{ 2, 2, 2, 0 }, //O
{ 1, 2, 2, 1 }, //P
{ 2, 2, 1, 2 }, //Q
{ 1, 2, 1, 0 }, //R
{ 1, 1, 1, 0 }, //S
{ 2, 0, 0, 0 }, //T
{ 1, 1, 2, 0 }, //U
{ 1, 1, 1, 2 }, //V
{ 1, 2, 2, 0 }, //W
{ 2, 1, 1, 2 }, //X
{ 2, 1, 2, 2 }, //Y
{ 2, 2, 1, 1 }, //Z
{ 3, 0, 0, 0 }, //_
};
void loop()
{
char stringToCode[]="a b c"; // Insert text for morse code here
// Need to do a space????? (1750)
delay(5000);
byte i, k;
char ch;
for (i=0; i<strlen(stringToCode); i++)
{
byte index;
ch = stringToCode[i];
index = ch-'a';
for (k=0; k<3; k++)
{
if (letter[index][k] == 1)
dot();
if (letter[index][k] == 2)
dash();
if (letter[index][k] == 3)
space();
if (letter[index][k] == 0)
break;
}
}
}
void dot()
{
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}
void dash()
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(250);
}
void space()
{
digitalWrite(pin, LOW);
delay(1750);
}
David
Thank you. Would that be an if then statement?
So,
if (space);
delay(1750);
But then, how would you define what space is? Or would it be better to do it as an abscence of a letter?
David
Tarawa565:
Thank you. Would that be an if then statement?So,
if (space);
delay(1750);But then, how would you define what space is? Or would it be better to do it as an abscence of a letter?
David
You can also integrate the space as a dah in your array but you need to provide Morse encoding between ' ' and A, which is a lot, including numbers and symbols. You can just steal my list though. No problem about that. Mine also has a very neat feature that each item's first letter is the character and Morse code starts from the second character. Helps you read when you don't know Morse code that well, like me, I only know hello and sos. All you do in your code is to skip first letter when playing Morse code.
Here you go:
unsigned char space_to_Z[][8]={" ", "!-.-.--", "\".-..-.", "# ", "$ ", "% ", "&.-...", "\'.----.", "(-.--.", ")-.--.-", "* ", "+.-.-.", ",--..--", "m-....-", "p.-.-.-", "/-..-.", "0-----", "1.----", "2..---", "3...--", "4....-", "5.....", "6-....", "7--...", "8---..", "9----.", ":---...", ";-.-.-.", "< ", "=-...-", "> ", "?..--..", "@.--.-.","A.-", "B-...", "C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I..", "J.---", "K-.-", "L.-..", "M--", "N-.", "O---", "P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--", "X-..-", "Y-.--", "Z--.."};
Some symbols don't have agreed-upon Morse code (eh, I'm no expert here
) so their lists are empty. I also wanted to store the list in PROGMEM to save SRAM but haven't done so with limited time.
For something a little more compact I'm going to suggest something along the line of my code I pointed out on the last page.
It uses a character/encoding pair table lookup to locate the morse encoding sequence, 'encoding' of the matching character sought and found 'ascii'.
PSUEDO CODE
FOR-EACH-CHARACTER-TO-BE-SENT
doCharactor(toupper(ch));
struct encoding_t
{
const char ascii;
const unsigned short encoding;
};
static const encoding_t morse_encodings[] =
{
// a string of 2-bit values encode the morse sequence, LSb -> MSb
// 00 end-of-sequence
// 01 '.'
// 10 '-'
// 11 unused
// NOTE: encoding are read (backwords) from right to left
// --- Numeric
{ '0', 0x02AA } // 00 00 00 10 - 10 10 10 10 "-----"
, { '1', 0x02A9 } // 00 00 00 10 - 10 10 10 01 ".----"
, { '2', 0x02A5 } // 00 00 00 10 - 10 10 01 01 "..---"
, { '3', 0x0295 } // 00 00 00 10 - 10 01 01 01 "...--"
, { '4', 0x0255 } // 00 00 00 10 - 01 01 01 01 "....-"
, { '5', 0x0155 } // 00 00 00 01 - 01 01 01 01 "....."
, { '6', 0x0156 } // 00 00 00 01 - 01 01 01 10 "-...."
, { '7', 0x015A } // 00 00 00 01 - 01 01 10 10 "--..."
, { '8', 0x016A } // 00 00 00 01 - 01 10 10 10 "---.."
, { '9', 0x01AA } // 00 00 00 01 - 10 10 10 10 "----."
// --- Alphabetic
, { 'A', 0x0009 } // 00 00 00 00 - 00 00 10 01 ".-"
, { 'B', 0x0056 } // 00 00 00 00 - 01 01 01 10 "-..."
, { 'C', 0x0066 } // 00 00 00 00 - 01 10 01 10 "-.-."
, { 'D', 0x0016 } // 00 00 00 00 - 00 01 01 10 "-.."
, { 'E', 0x0001 } // 00 00 00 00 - 00 00 00 01 "."
, { 'F', 0x0065 } // 00 00 00 00 - 01 10 01 01 "..-."
, { 'G', 0x001A } // 00 00 00 00 - 00 01 10 10 "--."
, { 'H', 0x0055 } // 00 00 00 00 - 01 01 01 01 "...."
, { 'I', 0x0005 } // 00 00 00 00 - 00 00 01 01 ".."
, { 'J', 0x00A9 } // 00 00 00 00 - 10 10 10 01 ".---"
, { 'K', 0x0026 } // 00 00 00 00 - 00 10 01 10 "-.-"
, { 'L', 0x0059 } // 00 00 00 00 - 01 01 10 01 ".-.."
, { 'M', 0x000A } // 00 00 00 00 - 00 00 10 10 "--"
, { 'N', 0x0006 } // 00 00 00 00 - 00 00 01 10 "-."
, { 'O', 0x002A } // 00 00 00 00 - 00 10 10 10 "---"
, { 'P', 0x0069 } // 00 00 00 00 - 01 10 10 01 ".--."
, { 'Q', 0x009A } // 00 00 00 00 - 10 01 10 10 "--.-"
, { 'R', 0x0019 } // 00 00 00 00 - 00 01 10 01 ".-."
, { 'S', 0x0015 } // 00 00 00 00 - 00 01 01 01 "..."
, { 'T', 0x0002 } // 00 00 00 00 - 00 00 00 10 "-"
, { 'U', 0x0025 } // 00 00 00 00 - 00 10 01 01 "..-"
, { 'V', 0x0095 } // 00 00 00 00 - 10 01 01 01 "...-"
, { 'W', 0x0029 } // 00 00 00 00 - 00 10 10 01 ".--"
, { 'X', 0x0096 } // 00 00 00 00 - 10 01 01 10 "-..-"
, { 'W', 0x00A6 } // 00 00 00 00 - 10 10 01 10 "-.--"
, { 'Z', 0x005A } // 00 00 00 00 - 01 01 10 10 "--.."
// --- Punctuation
, { ',', 0x0A5A } // 00 00 00 00 - 00 00 00 00 "--..--"
, { '.', 0x0999 } // 00 00 00 00 - 00 00 00 00 ".-.-.-"
, { ':', 0x056A } // 00 00 00 00 - 00 00 00 00 "---..."
, { '?', 0x05A5 } // 00 00 00 00 - 00 00 00 00 "..--.."
, { '-', 0x0956 } // 00 00 00 00 - 00 00 00 00 "-....-"
, { '/', 0x0196 } // 00 00 00 01 - 00 00 00 00 "-..-."
, { '(', 0x09A6 } // 00 00 00 00 - 00 00 00 00 "-.--.-"
, { ')', 0x09A6 } // 00 00 00 00 - 00 00 00 00 "-.--.-"
// --- ARDUINO Windows IDE has problems with escaped ascii literals!
#if 0
, { '\'', 0x06A9 } // 00 00 00 00 - 00 00 00 00 ".----."
, { '\"', 0x0699 } // 00 00 00 00 - 00 00 00 00 ".-.--."
#endif
};
enum { e_EOS = 0, e_DOT /* = 1 */, e_DASH /* = 2 */, eMASK /* = 3 */ };
static void doSequence(unsigned short encoding)
{
do
{
// mask of 2 most right bits giving a number 0 - 3
// 00 end-of-sequence
// 01 '.'
// 10 '-'
switch ( encoding & eMASK )
{
case e_DOT: dot(); break;
case e_DASH:dash(); break;
default: break;
}
// shift 'encoding' dropping the 2 most right bits and bringing 2 zero bits into left side
encoding >>= 2;
// if 'encoding' os NOT zero there must be more dashes or dots, delay for length of space
if ( encoding ) { delay(g_dtDot); }
// if 2 right most bits of 'encoding' are zero loop around again
} while ( e_EOS != (encoding & eMASK) );
// else we are done so simply fall thru and exit
}
static void doCharactor(char ch)
{
const int cEntries = sizeof(morse_encodings) / sizeof(morse_encodings[0]);
// prepare to look thru each entry in the 'morse_encodings' array for a matching character 'ch'
for ( int i = 0; i < cEntries; i++)
{
if ( ch == morse_encodings[i].ascii )
{
// if matching character found process encoded 'encoding' field to extract DOT/DASH's
// and then stop looking by returning.
doSequence(morse_encodings[i].encoding);
return;
}
}
}
[CODE]
[/code]
So liudr, that list completely replaces my 26 lines of code for the letters? But then, I have to redefine the dots and dashes as well?
And, just trying to understand the code, but every character/symbol is in quotes, then the first character is what the code is, and every character that follows is the code itself? So in the one below, the arduino doesn't read the '1' but instead starts with the '.' ?
"1.----",
And lloyddean,
That code seems interesting, I might try it later but for now I just want to try to get this array down. I'm a sophmore in High School with no formal coding instruction so I want to make sure I have stuff down before I move on.
David
Okay, so I have done everything BUT defining the dots and dashes. How do I go about doing this? Before it was a 'dot' or 'dash' but now it is a '.' or '-' Any suggestions?
Okay, so I got that down, but how do I connect the '-' to the 'if -, dash'? I sincerely apologize for all of the basic questions I have, but I'm basically learning as I go here. I've been looking up stuff for awhile now but I couldn't find anything ![]()
And no, I'm not ready to tackle something else yet. What you said was exactly what I was asking about, thank you.
David
Sorry for not being more clear. I get how to define what the '-' is, but I don't get how I connect that to my string of text. I think there should be a 'for' statement before I say 'If' but I don't know what the 'for' statement is.
David
Then yes. Sorry about that. I'm not familiar with all of the lexicon of the subject.
David