Button Not always working with changing LED's.

Anyone have any idea if I’m doing something wrong here? I’m a noob with programming.

I’ve got 16 rgb led strips that change color and a button for each. Each button when pressed turns the strip a solid color. Press it again and it goes back to changing colors. When I press the button it usually changes the color solid right away. When I try to change it back to color-changing by pressing the button again, it doesn’t always work. I have to press it multiple times before it activates correctly.

Again, I’m new to all this programming stuff so I figure maybe what I’m doing is a simple fix.

// output pins
int inMin = 1;
int inMax = 48;

//input pins
const byte button[]= {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13,
               A14, A15};
              
int buttonPress[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  
long previousMillis[48] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
     
long interval[48] = {500, 600, 500, 500, 600, 500, 1000};
 
boolean state1, lastState1 = LOW;
boolean state2, lastState2 = LOW;
boolean state3, lastState3 = LOW;
boolean state4, lastState4 = LOW;
boolean state5, lastState5 = LOW;
boolean state6, lastState6 = LOW;
boolean state7, lastState7 = LOW;
boolean state8, lastState8 = LOW;
boolean state9, lastState9 = LOW;
boolean state10, lastState10 = LOW;
boolean state11, lastState11 = LOW;
boolean state12, lastState12 = LOW;
boolean state13, lastState13 = LOW;
boolean state14, lastState14 = LOW;
boolean state15, lastState15 = LOW;
boolean state16, lastState16 = LOW;



 
void setup() { 

Serial.begin(115200);  

  for(int i=inMin; i<=inMax; i++)
{
   pinMode(i, OUTPUT);
}   
 
 for(int i = 0; i<= 16; i++)
 {
  pinMode(button[i], INPUT_PULLUP);
  } 
}



void loop() 
{

  
  
  
  
  //strip 1
  
state1 = digitalRead(button[0]);
state2 = digitalRead(button[1]); 
    
 if(state1 != lastState1)
 {
   lastState1 = state1;
 }

 if(state1 == HIGH && buttonPress[0] == 0)
 {
   digitalWrite(1, HIGH);
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   buttonPress[0]++;}
     
     
  else if (state1 == 0 && buttonPress[0] == 0)
  {
    
 unsigned long currentMillis1 = millis();
   
   if (currentMillis1 - previousMillis[0] >= interval[0]) {
     previousMillis[0] = currentMillis1;
     
         digitalWrite(1, !digitalRead(1));
       }
       
  unsigned long currentMillis2 = millis();
   
   if (currentMillis2 - previousMillis[1] >= interval[1]) {
     previousMillis[1] = currentMillis2;
     
         digitalWrite(2, !digitalRead(2));}
  
  unsigned long currentMillis3 = millis();
   
   if (currentMillis3 - previousMillis[2] >= interval[2]) {
     previousMillis[2] = currentMillis3;
     
         digitalWrite(3, !digitalRead(3));}
       
   }
   
   else  if (state1 == 1 && buttonPress[0] == 1)
  {buttonPress[0] = 0;}
     
     
     
     
  //strip 2      
    
 if(state2 != lastState2)
 {
   lastState2 = state2;
}

 if(state2 == HIGH && buttonPress[1] == 0)
 {
   digitalWrite(4, HIGH);
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   buttonPress[1]++;}
     
     
  else if (state2 == 0 && buttonPress[1] == 0)
  {
    
 unsigned long currentMillis4 = millis();
   
   if (currentMillis4 - previousMillis[3] >= interval[3]) {
     previousMillis[3] = currentMillis4;
     
         digitalWrite(4, !digitalRead(4));
       }
       
  unsigned long currentMillis5 = millis();
   
   if (currentMillis5 - previousMillis[4] >= interval[4]) {
     previousMillis[4] = currentMillis5;
     
         digitalWrite(5, !digitalRead(5));}
  
  unsigned long currentMillis6 = millis();
   
   if (currentMillis6 - previousMillis[2] >= interval[5]) {
     previousMillis[2] = currentMillis6;
     
         digitalWrite(6, !digitalRead(6));}
       
   }
   
   else  if (state2 == 1 && buttonPress[1] == 1)
  {buttonPress[1] = 0;}
}

try this.

 if(state1 != lastState1)
 {
 if(state1 == HIGH && buttonPress[0] == 0)
 {
   digitalWrite(1, HIGH);
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   buttonPress[0]++;}
     
     
  else if (state1 == 0 && buttonPress[0] == 0)
  {   
   lastState1 = state1;
 }

there is the code for my own project with a button . basically, you need to read the button value (HIGH or LOW), test if it's different than the last button value, inside this test, test again if it's HIGH or LOW, then you got the real button condition right.

buttonState[4]=digitalRead(buttonPin[4]);  // toggle from mode(1)-mode(4)
if(buttonState[4]!=lastButtonState[4]){
  if(buttonState[4]==LOW){
    mode++;
    if(mode==5)mode=1;
}
}
lastButtonState[4]=buttonState[4];

for the

lastButtonState[4]=buttonState[4];

if nothing happen to the buttonState, lastButtonState is alway equals to lastButtonState. if something happened to ButtonState, then lastButtonState equal to ButtonState. lastButtonState here is for detection. you can put it anywhere in the code.

 if(state1 != lastState1)
 { 
     
 if(state1 == HIGH && buttonPress[0] == 0)
 {
   digitalWrite(1, HIGH);
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   buttonPress[0]++;}
     
  else if (state1 == 0 && buttonPress[0] == 0)
  {
    lastState1 = state1;
  }

The color turns solid now, and changes only when the button is pressed :confused: