So this is weird

So I haven’t programmed in quite some time, and am trying to get back into it with my new arduino. So I coded a program that accepts inputs from four buttons, compares it to a predefined sequence and, if the buttons were pressed in the right order, some action is then performed. I used a bunch of serial.print and serial.println statements to do debugging with and, instead of commenting them out, I use a debugging flag to trigger whether they get executed or not. This might not save space, but it does make turning debugging on an off a lot easier. However, here is the problem. When debugging is turned off, there is some code that should still cause the LED to blink when a correct code is entered, but it doesn’t do it. When debugging is on, the LED blinks. When I turn debugging off, it doesn’t blink to give me feedback anymore. I tried this with commenting out the serial.print lines as well and its the same result. The code is pasted below. Any ideas why this might be happening? Thanks in advance.

—begin—

/* 
 The circuit:
 * Red pushbutton attached from pin 4 to +5V; 10K resistor attached from pin 4 to ground
 * Blue pushbutton attached from pin 5 to +5V; 10K resistor attached from pin 5 to ground
 * Green pushbutton attached from pin 6 to +5V; 10K resistor attached from pin 6 to ground
 * Yellow pushbutton attached from pin 7 to +5V; 10K resistor attached from pin 7 to ground
 * LED connected to pin 13 or uses onboard LED
 
*/

// When true, serial comms will occur to assist debugging
boolean debugging = false;

// Debounce time, in millisenconds
#define DEBOUNCE 10

// The buttons that we're using, and their associated pin numbers
#define RED 4      // the input number of the red button
#define BLUE 5     // the input number of the blue button
#define GREEN 6    // the input number of the green button
#define YELLOW 7   // the input number of the yellow button

// An array storing the pin numbers of connected buttons, and compute the array size
byte buttons[] = {RED, BLUE, GREEN, YELLOW};
#define NUMBUTTONS sizeof(buttons)

// Arrays to store button state
byte pressed[NUMBUTTONS];
byte justpressed[NUMBUTTONS];

// This is where we define the secret code, then compute its length
byte SecretCode[] = {RED, GREEN, BLUE, YELLOW};
int codeSize=sizeof(SecretCode);  // remember, sizeof() is the # of BYTES used to store the indicated item 
int NoCorrect = 0;    // when this equals codeSize, the code has been entered correctly

const int led = 13;      // the number of the LED pin for output

void setup() {

  // initialize serial communication at 9600 bits per second if debugging
  if (debugging) {
    Serial.begin(9600);
  }
  
  byte i;
    
  // Set LED pin to output
  pinMode(led, OUTPUT);
 
  // Set button pins to inputs
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
  }
  
  //blink LED twice for one second to signify that system is ready for input
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
}

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++) {
    justpressed[index] = 0;       // when we start, we clear out the "just" indicators
      
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    previousstate[index] = currentstate[index];   // Remember button state for next time
  }
}

void loop() {

  //First we check the switches to get their current states
  check_switches();

  // For each button, check to see if it was just pressed and, if so, compare to next in code sequence
  for (byte i = 0; i < NUMBUTTONS; i++) {
    if (justpressed[i] && buttons[i] == SecretCode[NoCorrect]) {
      NoCorrect++;      // correct button pressed, now look for next in sequence
      
      // blip the led once to show correct during TESTING
      if (debugging) {
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(250);                // wait for a quarter second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        Serial.println("So far so good:");
        Serial.print("Button on digital input ");
        Serial.print(buttons[i]);
        Serial.println(" was pressed.");
        Serial.print("Number correct so far is ");
        Serial.println(NoCorrect);
        Serial.print("Code size is ");
        Serial.println(codeSize);
      }
      
      // Because last entry was correct, let's see if entire code has been correctly entered.
      if (NoCorrect == codeSize) {
        
        // blink three quick times to show that correct code was entered
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(250);               // wait for a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        delay(250);
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(250);               // wait for a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        delay(250);
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);               // wait for a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        
        if (debugging) {
          Serial.println("Correct!");
        }
        
        // *** Insert code here for whatever should happen when secret code is entered correctly ***
    
        NoCorrect = 0;   // Reset code sequence to start
      }
        
    } else if (justpressed[i] && buttons[i] != SecretCode[NoCorrect]) {
      NoCorrect = 0;    // wrong button pressed, reset code sequence to start

      // blip the led twice to show incorrect, but only during testing
      if (debugging) {
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(250);                // wait for half a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        delay(250);        
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(250);                // wait for half a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        Serial.println(buttons[i]);
        Serial.println("Ouch, wrong button...");
      }
    }
  }
}

—end—

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I think the problem is that without the Serial.print() statements the check_switches() function gets called again almost immediately... less than the debounce time. The entries in the "justpressed" array don't get cleared so those same buttons are processed again and don't match the secret code. Try adding lines to clear the flags as they are processed.

     NoCorrect++;      // correct button pressed, now look for next in sequence
     justpressed[i] = false;  // Clear the flag so the button does not get processed twice
    else if (justpressed[i] && buttons[i] != SecretCode[NoCorrect]) {
      NoCorrect = 0;    // wrong button pressed, reset code sequence to start
      justpressed[i] = false;  // Clear the flag so the button does not get processed twice

Yep, that did the trick, thanks. It was that return; in the middle of the loop. My old teacher always told me that was a bad idea...

Thanks again.