Morse code program problems

/* THIS PROGRAM IS SUPPOSED TO CONVERT THE TEXT GIVEN IN THE SERIAL MONITOR INTO MORSE CODE
WHICH WILL BE FLASHED USING AN LED ON PIN 12
MADE BY-- ARJUN MONGA
DATE – 21/3/2015
FOR-- ARDUINO UNO */

// also if one could tell me how to optimise this code for multi-lettered messages then I would be grateful :slight_smile:

/*The problem is that like if one enters any character for the first time and presses enter, the appropriate morse code wil flash.
But if suppose one enters that character for the second time and presses enter, it will flash more than one time (two or three times).
Try with ‘0’ (character for zero). The morse code is ‘dash dash dash dash dash’ , but what flashes is ‘dash dash dash dash dash…dash dash dash…dash dash dash’ which is wrong.
*/

void setup() {
pinMode(12,OUTPUT); // SETTING PIN 12 AS THE OUTPUT
Serial.begin(9600);
digitalWrite(12,LOW); // SET THE PIN LOW INITIALLY
}

void loop() {
char morse_str[100]; //CHARACTER ARRAY FOR THE TEXT
void dot(void); // DECLARING DOT FUNCTION IN THE LOOP
void dash(void); // DECLARING DASH FUNCTION IN THE LOOP
if (Serial.available())
{
for (int j=0;j<=98;j++)
{
morse_str[j]=Serial.read(); // USING FOR LOOP TO READ THE TEXT INTO THE CHARACTER ARRAY (I THINK THE PROBLEM’S HERE)
}
for (int i=0;morse_str*!=’\0’;i++) // LOOPING THROUGH THE CHAR ARRAY TO CONVERT EACH CHAR INTO ITS RESPECTIVE MORSE CODE*

  • {*
    switch (morse_str) { // SWITCHING EACH CHAR (morse_str*)*

* case ‘A’:{dot();dash();}*
* case ‘B’:{dash();dot();dot();dot();break;}*
* case ‘C’:{dash();dot();dot();break;}*
* case ‘D’:{dash();dot();dash();dot();break;}*
* case ‘E’:{dot();break;}*
* case ‘F’:{dot();dot();dash();dot();break;}*
* case ‘G’:{dash();dash();dot();break;}*
* case ‘H’:{dot();dot();dot();dot();break;}*
* case ‘I’:{dot();dot();break;}*
* case ‘J’:{dot();dash();dash();dash();break;}*
* case ‘K’:{dash();dot();dash();break;}*
* case ‘L’:{dot();dash();dot();dot();break;}*
* case ‘M’:{dash();dash();break;}*
* case ‘N’:{dot();dash();break;}*
* case ‘O’:{dash();dash();dash();break;}*
* case ‘P’:{dash();dot();dash();dot();break;}*
* case ‘Q’:{dash();dash();dot();dash();break;}*
* case ‘R’:{dash();dot();dash();dot();break;}*
* case ‘S’:{dot();dot();dot();break;}*
* case ‘T’:{dash();break;}*
* case ‘U’:{dot();dot();dash();break;}*
* case ‘V’:{dot();dot();dot();dash();break;}*
* case ‘W’:{dot();dash();dash();break;}*
* case ‘X’:{dash();dot();dot();dash();break;}*
* case ‘Y’:{dash();dot();dash();dash();break;}*
* case ‘Z’:{dash();dash();dot();dot();break;}*
* case ’ ':{delay(700);break;} //FOR SPACES BETWEEN WORDS*
* case ‘1’:{dot();dash();dash();dash();dash();break;}*
* case ‘2’:{dot();dot();dash();dash();dash();break;}*
* case ‘3’:{dot();dot();dot();dash();dash();break;}*
* case ‘4’:{dot();dot();dot();dot();dash();break;}*
* case ‘5’:{dot();dot();dot();dot();dot();break;}*
* case ‘6’:{dash();dot();dot();dot();dot();break;}*
* case ‘7’:{dash();dash();dot();dot();dot();break;}*
* case ‘8’:{dash();dash();dash();dot();dot();break;}*
* case ‘9’:{dash();dash();dash();dash();dot();break;}*
* case ‘0’:{dash();dash();dash();dash();dash();break;}*
* default : {Serial.println(i);break;} // TO SEE THE NUMBER OF DEFAULTS (NOT EXPECTED, BUT OCCURING)*

* }}}*
}
void dot(void) //FUNCTION FOR DOT
{
* digitalWrite(12,HIGH);*
* delay(100);*
* digitalWrite(12,LOW);*
* delay(100);*
}
void dash(void) //FUNCTION FOR DASH
{
* digitalWrite(12,HIGH);*
* delay(300);*
* digitalWrite(12,LOW);*
* delay(100);*
}

Why is most of your code in italics in your post ?

It is because you didn't read this before posting a programming question
Add code tags to make it easier to look at.

How to use this forum

I'm sorry, I browsed through those rules, looks like i missed that point.
But now it says do not re-post, so what should I do?

  /* THIS PROGRAM IS SUPPOSED TO CONVERT THE TEXT GIVEN IN THE SERIAL MONITOR INTO MORSE CODE 
WHICH WILL BE FLASHED USING AN LED ON PIN 12 
MADE BY-- ARJUN MONGA
DATE -- 21/3/2015          
FOR-- ARDUINO UNO        */

// also if one could tell me how to optimise this code for multi-lettered messages then I would be grateful :-) 

