Why pin49 isn't turning off in my code?

pin 49 is never turning off. it doesn’t turn off when I press the button to activate ‘square16solid’ function, and it never turns off during ‘square16’ function.

I pasted the main parts to my code since it was over the 9500 limit. Thanks!

#define DEBOUNCE 10

// output pins
int inMin = 2;
int inMax = 49;

//input pins
byte button[16]= {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
               
#define NUMBUTTONS sizeof(button)

byte buttonpressed[NUMBUTTONS], pressed[NUMBUTTONS];

boolean state[16];

boolean lastState[48];

boolean oldState[16];
  
long previousMillis[48];
     
long interval[48] = {500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550,
                    500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550, 500, 600, 550,
                  500, 600, 550, 500, 600, 550};

long time[48];
 
void setup() { 
  
  byte i;

Serial.begin(9600);  

  for(int i=inMin; i<inMax; i++)
{
   pinMode(i, OUTPUT);
 
}   

for(int i=0; i<15; i++)
{
   buttonpressed[i] = 0;
   state[i] = LOW;
   pinMode(button[i], INPUT);
   oldState[i] = LOW;
}


for(int i=0; i<47; i++)
{
   previousMillis[i] = 0;
   time[i] = millis();
   lastState[i] = LOW;
}

}

void check_switches() {
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;
  
  if (millis() < lasttime){
    lasttime = millis();
} 

  if ((lasttime + DEBOUNCE) > millis()) {
    return;
  }

lasttime = millis();

for (index = 0; index < NUMBUTTONS; index++) {
buttonpressed[index] = 0;

currentstate[index] = digitalRead(button[index]);
if(currentstate[index] == previousstate[index]) {
  
  if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
    
    buttonpressed[index] = 1;
  }
  pressed[index] = !currentstate[index];
}
previousstate[index] = currentstate[index];
}
}


void loop() 
{
  
  check_switches();
 ////////////Square 1/////////////////
     if (oldState[0] != buttonpressed[0])  // check for state change
  {
    if(buttonpressed[0]) // if button state is HIGH, then do something
    {
      state[0]++;
      if (state[0] > 1)
      {
        state[0] = 0;
      }
    }
    oldState[0] = buttonpressed[0]; // update lastState
  }
  if (state[0] == 0)
  {
    square1();
  }

  else if (state[0] == 1)
  {
    square1solid();
  }
 
    ////////////Square 16/////////////////
     if (oldState[15] != buttonpressed[15])  // check for state change
  {
    if(buttonpressed[15]) // if button state is HIGH, then do something
    {
      state[15]++;
      if (state[15] > 1)
      {
        state[15] = 0;
      }
    }
    oldState[15] = buttonpressed[15]; // update lastState
  }
  if (state[15] == 0)
  {
    square16();
  }

  else if (state[15] == 1)
  {
    square16solid();
  }
}




/////////////////////////////////Square 1///////////////////////////////////

void square1solid() {
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);}



void square1(){
  unsigned long currentMillis1 = millis();
   if (currentMillis1 - time[0] > interval[0]) {
     time[0] = currentMillis1;
     
        if(lastState[0] == LOW)
          lastState[0] = HIGH; 
        else 
          lastState[0] = LOW;
          
          digitalWrite(2, lastState[0]);
   }
       
       
  unsigned long currentMillis2 = millis();
  
   if (currentMillis2 - time[1] > interval[1]) {
     time[1] = currentMillis2;
     
        if(lastState[1] == LOW)
          lastState[1] = HIGH;
          
        else 
          lastState[1] = LOW;
   
          digitalWrite(3, lastState[1]);
    }
    
     unsigned long currentMillis3 = millis();
  
   if (currentMillis3 - time[2] > interval[2]) {
     time[2] = currentMillis3;
     
        if(lastState[2] == LOW)
          lastState[2] = HIGH;
          
        else 
          lastState[2] = LOW;
          
          digitalWrite(4, lastState[2]);
          
   }
}
         /////////////////////////Square 16////////////////////////////////
void square16solid() {
  digitalWrite(47, HIGH);
  digitalWrite(48, LOW);
  digitalWrite(49, LOW);}
   
   
void square16(){
 unsigned long currentMillis46 = millis();
   if (currentMillis46 - time[45] > interval[45]) {
     time[45] = currentMillis46;
     
        if(lastState[45] == LOW)
          lastState[45] = HIGH; 
        else 
          lastState[45] = LOW;
          
          digitalWrite(47, lastState[45]);
   }
       
       
  unsigned long currentMillis47 = millis();
  
   if (currentMillis47 - time[46] > interval[46]) {
     time[46] = currentMillis47;
     
        if(lastState[46] == LOW)
          lastState[46] = HIGH;
          
        else 
          lastState[46] = LOW;
   
          digitalWrite(48, lastState[46]);
    }
    
     unsigned long currentMillis48 = millis();
  
   if (currentMillis48 - time[47] > interval[47]) {
     time[47] = currentMillis48;
     
        if(lastState[47] == LOW)
          lastState[47] = HIGH;
          
        else 
          lastState[47] = LOW;
          
          digitalWrite(49, lastState[47]);
          
   }

Mattdryer:
pin 49 is never turning off. it doesn’t turn off when I press the button to activate ‘square16solid’ function, and it never turns off during ‘square16’ function.

I pasted the main parts to my code since it was over the 9500 limit. Thanks!

Did you set that pin to output?

for(int i=inMin; i<inMax; i++)
{
   pinMode(i, OUTPUT);
int inMax = 49;

i

Yeah i thought the way I wrote it was okay.

With inmax at 49, wouldn't it get to 48, it will be less than 49 so it will add one to get to 49? Or is it supposed to be 50?

Or:

for(int i=inMin; i <= inMax; i++)

Matt, the loop tests the condition and then operates the index… that’s why it doesn’t get to 10 here.

So in the following example the loop runs ten times.

the i variable is set to 0.
a condition is created for the loop to run if the “i < 10” test is successful.
the counter i is increased by 1 each time the loop runs

  for(int i = 0; i < 10 ; i++)
  { 
    Serial.println(i);
  }

outputs this:

0
1
2
3
4
5
6
7
8
9

this syntax by the way makes it particularly nice for arrays, for example:

int myArray[10];

where the first element is zero, not one. It is easy to match the array size(10) to the for loop syntax

BullDog, thanks a lot for your help. I never had anyone explain it to me that way, makes complete sense (and would explain a lot of other past code issues I've had haha). I'm clearly a noob but I love learning this stuff.

You've helped me a lot with this project and I've learned a ton, thank you!!