Splitting a serial command

Hello, I’m quite at the beginner level of Arduino programming, and I have a question reading parsing of a command that I send with the serial monitor.

I have a stepper motor motor that I want to control using serial commands.

A relevant piece of example code is this:

void loop() {
  char cmd;

  if(Serial.available()){

  cmd = Serial.read();
  if(cmd == '1')
  {
    //do something
  }
// repeat ifs for other commands
 }
}

I would like to send a command like RPM100. It let’s the code know to change the RPM and set it to 100. Now I have to split this command to go the RPM if block and then input the value. It seems I can’t do that with the the char class?

I’ve tried to use the String object and it works, but this made the reading very slow, as in over a second before the motor responds compared to pretty much instant with the Char object.

void loop() {
  String cmd;

  // put your main code here, to run repeatedly:
  if(Serial.available()){

  cmd = Serial.readString();
  cmd.trim();
  if(cmd.substring(0,3) == "RPM")
  {
    SetRPM(cmd.substring(3).toInt());
  }
}
}

How can I split this command while retaining the performonce that I had before?

Can you change readString’s timeout?
Or use a proper terminator in your input?

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

TheMemberFormerlyKnownAsAWOL: Can you change readString's timeout? Or use a proper terminator in your input?

Thank you very much, the timeout works fine.

Can I ask another question? I have convert the number in the command to an integer. I would like to make it a float so it can accept decimal numbers. I have tried the toFloat() function, but it outputs only numbers before the decimal. Converting a predefined string ("0.9" for example) to a float works fine however... Any simple way to handle this?