It’s not possible to interact with strings, because arduino receives char by char, but there is a way to work around this problem.

The communication with the Arduino is simple. You usually get one char/byte, compare it to a predefined command in your code and do something. Now if you want to move for example a RC-Car to the left or to the right, you can use the char ‘R’ and ‘L’ -as it’s written in the code below- for moving to the specific direction.

#include  
Servo myservo;
char c;

void setup() {
    Serial.begin(9600);          // baud 9600
    myservo.attach(3);           // using pin 3 for the servo
    Serial.write("Power On\n");  // to know when its ready
    myservo.write(90);           // you have a range between 0°-180°
  }
void loop()                       
{
    while (Serial.available() > 0) // don't do unless there is
    {                              // an incoming byte
      c = Serial.read();      // save it to the variable 'c'
    
      if (c == 'L')                // if you recieved 'L'
      {
        Serial.write("Left\n");  // print to console
        myservo.write(180);      // set servo to 180
      } 
      else if (c == 'R')         // if you recieved 'R'
      {
        Serial.write("Right\n"); // print to console
        myservo.write(0);        // set servo to 0
      }
    }  
}

This code moves the Servo at the beginning to 90° and then if you type “R” or “L” in the Serial Monitor, it moves to 0° or 180°.

But do you really want to move it totally to the right or to the left? No. If we have the possibility to move it “smoother”, we want to do it. It’s a little more complicate, but it’s not that hard.

We have two possibilities:

1. Define several characters for different angles, e.g:

      if (c == 'Q')            
      {
        myservo.write(0);
      } 
      else if (c == 'W')
      {
        myservo.write(10);
      }
      else if (c == 'E')
      {
        myservo.write(20);
      }
      else if (c == 'R')
      {
        myservo.write(30);
      }

Then you have to know the characters of each angle and you’re not flexible. You can’t tell the servo to go to 13°. But it’s a really fast way and sometimes enough for little things.

2. “Use strings instead of chars” – This is very flexible and you can create your own syntax. Actually you not use real strings, you know how many chars a command has and you store it in a array. If the incoming char does not match with your syntax – you delete your array and everything starts again.

I wanted to control a servo without defining several characters for different angles, i wanted to create my own syntax like “S112#” to move the servo to the exact position (the number after ‘S’ is the angle and ‘#’ says that the command has ended).
Here now I explain shortly how the communication works and then how to use your own syntax.

#include  

#define charS 'S'      //defining our initial char for the command as constant
Servo myservo;
char c;

char S_data[3];           //servo command data -> 3 is the length of the command syntax
byte S_index = 0;         //servo command index

char cmdTemp;             // which will be used to know which command was sent

void setup() {
    Serial.begin(9600);          // baud 9600
    myservo.attach(3);           // using pin 3 for the servo
    Serial.write("Power On\n");  // to know when its ready
    myservo.write(90);           // you have a range between 0°-180°
  }
void loop()                       
{
    if (Serial.available() > 0) // Here we have to use IF!
    {                           // if there is an incoming byte to this:
      c = Serial.read();           // save it to the variable 'c'
      if (c == charS)            // if you received your initial command char
      {                          // 'S' in this case
        Serial.println("received: 'S'");
        cmdTemp = charS;         // to know that the following data should be a servo angle
        S_index = 0;             // 0 data received, only initial command char
      } 
      else if (c == '#')        // if you receive '#', store 'e' (like ended) in cmdTemp
      {
        Serial.println("received: '#'");
        cmdTemp = 'e';           // you can't use '#' in this case, because of our if-statements
      }
      
      
      if (cmdTemp == charS && c != charS){    //if cmdTemp was 'S' and current command != 'S'
        Serial.println("added one char to S_data");
        S_data[S_index] = c;                  // store current cmd in S_data[index]
        S_index++;                            // add index + 1
      }
      else if (cmdTemp == 'e'){               // if cmdTemp == 'e' so command has ended
        Serial.println("command ended");
        myservo.write(atoi(S_data));          // write S_data to servo
                                              // atoi ignores if char is not integer
        Serial.println(atoi(S_data));
        delay(10);                            // the servo needs some time for moving
      }
    }  
}

If we type “S112#” into the serial monitor we get this output and my servo moves to position 112:
Power On
received: 'S'
added one char to S_data
added one char to S_data
added one char to S_data
received: '#'
command ended
112

You obviously can extend your codes with defining more commands.

Have a nice day!