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();
}