Arduino Serial.readString() problems

Hey, I've been working on this code for an arduino nano robot arm system that runs off commands from the Serial monitor for a while now and I came back to it today and it appears that the Serial.readString() command is not working or is wrong (that's at least this is what I have concluded, I could be wrong).

I have tried this command in other sketches and it works as expected, but its just this line of code that it wont get past.

I won't post the rest of the code unless requested because its over 400 lines long and I am 90% sure its the following code that's the problem.

void Run() {
    String input = Serial.readString(); //Read the input
    Serial.println(input); //Repeat the input back
    if (input.startsWith("manual")) { 
      manual();
    }
    if (input.startsWith("zero")) {
      zero();
    }
    if (input.startsWith("end")) {
      prog_end();
    }
    if (input.startsWith("test")) {
      test();
    }
    if (input.startsWith("help")) {
      help();
    }
}

Any ideas?

Thanks for your time, A_Nerd

PS. I'm open to any recommendations to improve my code

You are reading from the serial buffer before you know if there is data in the buffer to be read. Use Serial.available() to see if there is data in the buffer.

Consider dropping the use of String objects. And readString is a blocking function that can tie up the processor for long times. Use the serial input basics methods to read data into a null terminated character array in a non blocking fashion.
Use c-string functions to do search and copy.

@OP

1. This is what the Serial.available() command does, and you should invoke it before getting out data from the FIFO buffer as has been advised in Post#1.

When a valid data item (8-bit ASCII code or binary code) arrives at the Receiver Register of the UNO's MCU, the data byte immediately enters into a FIFO type buffer. If we don't bring out the data byte from the FIFO into a user variable by executing a Serial.read() instruction, it will remain there. Before we perform a Serial.read() operation on the FIFO, we naturally wish to check if the FIFO has actually accumulated any data item. The execution of the
byte n = Serial.available(); instruction assigns an integer value to the variable n, which is equal to the number of 'data items' currently present in the FIFO. Therefore, the logical codes would like:

void loop()
{
   byte n = Serial.available();
   if (n != 0)       //FIFO has one data item provided we have read the previously arrived data item 
   {
       byte x1 = Serial.read();  //there is no more data item in the FIFO
    }
}

if (n != 0) //FIFO has one data item provided we have read the previously arrived data item

Another example of you talking out your ass. The FIFO buffer has AT LEAST ONE, NOT exactly one, byte in it.

PaulS:
Another example of you talking out your ass. The FIFO buffer has AT LEAST ONE, NOT exactly one, byte in it.

To care how many of them?

  1. Someone is creating wit and humor;

  2. Someone is cryptic;

  3. Someone is critical;

  4. Someone is poetic;

  5. Someone is running after others;

  6. Someone is ............................

if (n != 0) //FIFO has one data item provided we have read the previously arrived data item

The Compiler (being a Machine) is considerate to the asshole; but, not the Human Being who has created the Compiler! A Machine would certainly correct the semantic error of the commented message by putting the not at the appropriate place; whereas, the Man is running after the treasure that is missing here. The issue could be brought to attention through the creation of a wit for which he is famous; instead, things has been unnecessarily exaggerated as now-a-days there are not many 'quality posts' to interact.

Would the following work to check whether there are available data in the serial buffer and then run the run() function?

if(Serial.available() > 0){
    run();
}

void Run() {
    String input = Serial.readString(); //Read the input

    Serial.println(input); //Repeat the input back

    if (input.startsWith("manual")) { 
      manual();
    }
}

Would the following work to check whether there are available data in the serial buffer and then run the run() function?

What did your testing tell you?

PaulS:
What did your testing tell you?

Sorry, I was a bit quick to post a question before testing.
The testing relieved no changes and I have decided to find a better way of doing this, possibly using a GUI.