Morse Code Translator

I am trying to do the “Morse Code Translator from Getting Started with Sketches” Book by Simon Monk. Although I have copied the code and gone through it several times I keep getting compilation errors which I cannot get rid of. Any pointers to what is wrong are gratefully appreciated. Its driving me mad…
Code Below:

//morse code translator

int dotDelay = 200;
int ledPin = 13;

// Morse code for Alphabet
char* letters[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",   // A-G
                        "....", "..", ".---", "_._", ".-..", "--", "-.",    // H-N
                        "---", ".--.", "--.-", ".-.", "...", "-", "..-",    // O-U
                        "...-", ".--", "-..-", "-.--", "--.."               // V-Z
                      };

// Morse code for Numbers
char* numbers[] = { "-----", ".----", "..---", "...--", "....-",       // 0-4
                        ".....", "-....", "--...", "---..", "----."         // 5-9
                      };

void setup()
{
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
}

void loop()
{
char ch;
if (Serial.available()  >  0)
{
ch = Serial.read();
if (ch >= 'a' &&  ch <= 'z')
{
flashSequence (letters [ch – 'a']);
}
else if (ch >= 'A' && ch <= 'Z')
{
flashSequence (letters [ch – 'A']);
}
else if (ch >= '0' && ch <= '9')
{
flashSequence (numbers [ch – '0']);
}
else if (ch == ' ' )
{
delay (dotDelay * 4);  //gap between words
    }
  }
}

void flashSequence (char* sequence)
{
int i = 0;
while (sequence[i] != '\0');
{
flashDotOrDash (sequence [i]);
i++;
}
delay(dotDelay * 3); 
}

void flashDotOrDash (char dotOrDash)
{
digitalWrite (ledPin, HIGH);
if  (dotOrDash == '.' )
{
delay (dotDelay);
}
else // must be a -
{
delay (dotDelay * 3);
}
digitalWrite (ledPin, LOW);
delay (dotDelay); //gap between flashes
}

Errors:
sketch_aug16a:33: error: stray ‘’ in program
sketch_aug16a:37: error: stray ‘’ in program
sketch_aug16a:41: error: stray ‘’ in program
sketch_aug16a.ino: In function ‘void loop()’:
sketch_aug16a:33: error: expected ]' before 'u2013' sketch_aug16a:33: error: expected )’ before ‘u2013’
sketch_aug16a:37: error: expected ]' before 'u2013' sketch_aug16a:37: error: expected )’ before ‘u2013’
sketch_aug16a:41: error: expected ]' before 'u2013' sketch_aug16a:41: error: expected )’ before ‘u2013’

moderator: code tags added == # button above smileys

  1. Read the how to use the forum and post the code correctly - you can correct your post
  2. Where did you get the code from? It has all the hall marks of being copied from a web browser or text recognition output and having some none printing characters in it.

the problem you encounter might be that the copy is done in unicode not in flat ascii

Code modified to compile, essentially retyped the references to the arrays. (which I wonder if they work, but that’s a second Q)

//morse code translator

int dotDelay = 200;
int ledPin = 13;

// Morse code for Alphabet
char *letters[] = { 
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", 
  "....", "..", ".---", "_._", ".-..", "--", "-.", 
  "---", ".--.", "--.-", ".-.", "...", "-", "..-", 
  "...-", ".--", "-..-", "-.--", "--.."
};

// Morse code for Numbers
char *numbers[] = { 
  "-----", ".----", "..---", "...--", "....-",       // 0-4
  ".....", "-....", "--...", "---..", "----."         // 5-9
};

void setup()
{
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
}

void loop()
{
  char ch;
  if (Serial.available()  >  0)
  {
    ch = Serial.read();
    if (ch >= 'a' &&  ch <= 'z')
    {
      flashSequence(letters[ch - 'a']);
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
      flashSequence(letters[ch - 'A']);
    }
    else if (ch >= '0' && ch <= '9')
    {
      flashSequence(numbers[ch - '0']);
    }
    else if (ch == ' ' )
    {
      delay (dotDelay * 4);  //gap between words
    }
  }
}

void flashSequence (char* sequence)
{
  int i = 0;
  while (sequence[i] != '\0');
  {
    flashDotOrDash(sequence [i]);
    i++;
  }
  delay(dotDelay * 3); 
}

void flashDotOrDash(char dotOrDash)
{
  digitalWrite (ledPin, HIGH);
  if  (dotOrDash == '.' )
  {
    delay (dotDelay);
  }
  else // must be a -
  {
    delay (dotDelay * 3);
  }
  digitalWrite (ledPin, LOW);
  delay (dotDelay); //gap between flashes
}

Take a look at this thread which discusses the same piece of code.

Hi Sue

There are three statements in the program like this:

flashSequence (letters [ch - 'a']);

The problem seems to be that the minus sign character between ch and ‘a’ is not a minus sign but a different dash. Try deleting the dash and typing a minus sign.

Regards

Ray

Using Rob's posted code and following Ray's suggestion on the retyping of the - signs I then had to correct the syntax of the while statement

 while (sequence[i] != '\0');

by removing the semi colon

 while (sequence[i] != '\0')

Compiles, runs, and flashes.