Servo not changing with if statement

I'm trying to move a servo when the value received is 1 to a certain position and if the value received is 2, it should move to a new position. It is not working even though the hc12 is receiving the values correctly.
Thank you in advance.

#include <NeoSWSerial.h>
#include <ServoTimer2.h>

ServoTimer2 shift;
ServoTimer2 steer;
ServoTimer2 motor;

String input;
NeoSWSerial hc12(5,6); //TX,RX


int steerold=1493;
int speedold=800;
int x;
int y;
int z;
const char coma=',';

void setup() {

shift.attach(9);
steer.attach(10);
motor.attach(11);

pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
hc12.begin(9600);

}

void loop() {

while (hc12.available()){
  input=hc12.readStringUntil('\n');
 
  
   
    Serial.println(input);

    x=input.indexOf(coma);
    int shiftVal=input.substring(0,x).toInt();
    y=input.indexOf(coma,x+1);
    int steernew=input.substring(x+1,y).toInt();
    z=input.indexOf(coma,y+1);
    int speednew=input.substring(y+1,z).toInt();

//this part not working
//servo staying in only one postition and not changing
    if (shiftVal == 1){
      shift.write(0);
    }

    if (shiftVal == 2){
      shift.write(50);
    }
//rest working fine

    if (abs(steernew-steerold)>=10){
      steerold=steernew;
      steer.write(steerold);
    }

    if (abs(speednew-speedold)>=20){
      speedold=speednew;
      motor.write(speedold);
    }

    delay(10);
   
  }

}

I suspect the problem is that what you have in input is a character rather than a number.

Try this

    if (shiftVal == '1'){
      shift.write(0);
    }

    if (shiftVal == '2'){
      shift.write(50);
    }

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

...R

ServoTimer2.write() acts like Servo.writeMicroseconds() NOT Servo.write(). It requires a pulse width between 750 - 2250 microseconds not a 0-180 angle like Servo.h. So when you write(0) and write(50) they are both translated to the equivalent of writeMicroseconds(750) which explains why the servo doesn't move.

Steve

slipstick:
ServoTimer2.write() acts like Servo.writeMicroseconds() NOT Servo.write(). It requires a pulse width between 750 - 2250 microseconds not a 0-180 angle like Servo.h. So when you write(0) and write(50) they are both translated to the equivalent of writeMicroseconds(750) which explains why the servo doesn't move.

Steve

Yesssss, I totaly forgot about that, thank you so much.