Variable optimized away?

I have a sketch that isn’t working correctly. There is a test (justpressed) that is not working correctly. It is always testing as true when in fact it is false. I loaded the sketch in to an AVR Compiler and got the same results but it has a Watch window and an immediate window (is there something similar for Arduino?) and when I got to justpressed it showed i as being optimized away and if I plug in justpressed I get the message optimized away. If I change the settings to no optimization, then It runs properly… Any idea why this would happen? :sob:

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

byte buttons[] = {21, 22, 23, 24, 25, 26, 27, 27, 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[]={13,14,15,16}; // 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' 
byte pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[NUMBUTTONS]={};

void setup() {
  byte i;
  
// Set LEDs pins as out put and turn off
  for(int 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);
    digitalWrite(buttons[i], HIGH);
  }

  // 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
     lasttime = millis();
  }
  
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return; 
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  
  for (index = 0; index < NUMBUTTONS; index++){
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    /*     
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  byte numOn=0;
  byte comboCheck=0;
  for (byte i = 0; i < 12; i++){ //only check first 12 buttons
  
    if (justpressed[i]) {
      justpressed[i] = 0;
      LEDState[i]=!LEDState[i]; //toggle LED Status
      digitalWrite(LEDs[i], !digitalRead(LEDs[i])); //Toggle LED
      for (byte j =0; j < 12; j++) { //count the number of LEDs lit
        if (LEDState[j]) {
          numOn++;  
        }
      }
      if (numOn==5){
        for (byte i=0; i<5; i++){
        if (LEDState[combo1[i]])
          comboCheck++;
        }
        if (comboCheck==5){// Correct combo reached
        // to do
        }
        else {// wrong combo
          for (int i = 0; i< 12; i++){
            digitalWrite(LEDs[i],LOW);
            LEDState[i]=false;         
          }
        //Call second routine  
        }
          
      

      }
    }



  }

}

Make ALL the variables modified by the ISRs and shared with non-ISR code be "volatile."

volatile byte pressed[NUMBUTTONS]={}, justpressed[NUMBUTTONS]={}, justreleased[NUMBUTTONS]={};
SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

Variables used in an ISR should be declared volatile.

And you don't escape from this rule by calling a function from the ISR.

westfw put it more accurately:

Make ALL the variables modified by the ISRs and shared with non-ISR code be "volatile."

What's this for

justpressed[NUMBUTTONS]={}

If you don't want to initialise it, then don't.

justpressed[NUMBUTTONS];

Volatile did the trick! Thanks!