Auto"snake" on FastLED matrix

So Ive made a host of animations for a 16 by 16 FastLED matrix. One I want is a "snake" that goes around the screen by itself. The code seems to work however after some time it goes crazy and stops working. I cant find why in the code. Any suggestions... Pixel uses x y coordinates and returns the pixel number btw

  void snake()
  {
    if (snakerunning == false)
    {
      FastLED.clear();
      dir = 0;
      lastdir = 0;
      randomstartx = random(0, 16);
      randomstarty = random(0, 16);
      for (int i = 0; i < SNAKELENGTH; i++)
      { //saving snake posistion in a matrix for later
        snakepixels[i] = pixel(randomstartx, randomstarty + i);
      }
      snakerunning = true;
    }

    int randomtime = random(100, 500);
    int randomtime2 = random(0, 60);
    while (snakerunning == true)
    {
      EVERY_N_MILLISECONDS(100)
      {
        drawsnake();
        switch (dir)
        {
        case 0:
          randomstarty--;
          if (randomstarty < 0)
          {
            randomstarty = 15;
          }
          break;
        case 1:
          randomstarty++;
          if (randomstarty > 15)
          {
            randomstarty = 0;
          }
          break;
        case 2:
          if (randomstartx > 15)
          {
            randomstartx = 0;
          }
          break;
        case 3:
          randomstartx--;
          if (randomstartx < 0)
          {
            randomstartx = 15;
          }
          break;
        }
      }
      EVERY_N_MILLISECONDS(randomtime)
      {
        dir = random(4);
        if (lastdir == 0 || lastdir == 1)
        {
          dir = random(2, 4);
        }
        if (lastdir == 2 || lastdir == 3)
        {
          dir = random(2);
        }
        lastdir = dir;
      }
      EVERY_N_SECONDS(10)
      {
        snakerunning = false;
        FastLED.clear();
        for (int i = 0; i < SNAKELENGTH; i++)
        { //blanking snake pixels for the next run
          snakepixels[i] = 0;
        }
      }
    }
  }
  void drawsnake()
  {
    FastLED.clear();
    for (int i = SNAKELENGTH - 1; i > 0; i--)
    {
      snakepixels[i] = snakepixels[i - 1];
    }
    snakepixels[0] = pixel(randomstartx, randomstarty);
    for (int i = 0; i < SNAKELENGTH; i++)
    {
      leds[snakepixels[i]] = CHSV((255 / SNAKELENGTH) * i, 255, 255);
    }
    FastLED.show();
  }

Please post a complete sketch showing how the variables are declared and how the functions are called

My best guess would be that you are writing outside the bounds of an array

nothing obvious in that part of the code... Maybe the bug is in the rest of the code, or the power or something you did not share

this case look a bit suspicious compared to the others as you don't modify randomstartx but that would not be the cause for a crash.

unconventional. can you post the definition?

        randomstartx = random(0, 16);
        randomstarty = random(0, 16);
        for (int i = 0; i < SNAKELENGTH; i++)
        { //saving snake posistion in a matrix for later
            snakepixels[i] = pixel(randomstartx, randomstarty + i);

if "randomstarty" is 16, is "randomstarty + i" guranteed to be within range of snakepixels?

EVERY_N_MILLISECONDS is defined in the FastLED library. It is a preprocessor macro which is replaced by a block of code when compiled.

I think to remember people had issues with the hidden variable it generates. I would advise to use millis() and your own variables...

Thanks for this suggestion. It was trying to write outside the array. During the first for loop when I write the values to the snake pixels I didnt allow for when randomstarty+i goes above the size of the array in the y direction. Ive changed that statement now to roll round back to 0.

agree. makes it hard to follow
why make it hard for reader to understand what the code is doing?

That was the issue, watching the code again it only broke when the snake started at the bottom edge of the matrix. Ive added lines to solve the rollover.