if (var >0 ){...} Execution enters the if statement, even when var == 0!

Hi all,

I am facing a very strange problem with a pretty plain statement: if (var >0 ). Execution enters the if statement, even when var == 0! :o

I am building up a basic DCC command station for controlling model trains; user commands are given via a KEYES infrared remote control; there is a little (2x16) LCD display, to show the user the current loco address, speed and direction.
The user inputs the loco address using the numeric keypad on the IR control, chooses the loco direction (forward/reverse) via the right/left arrow, increases the train speed with the UP arrow, and decreases it with the DOWN arrow.

The full sketch is 280+ lines long, so maybe for now it is better to post only the relevant portion of the code.

This is a part of the infrared commands detection routine: when the UP arrow is pressed, a "speed" variable is increased; when the DOWN arrow il pushed, the variable is decreased; obviously, when "speed" reaches 0 (the train stops), no further decrement should be done, because a negative speed would be meaningless. Moreover, to save memory i declared the speed variable as BYTE, because the DCC protocol defines only 128 speed steps, so a single byte will suffice. So I am checking if speed is still greather than zero, to avoid decrementing it when it reaches the zero value.

And here it is my problem: the variable get decremented even if it is equal to zero (causing it to raise again to 255, of course)!! below the code:

(...)
    byte speed = 0;
(...)
    case ARROW_UP :
      {
      
      Serial.print(F("Arrow UP pressed. speed before:" )); 
      Serial.println(speed);     
        if (speed <= 126)
        {
          speed++;
        }
        lcd.setCursor(7,1);       
        lcd.print(speed);
        Serial.print(F("speed after: ")); 
        Serial.println(speed);
        break;
        
      }
    case ARROW_DOWN :
      {
         Serial.print(F("Arrow DOWN pressed. speed before:" )); 
          Serial.println(speed); 
        if (speed > 0);
        {
             Serial.print("speed > 0? "); 
              Serial.println(speed > 0); 
            Serial.print(F("speed inside IF: ")); 
           Serial.println(speed); 
          speed--;
        }         
        
         Serial.print(F("speed after: ")); 
         Serial.println(speed);
         lcd.setCursor(7,1);
        lcd.print(speed);
       
        break;
      }
(...)

As you can see, I put some "displays" to check the variable values on the serial monitor. Here what I am collecting:

Arrow UP pressed. speed before:0
speed after: 1
Arrow UP pressed. speed before:1
speed after: 2
Arrow UP pressed. speed before:2
speed after: 3
Arrow DOWN pressed. speed before:3
speed > 0? 1
speed inside IF: 3
speed after: 2
Arrow DOWN pressed. speed before:2
speed > 0? 1
speed inside IF: 2
speed after: 1
Arrow DOWN pressed. speed before:1
speed > 0? 1
speed inside IF: 1
speed after: 0
Arrow DOWN pressed. speed before:0
speed > 0? 0
speed inside IF: 0
speed after: 255
Arrow DOWN pressed. speed before:255
speed > 0? 1
speed inside IF: 255
speed after: 254
Arrow UP pressed. speed before:254
speed after: 254
Arrow UP pressed. speed before:254
speed after: 254
Arrow UP pressed. speed before:254
speed after: 254

you see? even when speed has a value of zero, the code enter the If (speed > 0) statement, and speed gets decremented once more. As a triple-check, I am printing the vaue of the expression itself (speed > 0) and it is evaluated to 0, correctly.

So what am I doing wrong?

thanks in advance,

Gianfranco

if (speed > 0);

get rid of the ;
.

Your if (condition );

syntax is terminated with empty code block with semicolon and is missing the "what to do" code and is the same as

if (speed > 0)
{
do nothing , no code
}
so it appears as the if(0 is always executed.

:o
I coudn't see that... what a stupid mistake!!! two hours wasted... :confused:

Thank you very much, and please excuse me for the silly question.

iw1dov:
:o
I coudn't see that... what a stupid mistake!!! two hours wasted... :confused:

Thank you very much, and please excuse me for the silly question.

No problem, that is why using AutoFormater with right options ( put brackets on separate line AFTER header) sometime, not always, helps.
Cheers

And one lesson for (many) people to take away: If you think the compiler is making a mistake, you're almost certainly wrong. It's your code...

Regards,
Ray L.