Using 'NeoPatterns' with a momentary push button to cycle led patterns

I’ve been doing some research on how I can reliably update looping led patterns using a momentary push button.
I’ve found this multitasking NeoPatterns introduction and studied them all night to see how I can modify it to use for my project. (multitasking neopixels here: Overview | Multi-tasking the Arduino - Part 3 | Adafruit Learning System)

I can’t seem to change the button workflow to one that would switch the led patterns from case 0, 1, 2, 3 and back to 0. Tried to implement the state change button example sketch.
It won’t play the patterns and the serial print doesnt show it’s changing the case patterns either.
It does seem to do some weird behaviour, after a bit of time being on, it’ll do the colorwipe and invert direction after button press(from completion function).
I feel like i’m missing something extremely obvious.

I use an Arduino nano, 50 pixels long ws2812b led strip connected to data pin 5, and a momentary push button connected to data pin 2 with a 10k resistor to ground.

Code is attached, setup/loop etc are all the way to the bottom as the patterns are defined first according to the NeoPatterns code.

colorbuttoncycler.ino (8.05 KB)

Please insert your code into a post, between code tags, rather than as an attached file. Many if not most form members use phones & tablets to read the forum most of the time, and cannot open a .ino file.

PaulRB:
Please insert your code into a post, between code tags, rather than as an attached file. Many if not most form members use phones & tablets to read the forum most of the time, and cannot open a .ino file.

It doesn't fit within the 9k character limit unfortunately.

Ok, that's a problem, because no-one is going to want to read through 9K+ of code to spot the error. Can you reduce the size of the sketch by replacing the 4 presumably quite long pattern functions with 4 simple short ones? For example 4 simple chase patterns in different colours? If it still does not work, hopefully that code will be short enough to post. If it does work, that tells us something important in itself. If that happens, replace one of the simple patterns with one of your complex patterns and test again. If that also works, try each of your other 3 complex patterns in turn with 3 simple patterns. Hopefully one of the 4 will turn out to be the "rogue" that causes the problem, and the sketch will still be short enough to post.

PaulRB:
Ok, that's a problem, because no-one is going to want to read through 9K+ of code to spot the error. Can you reduce the size of the sketch by replacing the 4 presumably quite long pattern functions with 4 simple short ones? For example 4 simple chase patterns in different colours? If it still does not work, hopefully that code will be short enough to post. If it does work, that tells us something important in itself. If that happens, replace one of the simple patterns with one of your complex patterns and test again. If that also works, try each of your other 3 complex patterns in turn with 3 simple patterns. Hopefully one of the 4 will turn out to be the "rogue" that causes the problem, and the sketch will still be short enough to post.

Thanks! I have solved the issue so far thankfully. The code is super long due to the multitude of color patterns set up (from the post on adafruit ive been using) true that. I've not had time yet to make my own patterns that would make it shorter.

The Adafruit post used a latched button to switch to another specific pattern (in void loop), the only thing I had to do was change that section to momentary button (from buttoncycler/state change example sketches) and have it switch to the NEXT pattern (and go back to the first pattern in line after reaching the end of the pattern set and another button press).

Took almost entire day to understand most of it but I think I do now. The patterns themselves were fine to run btw, it was mostly that I wanted to use a momentary push button to swap to the next pattern that wouldnt work out.

(this is just a snippet of the loop)

// Main loop
void loop()
{
    // Update the ring.
    Ring1.Update();

    buttonState = digitalRead(BUTTON_PIN);  //read the button
    
    if (buttonState != lastButtonState) { //if button is pressed
      buttonState = digitalRead(BUTTON_PIN);  //read the button again
      if (buttonState == HIGH){// Button still pressed
         
      buttonCounter++;
      
      if (++mode > 3)  {
        mode = 0;
      }
      switch(mode) {
        case 0: 
          Ring1.ActivePattern = COLOR_WIPE;
          break;
        case 1: 
          Ring1.ActivePattern = THEATER_CHASE;
          break;
        case 2: 
          Ring1.ActivePattern = FADE;
          break;
        case 3: 
          Ring1.ActivePattern = RAINBOW_CYCLE;
          break;
      }
      
      Serial.println("on"); //print button is on
      Serial.print("number of button pushes: "); 
      Serial.println(buttonCounter); //print the amount of buttons pushed

      Serial.print("current pattern: "); 
      Serial.println(mode); 

      delay(50);    //Delay a little bit to avoid bouncing

      }
      else {//Back to normal operation
        
        Serial.println("off"); //if the current state is LOW then the button went from on to off
        delay(50);    //Delay a little bit to avoid bouncing

      }

   }
 lastButtonState = buttonState;
}
if (buttonState != lastButtonState) { //if button is pressed

The comment does not match the code on that line. The code is not checking if the button is pressed. It's checking if either the button is pressed now but wasn't before or is not pressed now but was before. In other words, the button's state has just changed.

buttonState = digitalRead(BUTTON_PIN);  //read the button again

That is pointless. Almost certainly the button state is still the same. And even if it isnt, it will be picked up in the next execution of loop() anyway. You can remove that line.

What you do need to worry about is the button bouncing. Buttons are mechanical devices with springs. When a button is pressed, it can bounce several times, like a ball dropped onto hard floor. But with buttons, it happens too fast for us humans to perceive, but the arduino can and might change the mode 2 or more times when we only wanted one change. A simple delay(20), after the first change is detected, is normally enough to fix the problem.