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);
}
}