Newbie : arrays and strings and morse code and help needed

Hey everyone,

First off, some info: Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Uno"

So I'm not that familiar with C/C++ (as you'll soon see), and what I'm trying to make right now is a program that will flash a light in morse code based on the string that's entered. I'm looping through the string one character at a time and checking IF A ELSE IF B ELSE IF C etc etc etc.... I realize I can probably set up another array and so a simple comparison, but right now the nasty if, else if works.

Only it's not. The problem I'm running into is the array which I want to hold the morse code value. The longest string is 6 (numbers are 5 characters long and the \n, right?) but when I try to set the array to be anything less than 6 (the letter A for example is only 3) it breaks with this error:
error: incompatible types in assignment of 'const char [3]' to 'char [6]'

How can I fix this? Ideas are welcome!

Thanks in advance!

Here's the (bad) code:

/*
This is a simple morse code program.
It uses international Morse Code.
*/

//Declaring the pin to use
int ledPin = 13;

//Declaring the various intervals to use
int dotLength = 1       //length of time the light will be on for a dot
  , dashLength = 3      //length of time the light will be on for a dash
  , letterPartPause = 1 //length of pause between parts of the same letter
  , letterPause = 3     //length of pause between letters
  , wordPause = 7       //length of pause between words
  , multiplier = 250;   //this will turn each unit in the above durations into a quarter second
  

//Declaring a variable that will be loaded with the morse code values
char morseCode[6];

//Declaring the word to output
char messageToConvert[] = "ARDUINO IS AWESOME";

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

void loop()
{
  //Start the loop with the LED off
  digitalWrite(ledPin, LOW);
  
  //Loop through each character of the message
  for (int i = 0; i<= sizeof(messageToConvert); i++)
  {
    if (messageToConvert[i] == 'A')
    {
      morseCode = ".-";
    }
    else if (messageToConvert[i] == 'B')
    {
      morseCode = "-...";     
    }
    else if (messageToConvert[i] == 'C')
    {
      morseCode = "-.-.";     
    }
    else if (messageToConvert[i] == 'D')
    {
      morseCode = "-..";     
    }
    else if (messageToConvert[i] == 'E')
    {
      morseCode = ".";     
    }
    else if (messageToConvert[i] == 'F')
    {
      morseCode = "..-.";     
    }
    else if (messageToConvert[i] == 'G')
    {
      morseCode = "--.";     
    }
    else if (messageToConvert[i] == 'H')
    {
      morseCode = "....";     
    }
    else if (messageToConvert[i] == 'I')
    {
      morseCode = "..";     
    }
    else if (messageToConvert[i] == 'J')
    {
      morseCode = ".---";     
    }
    else if (messageToConvert[i] == 'K')
    {
      morseCode = "-.-";     
    }
    else if (messageToConvert[i] == 'L')
    {
      morseCode = ".-..";     
    }
    else if (messageToConvert[i] == 'M')
    {
      morseCode = "--";     
    }
    else if (messageToConvert[i] == 'N')
    {
      morseCode = "-.";     
    }
    else if (messageToConvert[i] == 'O')
    {
      morseCode = "---";     
    }
    else if (messageToConvert[i] == 'P')
    {
      morseCode = ".--.";     
    }
    else if (messageToConvert[i] == 'Q')
    {
      morseCode = "--.-";     
    }
    else if (messageToConvert[i] == 'R')
    {
      morseCode = ".-.";     
    }
    else if (messageToConvert[i] == 'S')
    {
      morseCode = "...";     
    }
    else if (messageToConvert[i] == 'T')
    {
      morseCode = "-";     
    }
    else if (messageToConvert[i] == 'U')
    {
      morseCode = "..-";     
    }
    else if (messageToConvert[i] == 'V')
    {
      morseCode = "...-";     
    }
    else if (messageToConvert[i] == 'W')
    {
      morseCode = ".--";     
    }
    else if (messageToConvert[i] == 'X')
    {
      morseCode = "-..-";     
    }
    else if (messageToConvert[i] == 'Y')
    {
      morseCode = "-.--";     
    }
    else if (messageToConvert[i] == 'Z')
    {
      morseCode = "--..";     
    }
    else if (messageToConvert[i] == '1')
    {
      morseCode = ".----";     
    }
    else if (messageToConvert[i] == '2')
    {
      morseCode = "..---";     
    }
    else if (messageToConvert[i] == '3')
    {
      morseCode = "...--";     
    }
    else if (messageToConvert[i] == '4')
    {
      morseCode = "....-";     
    }
    else if (messageToConvert[i] == '5')
    {
      morseCode = ".....";     
    }
    else if (messageToConvert[i] == '6')
    {
      morseCode = "-....";     
    }
    else if (messageToConvert[i] == '7')
    {
      morseCode = "--...";     
    }
    else if (messageToConvert[i] == '8')
    {
      morseCode = "---..";     
    }
    else if (messageToConvert[i] == '9')
    {
      morseCode = "----.";     
    }
    else if (messageToConvert[i] == '0')
    {
      morseCode = "-----";     
    }
    else
    {
      moreseCode = " "
    }

    for (int m = 0; i <= sizeof(morseCode); m++)
    {
      if (morseCode[m] == '.')
      {
        digitalWrite(ledPin, HIGH);
        delay(dotLength * multiplier);
      }
      else if (morseCode[m] == '-')
      {
        digitalWrite(ledPin, HIGH);
        delay(dashLength * multiplier);
      }
      else if (morseCode[m] == ' ')
      {
        digitalWrite(ledPin, LOW);
        delay(wordPause * multiplier);
      }
      digitalWrite(ledPin, LOW);
      delay(letterPartPause * multiplier);
    }
    digitalWrite(ledPin, LOW);
    delay((letterPause - letterPartPause) * multiplier);
  }
  
  //Pause added between message repeats
  digitalWrite(ledPin, LOW);
  delay(60000);
}

Part of the problem is that you can't do simple assignments with char strings. For example, your line:

    if (messageToConvert[i] == 'A')
    {
      morseCode = ".-";

needs to be changed to:

    if (messageToConvert[i] == 'A')
    {
      strcpy(morseCode,  ".-");

and so on. After you get it to work, then worry about better ways to code a solution.

This causes an error too.

     moreseCode = " "

Pete

Thanks econjack and el_supremo, I can now get it to display the first letter in the message!
From there it's all downhill, maybe. For some reason it's not continuing the loop through to the end.

I'll see what else I can find. Thanks for your help!

  for (int i = 0; i<= sizeof(messageToConvert); i++)

That needs to be:

  for (int i = 0; i < strlen(messageToConvert); i++)

strlen returns the length of a string.

Great! That was the last piece I was missing! Thanks, Nick!
I got it working! Woohoo for my little morse code message generator!

Also, I replaced the If else if block with a switch.

Good. I didn't want to say anything before, but that is certainly an improvement. You might also consider an array.

From a morse program I wrote a while back:

char * letters [26] = {
   ".-",     // 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
};

Then for a particular letter, you just look up the code from the array, eg.

   char * sequence = NULL;
    
    if (c >= 'A' && c <= 'Z')
      sequence = letters [c - 'A'];

Quick and easy.

You might also add, just before your switch statement and within the for loop, something like:

   char targetCharacter;                // near the top of loop()
   
   // more code...

   // for loop code using variable i...

      targetCharacter = toupper(messageToConvert[i]);
      switch (targetCharacter) {
         // the switch code...
      }

The call to toupper() will convert each character in the message to an upper case character just in case the message mixes upper and lower case letters. Otherwise it will fail on lower case characters.