Control motor outputs with If Else statement

Hello all,

I am trying to control a line following differential drive robot using an If Else statement and mapping certain values to the PWM outputs.

However I am not able to change the values the motors take (they stay on the values given in the first part of the statement, 200 for both)

In the serial monitor I can see the value the sensor reads change, so I know the problem is not there.

Here is the code

//Code for Line Following Robot project, 2nd Year MEng
//Robot has to follow a white line on dark floor, drive up a ramp and push a tin can off the top without 
//going off the end.
//Uses output of Wheatstone bridge to control differential drive


const int sensorMin = 0;
const int sensorMax = 1023;
int leftMotor=3; //Left motor pin
int rightMotor=11; //Right motor Pin
int leftValue = 0; //Intial Motor Value
int rightValue = 0; //Initial Motor Value
int tipMotor=10; //Tip motor pin
int analogPin = 0; //Sensor Head Input
int sensorValue = 0; //Sensor Value
int range = 0; //Range of values for switch loop
int sensorGain = 0; //Sensor value after gain is applied

void setup() {
  // initialize the Left Motor and Right Motor pins as outputs:
  pinMode(leftMotor, OUTPUT);
  pinMode(rightMotor, OUTPUT); 
  // initialize serial communications:
  Serial.begin(9600);
}

void loop () {
  
   
  // read the analog in value:
  sensorValue = analogRead(analogPin);
 
 
  if (400<sensorValue<=600) 
{ 
  leftValue = 200;
  rightValue = 200;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (600<sensorValue<=700) 
{ 
  leftValue = 200;
  rightValue = 140;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
} 
else if (700<sensorValue<=800) 
{ 
  leftValue = 200;
  rightValue = 100;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (800<sensorValue<=900) 
{ 
  leftValue = 200;
  rightValue = 70;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (900<sensorValue<=1023) 
{ 
  leftValue = 200;
  rightValue = 40;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (300<sensorValue<=400) 
{ 
  leftValue = 200;
  rightValue = 140;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (200<sensorValue<=300) 
{ 
  leftValue = 200;
  rightValue = 100;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (100<sensorValue<=200) 
{ 
  leftValue = 200;
  rightValue = 70;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}
else if (0<sensorValue<=100) 
{ 
  leftValue = 200;
  rightValue = 40;
  analogWrite (leftMotor, leftValue);
  analogWrite (rightMotor, rightValue);
}


 // print the results to the serial monitor:

  Serial.print(sensorValue);
  Serial.print("\t");
  Serial.print(leftValue);
  Serial.print("\t");
  Serial.println(rightValue); 
 
 delay(200);


}

Thanks!

  if (400<sensorValue<=600)

Wrong. That is NOT how to test that a value is in a range. You can test that a variable is above one value && below another. (That was a hint).

Ah thank you - so I would need to say if (sensorValue>400 && sensorValue<=600)? using the boolean logical and?

Yes.

And I'd also recommend putting serial prints into each leg of your "if" so you can see where the logic takes you if you need further de-bugging.