# If statement not working

I have a sketch that the if statement isn’t working correctly. I have a global variable roundNum and it is set in one part of the code to 2 but when i run the sketch it doesn’t seem to work. I ran it in Simulator for Arduino and got some weird results. The test for if (roundNum==0) will revert to true if I set roundNum to 2 in the variable window so I think that it might be doing the same in the real Arduino.
If I set roundNum to 2 immediately before the test then it works…maybe my variable is never getting set to 2? I don’t see how it wouldnt,

``````#define DEBOUNCE 10  // button debouncer, how many ms to debounce, 5+ ms is usually plenty

byte buttons[] = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}; // the pin #s of the buttons
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)

byte LEDs[]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20}; //the pin #s of LED pins
boolean LEDState[NUMBUTTONS]={}; //keep track of LED state Set all to False
int LEDOn=0; //to keep track of how many LEDs are currently lit....Do we need? check array?
byte combo1[]={0,1,2,3,4}; // The buttons that make up the combination in the first puzzle
byte combo2[]={12,13,14,15}; // The buttons that make up the combination in the second puzzle

// we will track if a button is just pressed, just released, or 'currently pressed'
volatile byte pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[]={};
volatile byte i;
byte roundNum=0;

void setup()
{
// Set LEDs pins as out put and turn off
for(i=0; i < NUMBUTTONS; i++)
{
pinMode(LEDs[i], OUTPUT);
digitalWrite(LEDs[i], LOW);
}

// Make button pins as input & enable pull-up resistors on switch pins
for (i=0; i < NUMBUTTONS; i++)
{
pinMode(buttons[i], INPUT_PULLUP);
}

//set up trigger pin
pinMode(41, OUTPUT);
digitalWrite(41, LOW);
/*
// Run timer2 interrupt every 15 ms
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //1024 prescaler

//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
*/

}
/*
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
*/

void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;

if (millis() < lasttime)// we wrapped around, lets just try again.. do we need with timer of?
{
lasttime = millis();
}

if ((lasttime + DEBOUNCE) > millis()) // not enough time has passed to debounce do we need with timer of
{

return;
}
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
lasttime = millis();

for (index = 0; index < NUMBUTTONS; index++)
{

if (currentstate[index] == previousstate[index]) // two readings the same 10ms apart
{
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) // just pressed
{
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) // just released....Is this needed?
{
justreleased[index] = 1;
}
pressed[index] = !currentstate[index];  // Button state has changed so update button status
}
previousstate[index] = currentstate[index];   // stores last state to see if it stays the same after debounce period
}
}

void loop()
{
byte numOn=0;
byte comboCheck=0;
check_switches();

if (roundNum==0) //this is always testing true????
{
for (i = 0; i < 12; i++)//only check first 12 buttons
{
if (justpressed[i])
{
justpressed[i] = 0;
LEDState[i]=!LEDState[i]; //toggle LED Status
for (byte j =0; j < 12; j++) //count the number of LEDs lit
{
if (LEDState[j])
{
numOn++;
}
}
if (numOn==5)
{
for (byte k=0; k<5; k++)
{
if (LEDState[combo1[k]])
comboCheck++;
}
if (comboCheck==5)// Correct combo reached
{
roundNum=2; //is this ever happening?
}
else // wrong combo
{
for (byte k = 0; k< 12; k++)
{
digitalWrite(LEDs[k],LOW);
LEDState[k]=false;
}
}
}
}
}
}
if (roundNum==2)
{
for (byte j =12; j < 20; j++) //count the number of LEDs lit
{
if (LEDState[j])
{
numOn++;
}
}
if (numOn==4)
{
for (byte k=0; k<4; k++)
{
if (LEDState[combo2[k]])
{
comboCheck++;
}
if (comboCheck==4)// Correct combo reached
{
digitalWrite(41,HIGH);
}
else // wrong combo
{
for (byte k = 12; k< 20; k++)
{
digitalWrite(LEDs[k],LOW);
LEDState[k]=false;
}
}
}
}
}
}
``````
``````volatile byte pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[]={};
``````

Still with the useless initialization sections…

``````/*
// Run timer2 interrupt every 15 ms
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20; //1024 prescaler

//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1<<TOIE2;
*/
``````

We REALLY do not need to see commented out crap.

``````        if (comboCheck==5)// Correct combo reached
{
roundNum=2; //is this ever happening?
Serial.print("roundNim WAS set to 2!"); // Why not find out?
}
``````

Print the value of comboCheck immediately after the line comboCheck ++;

...R

Paul,

I was tired and forgot to clean the code up...

Robin,

Thanks.... Well I was relying on the simulator to work correctly for debugging purposes, but it didn't. So I put in a bunch of print statements and found the problem.