read serial data

I recently got an arduino, the only output device I currently have is an led. After already completing the blink and fade projects I decided to create a program where I could enter a sentence through the serial monitor and the arduino would use the led to express it in Morse code however I am unsure of how to record sentences sent from the serial monitor.

A great place to start:

http://www.gammon.com.au/forum/?id=11425

steinie44: A great place to start:

http://www.gammon.com.au/forum/?id=11425

you will learn a lot with this exercise...

look to arrange your data for each of the 36 basic morse characters...

pseudo:

int morseA[] = {dot, dash};

or like this:

byte morseA [] = {0, 1}
byte morseB [] = {1, 0, 0, 0};

so try something like this:

define all of the characters

look for serial input and call a function for each letter received

if (Serial.available) 
  dashDotMyChar();

in dashDotMyChar() process each byte with a short or long illuminations of your LED.

BulldogLowell:
if (Serial.available())
dashDotMyChar();

I regularly forget the () after Serial.available myself - until the compiler pulls me up.

The code might be easier to construct if the arrays of dots and dashes for each letter are all the same length. Of course some letters need more dots and dashes than others so you might use something like this - where the number 2 means “ignore me”

morse[26][5] = { {0,1,2,2,2}, // A
{1,0,0,0,2}, // B
// etc
}

then the code to send an A would be something like

char charToSend = Serial.read();
sendMorse(charTosend - 'A');
void sendMorse(cts) {
   for (byte n = 0; n < 5; n++) {
       if (morse[cts][n] == 2) {
           return;
        }
       else if (morse[cts][n] == 1) {
           // make a  dash
       }
      else {
          // make a dot
       }
  }
}

…R

nice.

I thought about showing him sizeof() too, but your array structure is easier with all 26 no, make that 36 elements.

#define DOT 0
#define DASH 1
#define NULL 2

morse[36][5] = { {DOT, DASH, NULL, NULL, NULL},   // A

just for the old school morse guys who wouldn't mind all that extra typing! XD

Why you don't get a buzzer and play the "real" Morse, like this: http://silveiraneto.net/2009/02/28/morse-code-translator-with-arduino/

You could make the programming even simpler if you defined the codes like this

morse[36][5] = { {100,200,0,0,0},   // A

Where the numbers represent the number of milliseconds that the dot and dash flashes/buzzes should last.

void sendMorse(cts) {
   for (byte n = 0; n < 5; n++) {
       if (morse[cts][n] == 0) {
           return;
        }
       else {
            digitalWrite(morsePin, HIGH);
            delay(morse[cts][n]);
            digitalWrite(morsePin, LOW);
       }
  }
}

Of course one should really use the Blink Without Delay technique rather than delay() - but the simplicity of the code is attractive.

…R

for (byte n = 0, n < 5, n++)

{ cough }

AWOL:

for (byte n = 0, n < 5, n++)

{ cough }

I must be temporarily blind - what’s the problem?

…R

Give it a moment to wear off, and then read it again.

AWOL: Give it a moment to wear off, and then read it again.

I did that (several times) before I wrote my previous request for an explanation of the problem you see, and I don't.

...R

Robin2:

AWOL: Give it a moment to wear off, and then read it again.

I did that (several times) before I wrote my previous request for an explanation of the problem you see, and I don't.

...R

It looks a little like this ".", but it's a tiny bit higher, above the ","

AWOL: It looks a little like this ".", but it's a tiny bit higher, above the ","

OK, I had commas where I should have had semi-colons and thanks for spotting the mistake. I have corrected the code.

But you could have saved both us a lot of time - and saved the readers several column inches - if you had explained the problem directly the first time.

...R

Robin2:

AWOL: It looks a little like this ".", but it's a tiny bit higher, above the ","

OK, I had commas where I should have had semi-colons and thanks for spotting the mistake. I have corrected the code.

But you could have saved both us a lot of time - and saved the readers several column inches - if you had explained the problem directly the first time.

...R

Think of me as the Mary Poppins of the Arduino world - "I never explain anything"

(I'd also say "Mary Poppins, practically perfect in every way", but I'm far too modest)

void sendMorse(cts) {
   for (byte n = 0; n < 5; n++) {
       if (morse[cts][n] == 0) {
           return;
        }
       else {
            digitalWrite(morsePin, HIGH);
            delay(morse[cts][n]);
            digitalWrite(morsePin, LOW);
       }
  }
}

Looks good. Might suggest a short delay between pulses, otherwise 100,200 will sound like one extra long pulse.

void sendMorse(cts) {
   for (byte n = 0; n < 5; n++) {
       if (morse[cts][n] == 0) {
           return;
        }
       else {
            digitalWrite(morsePin, HIGH);
            delay(morse[cts][n]);
            digitalWrite(morsePin, LOW);
            delay(50);
       }
  }
}

Don’t worry about the commas, we know what you mean,
and so did LOWA.

steinie44: Might suggest a short delay between pulses, otherwise 100,200 will sound like one extra long pulse.

Very wise.

...R