Input Text Box for Morse Code?

Hello,

I apologize if this has already been talked about but I could not find it using the search function. I'm, as my first project, trying to make a light blink out morse code. However, in the part of my code where I type the actual message, I have to put the "();" after every letter. Is there an easier way to do this such as a text box where the code could automatically separate the letters orr something that would increase the functionality of the code?

Below is the code I have been using:

int pin = 13;

void setup()
{
pinMode(pin, OUTPUT);
}

void loop()
{
p(); l(); e(); a(); s(); e(); //First Word Here. Must have (); after every letter.
delay(1250); //Pause for in between words
h(); e(); l(); p(); //Second Word Here. Must have (); after every letter. Continue in same pattern for as long as desired.
delay(1250);
m(); e();
delay(3000); //End Pause. Make Longer for actual use.
}
void a() //Script for letter 'a'. The next 25 are for all the letters of the alphabet. numbers may be added later. Dots and dashes defined after the alphabet.
{
dot(); dash();
}
void b()
{
dash(); dot(); dot(); dot();
}
void c()
{
dash(); dot(); dash(); dot();
}
void d()
{
dash(); dot(); dot();
}
void e()
{
dot();
}
void f()
{
dot(); dot(); dash(); dot();
}
void g()
{
dash(); dash(); dot();
}
void h()
{
dot(); dot(); dot(); dot();
}
void i()
{
dot(); dot();
}
void j()
{
dot(); dash(); dash(); dash();
}
void k()
{
dash(); dot(); dash();
}
void l()
{
dot(); dash(); dot(); dot();
}
void m()
{
dash(); dash();
}
void n()
{
dash(); dot();
}
void o()
{
dash(); dash(); dash();
}
void p()
{
dot(); dash(); dash(); dot();
}
void q()
{
dash(); dash(); dot(); dash();
}
void r()
{
dot(); dash(); dot();
}
void s()
{
dot(); dot(); dot();
}
void t()
{
dash();
}
void u()
{
dot(); dot(); dash();
}
void v()
{
dot(); dot(); dot(); dash();
}
void w()
{
dot(); dash(); dash();
}
void x()
{
dash(); dot(); dot(); dash();
}
void y()
{
dash(); dot(); dash(); dash();
}
void z()
{
dash(); dash(); dot(); dot();
}

void dot() //Scripting for the dot and dash. Dash's are 4x longer than dots.
{
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}

void dash()
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(250);
}

wow very complicated way to realize it !

you have to use arrays that contains information for each letter.

As you know morse for each letter is a maximum of 4 dash or dot symbol. So let us code them in an array

1 means dot
2 means dash
0 means nothing to do

byte morse_letter[26][4]= {
{  1, 2, 0, 0 },   // 'a' dot dash
{  2, 1, 1, 1 },   // 'b' dash dot dot dot
{ continue to fill until x I finish with z},
{ 2, 2, 1, 1 }     // 'z' dash dash dot dot
};

after in your soft :

void loop()
{
  char stringToCode[]="thisisatestformorse";   // A long string to code in morse without space, cause space doesn't exists in morse 



  byte i, k;                                                    // variable for 'for' loops
  char ch;                                                     // current letter to 'play'
 
for (i=0; i<strlen(stringToCode); i++)     // Lets do a loop from the first to the last character of the sentence
 {
   byte index;                                   // index will hold the 'right' letter

   ch = stringToCode[i];                     // get the letter #i from the sentence
   
   index = ch-'a';                              // convert it to the right index of our array as 'a'=64 in the ascii table

   for (k=0; k<3; k++)                       // lets look at the 4 values of the array for this letter
   {
      if (morse_letter[index][k] == 1)   // its 1, so lets do a dot !
        dot();
      if (morse_letter[index][k] == 2)   // its 2, so its a dash
        dash();
      if (morse_letter[index][k] == 0)   // its 0, there is no more datas we can exit from the loop.
         break;
   }
 }
}

It will be easier !

Okay, that seems to make sense. So what improvements does that make to my current system? I'm just trying to understand the advantages of an array.

