# Simple condition is wrongly evaluated

Hi. I’m working on a wirelessly controlled Arduino robot. I’m currently working on the remote controller part.
I’m using 2 joysticks to control 2 DC motors on the robot. (only the X position of the joystick is used)
I’ve written a simple function that takes the joystick’s X position, maps it between 0 and 255 and decides the direction of each motor.
I noticed that the Arduino is reading small values (like 1, 2, 3), so I decided to add an If statement to my function. It just checks if the reading is between -5 and 5 to eliminate noise. The problem is that this if statement is executed even if the reading is out of this range.

For example `-5 <= leftX <= 5` is true even if `leftX` is 24.

Here’s my function:

``````void calcMotorSpeeds(int leftX, int rightX){
if(-5 <= leftX <= 5){
//This code always executes
leftDirection = 0;
leftS = 0;
return;
}
if(-5 <= rightX <= 5){
//This code always executes
rightDirection = 0;
rightS = 0;
return;
}
if(leftX > 0){
leftDirection = 1;
} else if (leftX < 0){
rightDirection = 2;
} else {
rightDirection = 0;
rightS= 0;
return;
}
if(rightX > 0){
leftDirection = 1;
} else if (rightX < 0){
rightDirection = 2;
} else {
rightDirection = 0;
rightS = 0;
return;
}
leftS = map(abs(leftX), 0, 100, 0, 255);
rightS = map(abs(rightX), 0, 100, 0, 255);
}
``````

leftDirection, rightDirection, rightS, leftS are global variables.
I’m using a joystick library.

The first part -5 <= leftX eveluates to 0 or 1 (false or true), which is always <= 5, so the whole thing indeed will evaluate as true at all values of leftX.

This will work a lot better:

``````  if(leftX >= -5 && leftX <= 5)
``````

Thanks! Now it works fine. You can do this in Python, not in C++