Printing String in Serial Moitor with Buffer

Hey. I'm making a morse code proejct that outputs morse code through an led while printing it in the serial monitor.

I am trying to print the string that I input which will be converted to morse code at the start.
I am having trouble printing the inputted string at the start in which I have in bold.
Such as:

Hello there

H
....
E
.
L
.-..

and so on...

Here's my code!

const unsigned int MAX_MESSAGE_LENGTH = 64;

#define DEBUG_MODE
#define LEDPIN 13 //the led pin

#define TIME_UNIT 1000
#define DOT_L 1
#define DASH_L 3
#define LETTER_SPACE_L 3
#define WORD_SPACE_L 7
#define SYMBOL_L 1

#define DOT_TIME (TIME_UNIT * DOT_L)
#define DASH_TIME (TIME_UNIT * DASH_L)
#define LETTER_SPACE (TIME_UNIT * (LETTER_SPACE_L - SYMBOL_L))
#define WORD_SPACE (TIME_UNIT * (WORD_SPACE_L - (LETTER_SPACE_L)))
#define SYMBOL_DELAY (TIME_UNIT * SYMBOL_L)

char input;// to save the input
void setup () {
  pinMode(LEDPIN, OUTPUT);//tell that the 13 pin is an output
  Serial.begin(9600);//for the connect with the boared
}

