4x4x4 LED cube Snake

Hey! I have a problem with my code. The snake starts at 2 leds long, but for some reason the snake doesn't move and there's a gap in the snake... any idea why? I can't find anything

Thanks in advance!

All variables:

//Global Variables(I have more parts that uses these)
int pins[16] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37};
int pins2[4][4] = {{22, 23, 24, 25}, {26, 27, 28, 29}, {30, 31, 32, 33}, {34, 35, 36, 37}};
int lvls[4] = {38, 39, 40, 41};

//All snake variables
int heads[8][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int length = 2;
int nextLevel = 3;
int pin = NULL;
int currentX = NULL;
int currentY = NULL;
int currentZ = NULL;
int targetX = NULL;
int targetY = NULL;
int targetZ = NULL;
int foodX = NULL;
int foodY = NULL;
int foodZ = NULL;
int bufferX = NULL;
int bufferY = NULL;
int bufferZ = NULL;
int headBuffer = NULL;
boolean trueX = false;
boolean trueY = false;
boolean trueZ = false;
boolean currentTrueX = false;
boolean currentTrueY = false;
boolean currentTrueZ = false;
boolean currentAllTrue = false;
boolean food = false;
boolean ateFoodBool = false;
boolean target = false;

The main code:

void snake()
{
  if (!food)
  {
    Serial.println("food");
    foodX, foodY, foodZ = spawnFood(NULL, NULL, NULL);
  }
  if (!target)
  {
    Serial.println("target");
    targetX, targetY, targetZ = setTargetPos(targetX, targetY, targetZ);
  }
  Serial.println("display");
  display1();
  Serial.println("pathFineOneMove");
  currentX, currentY, currentZ = pathFindOneMove(currentX, currentY, currentZ, targetX, targetY, targetZ);
  Serial.println("moveSnake");
  moveSnake(NULL, NULL);
  Serial.println("ateFood");
  ateFoodBool = ateFood(ateFoodBool);
  if (ateFoodBool)
  {
    Serial.println("addHead");
    length, nextLevel = addHead(length, nextLevel);
    food = false;
    ateFoodBool = true;
  }
  
}

int pathFindOneMove(int currentX, int currentY, int currentZ, int targetX, int targetY, int targetZ)
{
  if (currentX < targetX)
  {
    currentX++;
  }
  else
  {
    if (currentX != targetX)
    {
      currentX--;
    }
    else
    {
      currentTrueX = true;
    }
  }
  if (currentY < targetY)
  {
    currentY++;
  }
  else
  {
    if (currentY != targetY)
    {
      currentY--;
    }
    else
    {
      currentTrueY = true;
    }
  }
  if (currentZ < targetZ)
  {
    currentY++;
  }
  else
  {
    if (currentZ < currentZ)
    {
      currentY--;
    }
    else
    {
      currentTrueZ = true;
    }
  }
  
  if (currentTrueX = true)
  {
    if (currentTrueY = true)
    {
      if (currentTrueZ = true)
      {
        currentAllTrue = true;
      }
    }
  }
  
  return currentX, currentY, currentZ;
}

void display1()
{
  for (int y = 0; y < 25; y++)
  {
    //-------------------Display Snake
    for (int h = 0; h < length; h++)
    {
      digitalWrite(lvls[heads[h][1]], HIGH);
      digitalWrite(pins2[heads[h][0]][heads[h][2]], HIGH);
      delay(1);
      digitalWrite(pins2[heads[h][0]][heads[h][2]], LOW);
      digitalWrite(lvls[heads[h][1]], LOW);
    }
  
    //-------------------Display Food
    digitalWrite(lvls[foodY], HIGH);
    digitalWrite(pins2[foodX][foodZ], HIGH);
    delay(1);
    digitalWrite(pins2[foodX][foodZ], LOW);
    digitalWrite(lvls[foodY], LOW);
  }
}

int coordToLed(int x, int z, int pin)
{
  pin = (x * z)-1;
  return pin;
}

int spawnFood(int foodX, int foodY, int foodZ)
{
  foodX = NULL;
  foodY = NULL;
  foodZ = NULL;
  foodX = random(1,5);
  foodY = random(1,5);
  foodZ = random(1,5);
  foodX--;
  foodY--;
  foodZ--;
  food = true;
  ateFoodBool = false;
  return foodX, foodY, foodZ;
}

int setTargetPos(int targetX, int targetY, int targetZ)
{
  targetX = NULL;
  targetY = NULL;
  targetZ = NULL;
  targetX = random(1,5);
  targetY = random(1,5);
  targetZ = random(1,5);
  targetX--;
  targetY--;
  targetZ--;
  target = true;
  return targetX, targetY, targetZ;
}

int addHead(int length, int nextlevel)
{
  int e = nextLevel;
  for (int d = length; d <= 0; d--)
  {
    for (int f = 0; f < 3; f++)
    {
      heads[e][f] = heads[d][f];
    }
    e--;
  }
  
  currentX, currentY, currentZ = pathFindOneMove(currentX, currentY, currentZ, targetX, targetY, targetZ);
  
  for (int g = 0; g < 3; g++)
  {
    if (g = 0)
    {
      heads[0][g] = currentX;
    }
    
    if (g = 1)
    {
      heads[0][g] = currentY;
    }
    
    if (g = 2)
    {
      heads[0][g] = currentZ;
    }
  }
  length++;
  nextLevel++;
  return length, nextLevel;
}

boolean ateFood(boolean ateFoodBool)
{
  for (int i = 0; i < 3; i++)
  {
    if (heads[0][i] = foodX)
    {
      trueX = true;
    }
    if (heads[0][i] = foodY)
    {
      trueY = true;
    }
    if (heads[0][i] = foodZ)
    {
      trueZ = true;
    }
    
    if (trueX && trueZ && trueZ)
    {
      return ateFoodBool = false;
    }
  }
  return ateFoodBool;
}

int moveSnake(int lenght, int nextlevel)
{
  int e = nextLevel;
  for (int d = length; d <= 0; d--)
  {
    for (int f = 0; f < 3; f++)
    {
      heads[e][f] = heads[d][f];
    }
    e--;
  }
  
  currentX, currentY, currentZ = pathFindOneMove(currentX, currentY, currentZ, targetX, targetY, targetZ);
  for (int g = 0; g < 3; g++)
  {
    if (g = 0)
    {
      heads[0][g] = currentX;
    }
    
    if (g = 1)
    {
      heads[0][g] = currentY;
    }
    
    if (g = 2)
    {
      heads[0][g] = currentZ;
    }
  }
  
  for (int g = length+1; g > 7; g++)
  {
    for (int h = 0; h < 3; h++)
    {
      heads[g][h] = NULL;
    }
  }
}

any idea why? I can't find anything

Running out of memory perhaps ?
Why are the majority of your variables ints when byte would suffice and save space ?

Running out of memory perhaps ?
Why are the majority of your variables ints when byte would suffice and save space ?

The reason why pretty much all of my variables are ints, is that I have to keep track of what the different things are and where they are in the cube, so I can do that more quickly if it is in int's and not bytes

But it's not memory I have the atmega1280 and I have used 6 736 bytes of a 126 976 byte maximum. So space is no problem

Space may be a problem if you're using too much RAM

I have to keep track of what the different things are and where they are in the cube, so I can do that more quickly if it is in int's and not bytes

Why is it quicker to use ints rather than bytes ? It sounds counter intuitive.

UKHeliBob:
Why is it quicker to use ints rather than bytes ? It sounds counter intuitive.

Probably thinks you mean ASCII string representation.

No, the reason I don't do bytes is that I (the programmer, as in the person programming) can see where the different thing are in my cube since I use a 3 coord system (x, y, z).

the reason I don't do bytes is that I (the programmer, as in the person programming) can see where the different thing are in my cube since I use a 3 coord system (x, y, z).

The cube is only 4 units on each side. If you really wanted to you could fit x, y and z into a single byte, so why the need for individual ints (2 bytes each) for x, y and z ?

Can you explain what this does ?

currentX, currentY, currentZ = pathFindOneMove(currentX, currentY, currentZ, targetX, targetY, targetZ);

Are you expecting the pathFindOneMove() function to return 3 values ?

You could fit x, y and z into a single "unsigned int", and still have room for a 4 bit t dimension :wink: