 # logical x between ..

I’ve got an int x = analogRead()
that prints the value 508
I test if it lies in the interval between 506 and 510

if( ( x <= 510 ) && ( x >= 506 ) ){

… and it’s rejected …

Have you got some fresh eyes on this?

99 times out of 100, the problem with code is not in the bit you think it is.

We'd need to see that whole section of code. (The two bits are correct as far as I can see, so there must be something else)

This…

``````int x;

void setup()
{
if(  ( x <= 510 ) && ( x >= 506 ) )
{
}
}

void loop()
{
}
``````

compiles ok.

What do mean by

… and it’s rejected …

?

``````void JoyStick(){
// Serial.println("Joy") ; // this part  showes
// delay(5);
Serial.print("x ") ;
Serial.println(x) ; // a proper value (508) displayes
delay(5);
long Xdelay ;

if(  ( x <= 510 ) && ( x >= 506 ) ){
Serial.println("ewX") ;
delay(5);
}
else{
Serial.println("else ewX") ;//this is executed for x=508
delay(5);
if( x <= 510 ){
Xdelay = x*1500 ;
pen.ewX.destination = pen.ewX.stepCounter + 1 ;
}
if( x >= 506 ){
Xdelay = ( 1024 - x )*1500 ;
pen.ewX.destination = pen.ewX.stepCounter - 1 ;
}
pen.ewX.setDelay(Xdelay) ;
}// end if(  ( x
``````

I think he means that a value of 508 fails the “if” test, altho it’s <=510 and >= 506.

I’d put some serial prints in and around the “if” to see where the code’s actually going. Edit… ah our posts crossed, I see you did that already

I just compared that to some code of mine and I’m not sure if you need the double brackets.

You have:

``````if(  ( x <= 510 ) && ( x >= 506 ) ){
Serial.println("ewX") ;
delay(5);
``````

In a similar piece of code I have:

``````if (results.value == 3148 || results.value == 1100) {
``````

so analogously, yours should maybe be:

``````if(  x <= 510  &&  x >= 506  ){
Serial.println("ewX") ;
delay(5);
``````

That code seems to work for me, or rather a slightly modified version as I dont have the pen.ewX code:

``````void setup(){
Serial.begin(115200);
// Serial.println("Joy") ; // this part  showes
// delay(5);
int x = 508 ;
Serial.print("x ") ;
Serial.println(x) ; // a proper value (508) displayes
delay(5);
long Xdelay ;

if(  ( x <= 510 ) && ( x >= 506 ) ){
Serial.println("ewX") ;
delay(5);
}
else{
Serial.println("else ewX") ;//this is executed for x=508
delay(5);
if( x <= 510 ){
Xdelay = x*1500 ;
Serial.println('+');
}
if( x >= 506 ){
Xdelay = ( 1024 - x )*1500 ;
Serial.println('-');
}
}
}

void loop(){

}
``````

Prints out:
x 508
ewX

I just compared that to some code of mine and I’m not sure if you need the double brackets.

No, they are not needed. However, they completely eliminate ambiguity, causing no problems. So, they are a good idea.

OP, it would be interesting to print the value of x again in the else block. It does not appear that it should get into the else block, but that if test is hard for me to read.

Logically, I think that if you want to test that a value is between two values, you start with testing that it is larger than or equal to the smaller value, then test whether it is lower than or equal to the larger value, as you described the situation is words. That is, no one says “if x is between 510 and 506, do something”. It is always expressed as “if x is between 506 and 510, do something”.

Converting that to code should, in my opinion, maintain the same order of testing.

Of course, none of that really matters, since you really only want to do something if x is less than 506 or greater than 510.

``````if(x < 506)
{
// Do the less than stuff
}
else if(x > 510)
{
// Do the greater than stuff
}
else
{
// x is between 506 and 510 inclusive
}
``````

Hi guys I'm sorry for the inconvinience, but I posted a "problem solved" yesterday ... it does not appear to have reached the forum. I fouled up slightly different x and z values, so the problem was not the logic.