void loop () {

 while (Serial.available() > 0)
 {
   //Create a place to hold the incoming message
   static char message[MAX_MESSAGE_LENGTH];
   static unsigned int message_pos = 0;

   //Read the next available byte in the serial receive buffer
   char inByte = Serial.read();

   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
   {
     //Add the incoming byte to our message
     message[message_pos] = inByte;
     message_pos++;
   }
   //Full message received...
   else
   {
     //Add null character to string
     message[message_pos] = '\0';

     //Print the message (or do other things)
     Serial.println(message);

     //Reset for the next message
     message_pos = 0;
   }
 }
  
  if (Serial.available()) {
    input = toUpperCase (Serial.read());//read the input
    Serial.println("");
    Serial.println (input);//print the latter saved in the input var

    if (input == 'A') {
      lA(); //if the input is a or A go to function lA
    }
    if (input == 'B') {
      lB(); //same but with b letter
    }
    if (input == 'C') {
      lC();
    }
    if (input == 'D') {
      lD();
    }
    if (input == 'E') {
      lE();
    }
    if (input == 'F') {
      lF();
    }
    if (input == 'G') {
      lG();
    }
    if (input == 'H') {
      lH();
    }
    if (input == 'I') {
      lI();
    }
    if (input == 'J') {
      lJ();
    }
    if (input == 'K') {
      lK();
    }
    if (input == 'L') {
      lL();
    }
    if (input == 'M') {
      lM();
    }
    if (input == 'N') {
      lN();
    }
    if (input == 'O') {
      lO();
    }
    if (input == 'P') {
      lP();
    }
    if (input == 'Q') {
      lQ();
    }
    if (input == 'R') {
      lR();
    }
    if (input == 'S') {
      lS();
    }
    if (input == 'T') {
      lT();
    }
    if (input == 'U') {
      lU();
    }
    if (input == 'V') {
      lV();
    }
    if (input == 'W') {
      lW();
    }
    if (input == 'X') {
      lX();
    }
    if (input == 'Y') {
      lY();
    }
    if (input == 'Z') {
      lZ();
    }
    if (input == '1') {
      n1(); // the numbers
    }
    if (input == '2') {
      n2();
    }
    if (input == '3') {
      n3();
    }
    if (input == '4') {
      n4();
    }
    if (input == '5') {
      n5();
    }
    if (input == '6') {
      n6();
    }
    if (input == '7') {
      n7();
    }
    if (input == '8') {
      n8();
    }
    if (input == '9') {
      n9();
    }
    if (input == '0') {
      n0();
    }
    if (input == ' ') {
      space(); //the space
    }

  }
}
//fonctions for the letters and the numbers
void lA () {
  dot();  //letter A in morse code!
  dash();
  shortspace();
}
void lB () {
  dash();  //same for B
  dot();
  dot();
  dot();
  shortspace();
}
void lC () {
  dash();
  dot();
  dash();
  dot();
  shortspace();
}
void lD () {
  dash();
  dot();
  dot();
  shortspace();
}
void lE () {
  dot();
  shortspace();
}
void lF () {
  dot();
  dot();
  dash();
  dot();
  shortspace();
}
void lG () {
  dash();
  dash();
  dot();
  shortspace();
}
void lH () {
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void lI () {
  dot();
  dot();
  shortspace();
}
void lJ () {
  dot();
  dash();
  dash();
  dash();
  shortspace();
}
void lK () {
  dash();
  dot();
  dash();
  shortspace();
}
void lL () {
  dot();
  dash();
  dot();
  dot();
  shortspace();
}
void lM () {
  dash();
  dash();
  shortspace();
}
void lN () {
  dash();
  dot();
  shortspace();
}
void lO () {
  dash();
  dash();
  dash();
  shortspace();
}
void lP () {
  dot();
  dash();
  dash();
  dot();
  shortspace();
}
void lQ () {
  dash();
  dash();
  dot();
  dash();
  shortspace();
}
void lR () {
  dot();
  dash();
  dot();
  shortspace();
}
void lS () {
  dot();
  dot();
  dot();
  shortspace();
}
void lT () {
  dash();
  shortspace();
}
void lU () {
  dot();
  dot();
  dash();
  shortspace();
}
void lV () {
  dot();
  dot();
  dot();
  dash();
  shortspace();
}
void lW () {
  dot();
  dash();
  dash();
  shortspace();
}
void lX () {
  dash();
  dot();
  dot();
  dash();
  shortspace();
}
void lY () {
  dash();
  dot();
  dash();
  dash();
  shortspace();
}
void lZ () {
  dash();
  dash();
  dot();
  dot();
  shortspace();
}
void n1 () {
  dot();  //number 1 in morse code
  dash();
  dash();
  dash();
  dash();
  shortspace();
}
void n2 () {
  dot();
  dot();
  dash();
  dash();
  dash();
  shortspace();
}
void n3 () {
  dot();
  dot();
  dot();
  dash();
  dash();
  shortspace();
}
void n4 () {
  dot();
  dot();
  dot();
  dot();
  dash();
  shortspace();
}
void n5 () {
  dot();
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void n6 () {
  dash();
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void n7 () {
  dash();
  dash();
  dot();
  dot();
  dot();
  shortspace();
}
void n8 () {
  dash();
  dash();
  dash();
  dot();
  dot();
  shortspace();
}
void n9 () {
  dash();
  dash();
  dash();
  dash();
  dot();
  shortspace();
}
void n0 () {
  dash();
  dash();
  dash();
  dash();
  dash();
  shortspace();
}
void space () {
  delay(WORD_SPACE); //space between words
}
void dot () {
  digitalWrite(LEDPIN, HIGH);  //the dot this code make the led on for 300 than off for 300
  Serial.print('.');
  delay(DOT_TIME);
  digitalWrite(LEDPIN, LOW);
  delay(SYMBOL_DELAY);
}
void dash () {
  digitalWrite(LEDPIN, HIGH);  //the dash this code make the led on for 900 than off for 300
  Serial.print('-');
  delay(DASH_TIME);
  digitalWrite(LEDPIN, LOW);
  delay(SYMBOL_DELAY);
}
void shortspace () {
  delay(LETTER_SPACE); //space between letters
}
`

That's a lot of code
Do you have a question?

My apologies, I updated the question now.

In your loop() function it you have a while loop at the beginning:

while (Serial.available() > 0)

Followed by this if conditional:

if (Serial.available()) {

As soon as there is not a character available for the while loop it will exit the while loop and move to the if conditional. I'm not sure what you are trying to do but I'm pretty sure it is not what you have actually done. When you are typing characters in it will be rather random whether the character is processed in the while loop versus the if block.

1 Like

Yes, you have conflicting Serial.read() routines.

Let the first routine read until the complete message is received.
Then you will read the characters from the message. Make message a global variable, and use a boolean control variable when the message has been read from the monitor to trigger the morse output.
See if this does what you want

#define DEBUG_MODE
#define LEDPIN 13 //the led pin

#define TIME_UNIT 1000
#define DOT_L 1
#define DASH_L 3
#define LETTER_SPACE_L 3
#define WORD_SPACE_L 7
#define SYMBOL_L 1

#define DOT_TIME (TIME_UNIT * DOT_L)
#define DASH_TIME (TIME_UNIT * DASH_L)
#define LETTER_SPACE (TIME_UNIT * (LETTER_SPACE_L - SYMBOL_L))
#define WORD_SPACE (TIME_UNIT * (WORD_SPACE_L - (LETTER_SPACE_L)))
#define SYMBOL_DELAY (TIME_UNIT * SYMBOL_L)

const unsigned int MAX_MESSAGE_LENGTH = 64;
char message[MAX_MESSAGE_LENGTH];
char input;// to save the input
boolean messageInput = false;

void setup () {
  pinMode(LEDPIN, OUTPUT);//tell that the 13 pin is an output
  Serial.begin(9600);//for the connect with the boared
  Serial.println("starting");
}

void loop () {

  while (Serial.available() > 0)
  {
    //Create a place to hold the incoming message
    //static char message[MAX_MESSAGE_LENGTH];
    static unsigned int message_pos = 0;

    //Read the next available byte in the serial receive buffer
    char inByte = Serial.read();

    //Message coming in (check not terminating character) and guard for over message size
    if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
    {
      //Add the incoming byte to our message
      message[message_pos] = inByte;
      message_pos++;
    }
    //Full message received...
    else
    {
      //Add null character to string
      message[message_pos] = '\0';

      //Print the message (or do other things)
      Serial.println(message);
      messageInput = true;

      //Reset for the next message
      message_pos = 0;
    }
  }


  //if (Serial.available()) {
  if (messageInput) {
    static byte i = 0;
    input = toUpperCase (message[i]);//read the input
    Serial.println("");
    Serial.println (input);//print the latter saved in the input var

    if (input == 'A') {
      lA(); //if the input is a or A go to function lA
    }
    if (input == 'B') {
      lB(); //same but with b letter
    }
    if (input == 'C') {
      lC();
    }
    if (input == 'D') {
      lD();
    }
    if (input == 'E') {
      lE();
    }
    if (input == 'F') {
      lF();
    }
    if (input == 'G') {
      lG();
    }
    if (input == 'H') {
      lH();
    }
    if (input == 'I') {
      lI();
    }
    if (input == 'J') {
      lJ();
    }
    if (input == 'K') {
      lK();
    }
    if (input == 'L') {
      lL();
    }
    if (input == 'M') {
      lM();
    }
    if (input == 'N') {
      lN();
    }
    if (input == 'O') {
      lO();
    }
    if (input == 'P') {
      lP();
    }
    if (input == 'Q') {
      lQ();
    }
    if (input == 'R') {
      lR();
    }
    if (input == 'S') {
      lS();
    }
    if (input == 'T') {
      lT();
    }
    if (input == 'U') {
      lU();
    }
    if (input == 'V') {
      lV();
    }
    if (input == 'W') {
      lW();
    }
    if (input == 'X') {
      lX();
    }
    if (input == 'Y') {
      lY();
    }
    if (input == 'Z') {
      lZ();
    }
    if (input == '1') {
      n1(); // the numbers
    }
    if (input == '2') {
      n2();
    }
    if (input == '3') {
      n3();
    }
    if (input == '4') {
      n4();
    }
    if (input == '5') {
      n5();
    }
    if (input == '6') {
      n6();
    }
    if (input == '7') {
      n7();
    }
    if (input == '8') {
      n8();
    }
    if (input == '9') {
      n9();
    }
    if (input == '0') {
      n0();
    }
    if (input == ' ') {
      space(); //the space
    }

    i++;
    if (i == strlen(message)+1)
    { 
      i = 0;
      messageInput = false;
      Serial.println("done");
    }
  }
  
}
//fonctions for the letters and the numbers
void lA () {
  dot();  //letter A in morse code!
  dash();
  shortspace();
}
void lB () {
  dash();  //same for B
  dot();
  dot();
  dot();
  shortspace();
}
void lC () {
  dash();
  dot();
  dash();
  dot();
  shortspace();
}
void lD () {
  dash();
  dot();
  dot();
  shortspace();
}
void lE () {
  dot();
  shortspace();
}
void lF () {
  dot();
  dot();
  dash();
  dot();
  shortspace();
}
void lG () {
  dash();
  dash();
  dot();
  shortspace();
}
void lH () {
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void lI () {
  dot();
  dot();
  shortspace();
}
void lJ () {
  dot();
  dash();
  dash();
  dash();
  shortspace();
}
void lK () {
  dash();
  dot();
  dash();
  shortspace();
}
void lL () {
  dot();
  dash();
  dot();
  dot();
  shortspace();
}
void lM () {
  dash();
  dash();
  shortspace();
}
void lN () {
  dash();
  dot();
  shortspace();
}
void lO () {
  dash();
  dash();
  dash();
  shortspace();
}
void lP () {
  dot();
  dash();
  dash();
  dot();
  shortspace();
}
void lQ () {
  dash();
  dash();
  dot();
  dash();
  shortspace();
}
void lR () {
  dot();
  dash();
  dot();
  shortspace();
}
void lS () {
  dot();
  dot();
  dot();
  shortspace();
}
void lT () {
  dash();
  shortspace();
}
void lU () {
  dot();
  dot();
  dash();
  shortspace();
}
void lV () {
  dot();
  dot();
  dot();
  dash();
  shortspace();
}
void lW () {
  dot();
  dash();
  dash();
  shortspace();
}
void lX () {
  dash();
  dot();
  dot();
  dash();
  shortspace();
}
void lY () {
  dash();
  dot();
  dash();
  dash();
  shortspace();
}
void lZ () {
  dash();
  dash();
  dot();
  dot();
  shortspace();
}
void n1 () {
  dot();  //number 1 in morse code
  dash();
  dash();
  dash();
  dash();
  shortspace();
}
void n2 () {
  dot();
  dot();
  dash();
  dash();
  dash();
  shortspace();
}
void n3 () {
  dot();
  dot();
  dot();
  dash();
  dash();
  shortspace();
}
void n4 () {
  dot();
  dot();
  dot();
  dot();
  dash();
  shortspace();
}
void n5 () {
  dot();
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void n6 () {
  dash();
  dot();
  dot();
  dot();
  dot();
  shortspace();
}
void n7 () {
  dash();
  dash();
  dot();
  dot();
  dot();
  shortspace();
}
void n8 () {
  dash();
  dash();
  dash();
  dot();
  dot();
  shortspace();
}
void n9 () {
  dash();
  dash();
  dash();
  dash();
  dot();
  shortspace();
}
void n0 () {
  dash();
  dash();
  dash();
  dash();
  dash();
  shortspace();
}
void space () {
  delay(WORD_SPACE); //space between words
}
void dot () {
  digitalWrite(LEDPIN, HIGH);  //the dot this code make the led on for 300 than off for 300
  Serial.print('.');
  delay(DOT_TIME);
  digitalWrite(LEDPIN, LOW);
  delay(SYMBOL_DELAY);
}
void dash () {
  digitalWrite(LEDPIN, HIGH);  //the dash this code make the led on for 900 than off for 300
  Serial.print('-');
  delay(DASH_TIME);
  digitalWrite(LEDPIN, LOW);
  delay(SYMBOL_DELAY);
}
void shortspace () {
  delay(LETTER_SPACE); //space between letters
}

Thanks so much, worked like a charm!

There is an easier way if you are inputting whole lines from the IDE serial monitor. Set the option to New Line, type in something and press ctrl-enter. In your routine put:

String input;
input=Serial.readStringUntil('\n');

\n being, of course, the newline character.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.