How come this if construction won't be executed?

Hi everyone,

I've been working on a simple piece of code but i just can't get it working.
The problem is an if construction which has or has not to do something dependent on two switch positions.
The code, simplified, is as follows:

// Declare vars
      byte ifCheck      ;
const byte switchA = 12 ;
const byte switchB = 13 ;

void setup() 
{
  pinMode(switchA, INPUT) ;
  pinMode(switchB, INPUT) ;
  Serial.begin(9600)      ;  
}

void loop() 
{
  // read switches
  bool switchARead = digitalRead(switchA) ;
  bool switchBRead = digitalRead(switchB) ;

  // If both switches are off, Do...
  if(!switchARead && !switchBRead)
      { 
        byte ifCheck = 1 ;
      }

  // Print the readings to serial
  Serial.println(switchARead, DEC) ;
  Serial.println(switchBRead, DEC) ;
  Serial.println(ifCheck, DEC    ) ;
}

So when i disable both switches, i create two falses/zeros/lows so the if statement is true and thus it should be executed. The serial monitor should display 0, 0, 1. (CR between)
That doesn't happen though. The serial displays 0, 0, 0.
If i enable one, the other, or both switches, i get 1,0,0 or 0,1,0 or 1,1,0.

So the serial does confirm that both switches are read (as 0 in this case), but the if statement is still not executed and so the ifCheck remains unset/0. This also confirms that it is a code problem and has nothing to do with wiring. I suppose it doesn't matter how i write it, but to be sure i also tried if(switchA == 0), if(switchA == LOW), and if(switchA == false) which all return the same, as expected. How come? Is, unlike the logical order, the var ifCheck changed back to 0 before serial prints it or something?

It's probably just an obvious stupid little thing i'm missing right now, so please point me at it.. :s

Thanks for the help

TR_7:
The code, simplified, is as follows:

How about the actual code that caused the problem. Can we see that? What if you didn't copy the error over when you simplified this?

if(!switchARead && !switchBRead)
      { 
        byte ifCheck = 1 ;
      }

That defines a new variable named ifCheck. It is not the same variable you defined earlier in the sketch, but it has the same name. That's a very bad idea because this one goes out of scope and vanishes as soon as the if block is complete. In reality, the compiler probably recognized this and removed that code entirely. Lose the "byte" and you'll have what I think you want.

Delta_G:

if(!switchARead && !switchBRead)

{
        byte ifCheck = 1 ;
      }




That defines a new variable named ifCheck.

XD thx i don’t know why i didn’t see that when i posted this…
Such a nasty little thing, i was looking over it the whole time.
Thank you for pointing it out, it works now!

I notice you are not using INPUT_PULLUP. If you use INPUT and dont supply a pullup resistor, then when the switch is open you are going to get random results on the input pin.

But the reason your code doesn't work is that you are declaring a new variable named ifCheck inside your if condition, and that new variable is hiding the variable outside the if condition.