Serial Printing Strings

When I try to print this, the variable comes out on a different line and I don't know why

String mname;
String msg = "name?";
String msg2 = "Hello, ";
String msg3 = "welcome ";
void setup() {

  Serial.begin(9600);
}

void loop() {

  Serial.println(msg);
  while (Serial.available() == 0) {
  }
  mname = Serial.readString();
  Serial.print(msg2);
  Serial.println(mname);
  Serial.print(msg3);
}

You probably have the serial monitor's line ending set to something other than no line ending, so a new line is being read in and thus printed out.

edit: and one of your prints is a println...

@op

Learn "char type" c-strings first (not String with capital-S) and then practice "String type" strings.

Example-1: "char type" c-string
Send the string "Hello" from the InputBox of Serial Monitor (Fig-1) with "Line ending tab" at "Newline" option. Receive the string by UNO and then send it to OutputBox of Serial Monitor.


Figure-1:

Sketch:
1. Upload the following sketch in UNO.

char myData[10] = "";

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  byte y = Serial.available();  //check that a char has come from Serial Monitor
  if(y != 0)
  {
    byte m = Serial.readBytesUntil('\n', myData, 10); //read and save until Newline
    myData[m] = '\0'; //insert null-byte as last element in char-type array
    Serial.print(myData); //show received message on Serial Monitor
    Serial.println();  //insert line feed
    //-----------------------------------
    memset(myData, 0, 10);  //reset the array
  }
}

2. Open the Serial Monitor at 9600 bd.
3. Choose Newline option for the Line ending tab.
4. Enter Hello in the InoutBox and then click on Send Button.
5. Check that Hello has appeared on the OutputBox (Fig-2).
6. Enter Welcome in the InoutBox and click on the Send button.
7. Check that Welcome has appeared on the next line of Hello (Fig-2).

smdfq.png
Figure-2:

smdfq.png

Yes I had the serial monitor set to "newline" by mistake, thank you

eatmorekale:
Yes I had the serial monitor set to "newline" by mistake, thank you

"newline" --> "Newline"

char myData[10] = "";
...
...
    byte m = Serial.readBytesUntil('\n', myData, 10); //read and save until Newline
    myData[m] = '\0';

Oops.

TheMemberFormerlyKnownAsAWOL:

char myData[10] = "";

...
...
   byte m = Serial.readBytesUntil('\n', myData, 10); //read and save until Newline
   myData[m] = '\0';




Oops.

You mean that the following line is unnecessary.

myData[m] = '\0';

Then, I will do the following to keep the above line in the sketch.

char myData[10];

No, I mean "consider the case where 'm' equals 10".

But "oops" is shorter, and is meant to convey the meaning "there's something wrong here, maybe you should reconsider your code"

TheMemberFormerlyKnownAsAWOL:
No, I mean "consider the case where 'm' equals 10".

But "oops" is shorter, and is meant to convey the meaning "there's something wrong here, maybe you should reconsider your code"

I hope that I have got your point.

When m = 10, it means 10 characters have already been stored in the array. Now, there is no more space in the array to hold the null-character as the array size is only 10. So, array size should be at least 11. My sketch worked; because, the entered string had less than 10 charcaters.

I have no words to repay for your meticulous observation.

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