Also, would I define the 'dots' and 'dashes' as I did in my original code or is there a special way to do it with arrays?

edit: In addition, KE7GKP, you referred to having the code parse my input. How would I go about trying to achieve that? Is it an 'if, then' format?

David

Get the same dash and dot from you code these are functions.

the use of arrays is just a versatil way to represent datas. It one of the the way of doing it. You could either use :

string of char like :

char a_letter[]=".-";
char b_letter[]="-...";
char ....
char z_letter[]="--..";

all depends of your favorite way you like to play with datas !

Right, sorry for not being clearer. I understand that a dot is represented by, say, "1" and a dash by "2", but how do I say what the "1" is; for example, in my original code I 'said' that a dot meant:

void dot()
{
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}

Do I do the same thing in an array or is there a special way of doing so? I apologize if I am missing something.

why dont you just put the whole thing in a 2 dimension array ( you could use a structure but i dont know about arduino support for it.
first use #defines for your dots, dash, space ie
#define DOT 1
then declare a array called morse if i remember you only have 0-9, a-z ie 36 plus the space for 37
and 5 wide that is one for the morse letter, and the rest for the code.
char morse[37][5] {'A', DOT, DASH,,} and so on
rember that C uses 0 indexing of arrays so you could now switch some letter from your keyboard call it letter
switch letter{
case morse[0][0]: ok now just build the string in a buffer and you are done.
case morse[0][1]:

Tarawa565:
Right, sorry for not being clearer. I understand that a dot is represented by, say, "1" and a dash by "2", but how do I say what the "1" is; for example, in my original code I 'said' that a dot meant:

void dot()
{
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
delay(250);
}

Do I do the same thing in an array or is there a special way of doing so? I apologize if I am missing something.

the choice is made when we are doing the comparison tests :

if (morse_letter[index][k] == 1)
dot();
if (morse_letter[index][k] == 2)
dash();

in that case 1 means dot() and 2 means dash() !!!

It's just as you want !

Oh, okay. Thank you. I apologize for not understanding that sooner.

Thank you for all the help. I'll have to try this out as soon as I get some free time.

Sincerely,
David

Don't apologize, every one here starts once in there live to understand coding.

Read, test and ask as many questions as you need to understand what you do.

Might want to look over post #8 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274407868/1>.

Check out my code to get an idea of how to do this properly.

raron has also developed similar code if you search some past forum posts in exhibition. His code employs some binary trees to further optimize the code.

Thanks for the links, those look like they'll help alot.

Question; I think I understand how the array itself works now, but I really don't get some of the coding itself. (I know what it does, I just don't know how it does it). Are there any good sites on the coding logic? The main one that I can think of right now is the function where its like:

i<_____; i++

I get what it does, I just don't understand what the arduino is 'reading' when it sees this.

David

http://cplusplus.com/doc/tutorial/control/

It's a tutorial.

Thank you so much! That's exactly what I was looking for! :slight_smile:

You're welcome.

I've also wrapped my code in a library. Check out this post:

http://arduino.cc/forum/index.php/topic,60242.0.html

Wow, thats really cool. I think I'm going to try to set up a speaker to it, but for now I'm just doing the light.

David

Speaker is easy, you replace your LED with the speaker, replace digitalWrite(led,HIGH); with tone(speaker,550); and digitalWrite(led,LOW); with noTone(speaker);

It's just as easy as that! I already memorized a few words in Morse code with my translator.

Sorry for the long response time. But thank you. That sounds alot easier than I thought. What kind of speaker did you use? All I saw at radio shack were buzzers and chimes.

David

David,

This one at radio shack may work fine. Have maybe a small resistor such as 150ohm in series with it just in case.

I am using this one as included in my phi-2 shield kit:

http://dipmicro.com/store/DBX-01PN

Okay, I think I saw that one, or atleast something similar to it. I'll try to get it ASAP. Thanks for all the help. I'm sure I'll have more questions but until school gets out, no time :frowning:

David