/*The problem is that like if one enters any character for the first time and presses enter, the appropriate morse code wil flash.
 But if suppose one enters that character for the second time and presses enter, it will flash more than one time (two or three times).
 Try with '0' (character for zero). The morse code is 'dash dash dash dash dash' , but what flashes is 'dash dash dash dash dash...dash dash dash...dash dash dash' which is wrong.
 */

void setup() {
  pinMode(12,OUTPUT);           // SETTING PIN 12 AS THE OUTPUT
  Serial.begin(9600);
  digitalWrite(12,LOW);       // SET THE PIN LOW INITIALLY
}



void loop() {
  char morse_str[100];       //CHARACTER ARRAY FOR THE TEXT
    void dot(void);                  // DECLARING DOT FUNCTION IN THE LOOP
    void dash(void);                 // DECLARING DASH FUNCTION IN THE LOOP
      if (Serial.available())
       { 
        for (int j=0;j<=98;j++)
          {
            morse_str[j]=Serial.read();             // USING FOR LOOP TO READ THE TEXT INTO THE CHARACTER ARRAY (I THINK THE PROBLEM'S HERE)
          }
        for (int i=0;morse_str[i]!='\0';i++)        // LOOPING THROUGH THE CHAR ARRAY TO CONVERT EACH CHAR INTO ITS RESPECTIVE MORSE CODE
        { 
          switch (morse_str[i])  {                  // SWITCHING EACH CHAR (morse_str[i])
           
          case 'A':{dot();dash();}
          case 'B':{dash();dot();dot();dot();break;}
          case 'C':{dash();dot();dot();break;}
          case 'D':{dash();dot();dash();dot();break;}
          case 'E':{dot();break;}
          case 'F':{dot();dot();dash();dot();break;}
          case 'G':{dash();dash();dot();break;}
          case 'H':{dot();dot();dot();dot();break;}
          case 'I':{dot();dot();break;}
          case 'J':{dot();dash();dash();dash();break;}
          case 'K':{dash();dot();dash();break;}
          case 'L':{dot();dash();dot();dot();break;}
          case 'M':{dash();dash();break;}
          case 'N':{dot();dash();break;}
          case 'O':{dash();dash();dash();break;}
          case 'P':{dash();dot();dash();dot();break;}
          case 'Q':{dash();dash();dot();dash();break;}
          case 'R':{dash();dot();dash();dot();break;}
          case 'S':{dot();dot();dot();break;}
          case 'T':{dash();break;}
          case 'U':{dot();dot();dash();break;}
          case 'V':{dot();dot();dot();dash();break;}
          case 'W':{dot();dash();dash();break;}
          case 'X':{dash();dot();dot();dash();break;}
          case 'Y':{dash();dot();dash();dash();break;}
          case 'Z':{dash();dash();dot();dot();break;}
          case ' ':{delay(700);break;}                //FOR SPACES BETWEEN WORDS
          case '1':{dot();dash();dash();dash();dash();break;}
          case '2':{dot();dot();dash();dash();dash();break;}
          case '3':{dot();dot();dot();dash();dash();break;}
          case '4':{dot();dot();dot();dot();dash();break;}
          case '5':{dot();dot();dot();dot();dot();break;}
          case '6':{dash();dot();dot();dot();dot();break;}
          case '7':{dash();dash();dot();dot();dot();break;}
          case '8':{dash();dash();dash();dot();dot();break;}
          case '9':{dash();dash();dash();dash();dot();break;}
          case '0':{dash();dash();dash();dash();dash();break;}

          default : {Serial.println(i);break;}     // TO SEE THE NUMBER OF DEFAULTS (NOT EXPECTED, BUT OCCURING)
          
  
     }}}
}

void dot(void)                //FUNCTION FOR DOT 
{
  digitalWrite(12,HIGH);
  delay(100);
  digitalWrite(12,LOW);
  delay(100);
}

void dash(void)              //FUNCTION FOR DASH
{
  digitalWrite(12,HIGH);
  delay(300);
  digitalWrite(12,LOW);
  delay(100);
}
      if (Serial.available())
       { 
        for (int j=0;j<=98;j++)
          {
            morse_str[j]=Serial.read();             // USING FOR LOOP TO READ THE TEXT INTO THE CHARACTER ARRAY (I THINK THE PROBLEM'S HERE)
          }

You know you have one byte available. Then you read 99. How does this work?

See this: http://forum.arduino.cc/index.php?topic=277631.msg1952967#msg1952967

Well, thanks Nick, but as I'm relatively new to C programming, I don' t know about the asterisks ( * ) you are using in the code. I assume it is something to do with pointers and references?
You said something about one byte being available. I wanted to know if there is a way to parse strings which are typed into the serial monitor, like the parseInt() function. I don't want to input one character at a time, but a string which might be 100 or even more characters long. I there is a way then please let me know...
Thanks

See Serial input basics

well, thanks guys for your help. I noticed that when i replaced the morse_str[100] array with a variable morse_str, and removed all the for loops which aided the array, the code worked just fine and now does what i want it to do, even if i enter a string in the serial monitor. now what i want is happening but i dont know how.
any ideas?

arjmon:
well, thanks guys for your help. I noticed that when i replaced the morse_str[100] array with a variable morse_str, and removed all the for loops which aided the array, the code worked just fine and now does what i want it to do, even if i enter a string in the serial monitor. now what i want is happening but i dont know how.
any ideas?

You mean it works but you can't imagine how it does? Just don't tell anybody! :slight_smile:

now what i want is happening but i dont know how.
any ideas?

You are asking on any ideas how your unposted code works? Sorry, no idea.