user41611: I'm using a Mega 2560 and Uno and want to input text to the serial monitor which can be stored in character array char in[];
I want array specifically as I'll be breaking each character to trigger different code for making a morse code.
    #include<Arduino.h>
//#include<Softwareserial.h>

int Speaker1 = 4;
int Speaker2 = 6;
int LED = 2;
int Relay = 12;
char n = '0';
char in[];
//int outPins[] = { 2, 4, 6, 8 };

void high() {

  digitalWrite(Speaker1, HIGH);
  tone(Speaker2, 800);
  digitalWrite(LED, HIGH);
  digitalWrite(Relay, HIGH);

}

void low() {

  digitalWrite(Speaker1, LOW);
  tone(Speaker2, 800);
  digitalWrite(LED, LOW);
  digitalWrite(Relay, LOW);

}

void setup() {

  Serial.begin(9600);

  pinMode(Speaker1, OUTPUT);
  pinMode(Speaker2, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(Relay, OUTPUT);

  Serial.print("Enter your desired text: ");
  //in = Serial.readString();  /*<-------------HELP NEEDED HERE */

  for (int i = 0; i < strlen(in); i++) {

    n = in[i];

    switch (n) {

      case 'A':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'B':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'C':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'D':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'E':
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'F':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'G':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'H':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'I':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'J':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'K':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'L':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'M':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'N':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'O':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'P':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'Q':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'R':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'S':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'T':
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'U':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'V':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'W':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'X':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Y':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Z':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case ' ':
        delay(700);
        break;
      case '1':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '2':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '3':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '4':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '5':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '6':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '7':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '8':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '9':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '10':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;

    }

  }

}

void loop() {

}


user7508: I suggested in a comment you read the blog post Reading Serial on the
Arduino. In this post, Majenko shows a readline()
function that does almost exactly what you want. This function is
non-blocking, which, in the embedded world, is most of the time a
mandatory feature. However, in one of your comments you wrote that you
want a blocking function:

I need the program to "wait" like [...] scanf

Degrading a non-blocking function into a blocking one is as easy as
wrapping the non-blocking function in a loop. Here is a code snippet for
reading a CR-terminated string, which is similar to a blocking version
of Majenko's readline():
char buffer[80];
size_t pos = 0;
for (;;) {
    int c = Serial.read();
    if (c == -1) continue;   // no input
    if (c == '\r') {         // end of line
        buffer[pos] = '\0';  // terminate the string
        break;
    }
    if (pos < sizeof buffer - 1) {
        buffer[pos++] = c;
    }
}

That being said, I could not resist the temptation to brutally simplify
your program. The key to the simplification is to consider the Morse
code as a data structure. Then, the code for keying a string is
generic: you look up each character in the code table and key it in
turn. This saves the long switch/case:
struct code_t {
    char c;
    const char *code;
};

static const code_t morse_code[] = {
    {'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', "--.." },
    {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
    {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."},
    {'9', "----."}, {'0', "-----"},
    {' ', " "    }, {'\0', NULL}  // sentinel
};

static inline void high() { digitalWrite(LED_BUILTIN, HIGH); }
static inline void low()  { digitalWrite(LED_BUILTIN, LOW);  }

// Key a string in Morse.
void key(const char *s)
{
    for (; *s; s++) {  // *s = current letter

        // Find this letter's code.
        const code_t *p;
        for (p = morse_code; p->c; p++) {
            if (p->c == toupper(*s)) break;  // found
        }
        if (!p->c) continue;  // not found -> ignore

        // Send this code's symbols.
        for (const char *code = p->code; *code; code++) {
            if (*code != ' ')
                high();
            switch (*code) {
                case ' ': delay(100); break;
                case '.': delay(100); break;
                case '-': delay(300); break;
            }
            low();
            delay(100);
        }
        delay(200);  // inter-letter spacing = 300 ms
    }
}

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

void loop()
{
    // Read a string.
    char buffer[80];
    size_t pos = 0;
    for (;;) {
        int c = Serial.read();
        if (c == -1) continue;   // no input
        if (c == '\r') {         // end of line
            buffer[pos] = '\0';  // terminate the string
            break;
        }
        if (pos < sizeof buffer - 1) {
            buffer[pos++] = c;
        }
    }

    // Key it in Morse.
    key(buffer);
}

Note that this program reads CR-terminated strings from the Serial port
and keys them on the builtin LED. If you want your program to work on a
single string, you can call exit() at the end of loop() or,
alternatively, merge loop() into setup() and provide an empty
loop().

41611: <p>I'm using a Mega 2560 and Uno and want to input text to the serial monitor which can be stored in character array char in[];
I want array specifically as I'll be breaking each character to trigger different code for making a morse code.</p>

<pre><code>    #include&lt;Arduino.h&gt;
//#include&lt;Softwareserial.h&gt;

int Speaker1 = 4;
int Speaker2 = 6;
int LED = 2;
int Relay = 12;
char n = '0';
char in[];
//int outPins[] = { 2, 4, 6, 8 };

void high() {

  digitalWrite(Speaker1, HIGH);
  tone(Speaker2, 800);
  digitalWrite(LED, HIGH);
  digitalWrite(Relay, HIGH);

}

void low() {

  digitalWrite(Speaker1, LOW);
  tone(Speaker2, 800);
  digitalWrite(LED, LOW);
  digitalWrite(Relay, LOW);

}

void setup() {

  Serial.begin(9600);

  pinMode(Speaker1, OUTPUT);
  pinMode(Speaker2, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(Relay, OUTPUT);

  Serial.print("Enter your desired text: ");
  //in = Serial.readString();  /*&lt;-------------HELP NEEDED HERE */

  for (int i = 0; i &lt; strlen(in); i++) {

    n = in[i];

    switch (n) {

      case 'A':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'B':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'C':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'D':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'E':
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'F':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'G':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'H':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'I':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'J':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'K':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'L':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'M':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'N':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'O':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'P':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'Q':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'R':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'S':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case 'T':
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'U':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'V':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'W':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'X':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Y':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case 'Z':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case ' ':
        delay(700);
        break;
      case '1':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '2':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '3':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '4':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;
      case '5':
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '6':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '7':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '8':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '9':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(100);
        low();
        delay(300);
        break;
      case '10':
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(100);
        high();
        delay(300);
        low();
        delay(300);
        break;

    }

  }

}

void loop() {

}
</code></pre>
 7508: <p>I suggested in a comment you read the blog post <a href="https://hackingmajenkoblog.wordpress.com/2016/02/01/reading-serial-on-the-arduino/" rel="nofollow noreferrer">Reading Serial on the
Arduino</a>. In this post, Majenko shows a <code>readline()</code>
function that does almost exactly what you want. This function is
non-blocking, which, in the embedded world, is most of the time a
mandatory feature. However, in one of your comments you wrote that you
want a blocking function:</p>

<blockquote>
  <p>I need the program to "wait" like [...] <code>scanf</code></p>
</blockquote>

<p>Degrading a non-blocking function into a blocking one is as easy as
wrapping the non-blocking function in a loop. Here is a code snippet for
reading a CR-terminated string, which is similar to a blocking version
of Majenko's <code>readline()</code>:</p>

<pre><code>char buffer[80];
size_t pos = 0;
for (;;) {
    int c = Serial.read();
    if (c == -1) continue;   // no input
    if (c == '\r') {         // end of line
        buffer[pos] = '\0';  // terminate the string
        break;
    }
    if (pos &lt; sizeof buffer - 1) {
        buffer[pos++] = c;
    }
}
</code></pre>

<p>That being said, I could not resist the temptation to brutally simplify
your program. The key to the simplification is to consider the Morse
code as a <em>data structure</em>. Then, the code for keying a string is
generic: you look up each character in the code table and key it in
turn. This saves the long <code>switch</code>/<code>case</code>:</p>

<pre><code>struct code_t {
    char c;
    const char *code;
};

static const code_t morse_code[] = {
    {'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', "--.." },
    {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
    {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."},
    {'9', "----."}, {'0', "-----"},
    {' ', " "    }, {'\0', NULL}  // sentinel
};

static inline void high() { digitalWrite(LED_BUILTIN, HIGH); }
static inline void low()  { digitalWrite(LED_BUILTIN, LOW);  }

// Key a string in Morse.
void key(const char *s)
{
    for (; *s; s++) {  // *s = current letter

        // Find this letter's code.
        const code_t *p;
        for (p = morse_code; p-&gt;c; p++) {
            if (p-&gt;c == toupper(*s)) break;  // found
        }
        if (!p-&gt;c) continue;  // not found -&gt; ignore

        // Send this code's symbols.
        for (const char *code = p-&gt;code; *code; code++) {
            if (*code != ' ')
                high();
            switch (*code) {
                case ' ': delay(100); break;
                case '.': delay(100); break;
                case '-': delay(300); break;
            }
            low();
            delay(100);
        }
        delay(200);  // inter-letter spacing = 300 ms
    }
}

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

void loop()
{
    // Read a string.
    char buffer[80];
    size_t pos = 0;
    for (;;) {
        int c = Serial.read();
        if (c == -1) continue;   // no input
        if (c == '\r') {         // end of line
            buffer[pos] = '\0';  // terminate the string
            break;
        }
        if (pos &lt; sizeof buffer - 1) {
            buffer[pos++] = c;
        }
    }

    // Key it in Morse.
    key(buffer);
}
</code></pre>

<p>Note that this program reads CR-terminated strings from the Serial port
and keys them on the builtin LED. If you want your program to work on a
single string, you can call <code>exit()</code> at the end of <code>loop()</code> or,
alternatively, merge <code>loop()</code> into <code>setup()</code> and provide an empty
<code>loop()</code>.</p>
