Wait for user input? String, not char!

Somewhere in my code I need to pause the program and wait for user input (serial). The input I'm getting from the user is a string. This is what I found on google for "wait for user input":

while (Serial.available() == 0) {}

Then I tried to combine with my get string for serial code:

while (Serial.available() > 0) {
input = Serial.read();
output += input;
}

Like this:

while (Serial.available() == 0) {
while (Serial.available() > 0) {
input = Serial.read();
output += input;
}
}

But it doesn't work. When I write the string in serial input and press enter, the parent while statement becomes true immediately exiting the child while loop resulting in only 1 character retrieved. So my question is:

How can I pause the program, get user input to string and carry on?

How can I pause the program, get user input to string and carry on?

Read single characters and add them to a String (if you must !) until you receive a terminating character such as Carriage Return

// wait for input
while(Serial.avalable() ==0) { // do nothing }
// read input as long as data is available
while(Serial.available() > 0)
{
  // read data here
  ...
  ...
}

How can I pause the program, get user input to string and carry on?

What terminates the string? How do you KNOW when all the user input has arrived?

If you KNOW when the end of the input has arrived, you can wait for it:

char c = '';
while(c != endOfPacketMarker)
{
   if(Serial.available() > 0)
   {
      c = Serial.read();
      // Add c to the string, NOT a stupid String
   }
}

If you don't know what terminates the user input, then you can't wait for it all to arrive, since you would have no way of knowing that it HAS all arrived. Then, you'd just have to wait for some time and hope...

UKHeliBob:
Read single characters and add them to a String (if you must !) until you receive a terminating character such as Carriage Return

I'm not sure how? Doesn't my string already have a null terminator when I initialize the variable?

sterretje:

// wait for input

while(Serial.avalable() ==0) { // do nothing }
// read input as long as data is available
while(Serial.available() > 0)
{
  // read data here
  ...
  ...
}

I tried this and didn't work :frowning:

PaulS:
What terminates the string? How do you KNOW when all the user input has arrived?

If you KNOW when the end of the input has arrived, you can wait for it:

char c = '';

while(c != endOfPacketMarker)
{
  if(Serial.available() > 0)
  {
      c = Serial.read();
      // Add c to the string, NOT a stupid String
  }
}




If you don't know what terminates the user input, then you can't wait for it all to arrive, since you would have no way of knowing that it HAS all arrived. Then, you'd just have to wait for some time and hope...

This is the format I am looking for:
A string that contains 3 "values" separated with a delimiter. I thought of using string.length() but the values do not have a fixed length. It could be:

aa.aa.aa

or

7dj.aksm.sihuiad

It could be

Where is the data coming from? If the user is entering the data in the Serial Monitor application, require them to set the line ending value to something other than none.

To get input from the Serial object via the monitor, try this simple test:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char input[20];
  int charsRead;

  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', input, sizeof(input) - 1);  // Read chars from serial monitor
    input[charsRead] = '\0';                                            // Terminate with null
    Serial.println(input);
  }
  // Now figure out how to use the strtok() function
}

After you have grabbed the user input, study how to use strtok() to read the delimiters to break out the string.

Doesn't my string already have a null terminator when I initialize the variable?

Can we be clear whether you want to use a String (a String object created using the String library) or a C style string (a zero terminated array of chars) ?

In either case you need to decide how the program will know that user input is complete.