Counter for patterns not wrapping at last index

Hey guys,

I've got an array of addressable LEDs coming off an UNO and a self wrapping counter for the patterns i am sending to them.

Once i go from Cylon to Sinelon, I can't seem to come back. I'm not sure if this is bounce (I'm using INPUT_PULLUP). It seems like Sinelon seems to be doing the same thing more elegantly, anyway, though with the orientation of my LEDs (kind of a Knight Rider's K.I.T. look), it's nice to have that drag because it looks like each LED cell (that holds 3) is it's own light.

Any suggestions?

#include "FastLED.h"
#include  <LiquidCrystal_I2C.h>
#include  <Wire.h>

FASTLED_USING_NAMESPACE

#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    5
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS    48
#define buttonPin 2
#define BRIGHTNESS         180
#define FRAMES_PER_SECOND  250
#define BACKLIGHT PIN       13

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); /* FOR LCD: SDA PINA4; SCL PIN A5 */

CRGB leds[NUM_LEDS];

//--------------------------------------------------------------------------------------------------------------

void setup() 
  {
    lcd.begin(16,2);
    LEDS.addLeds<WS2812B,DATA_PIN,RGB>(leds,NUM_LEDS);
    LEDS.setBrightness(84);
    pinMode (buttonPin, INPUT_PULLUP);
    delay(100); // 3 second delay for recovery

 
    // tell FastLED about the LED strip configuration
    FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
    //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

    // set master brightness control
    FastLED.setBrightness(BRIGHTNESS);
  }

/* patterns fo cycle through */

typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { sinelon, cylon };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
 
void fadeall() 
  { 
  for(int i = 0; i < NUM_LEDS; i++) 
  { leds[i].nscale8(215); } 
  }

//--------------------------------------------------------------------------------------------------------------
 
void loop()
  {
    static uint8_t gHue = 0;
    gPatterns[gCurrentPatternNumber]();
    FastLED.show(); 
    FastLED.delay(400/FRAMES_PER_SECOND);
    EVERY_N_MILLISECONDS( 50 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  
    if (digitalRead(buttonPin) == LOW)
      {
        nextPattern();
        delay(10);
      }
    if (gCurrentPatternNumber == 0)
      {
        lcd.clear();
        lcd.print("Pattern: Cylon");
      }
    else if (gCurrentPatternNumber == 1)
      {
        lcd.clear();
        lcd.print("Pattern: Sinelon");
      }
  }

//--------------------------------------------------------------------------------------------------------------

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) /* counter for pattern # */

void nextPattern()
  {
    gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns); /*current pattern # + 1,wrap around to end */
  }

void sinelon()
  {
    fadeToBlackBy( leds, NUM_LEDS, 20);
    int pos = beatsin16(13, 0, NUM_LEDS-1 );
    leds[pos] = CHSV( 160, 255, 255);
  }

void cylon()
    { 
      static uint8_t hue = 0;
      for(int i = 0; i < NUM_LEDS; i++) //  slide LEDs to one direction
      
        {
          leds[i] = CHSV(hue++, 255, 255);
          FastLED.show(); 
          fadeall();
          delay(10);
        }
 
      for(int i = (NUM_LEDS)-1; i >= 0; i--)   // other direction
      
        {
          leds[i] = CHSV(hue++, 255, 255);
          FastLED.show();
          fadeall();
          delay(10);
        } 
}

Several things.
Check that ARRAY_SIZE returns what you’re expecting.
Read up on the modulo % operator to loop around your index value
Read up on the switch-case method of controlling program flow