LED tetris HELP

Hi all.
im currently using arduino UNO and a 16x16 RG led matrix to run LED TETRIS.
But the problem now we have is after we play more than 30 seconds, the T block is going werid when we rotate it.
other function runs well but only that T block is getting anoying and we cant figure out why this is happening..
is it because of lack of memory in UNO?
we wrote the block program with triple array and we think this is the problem...
sorry for bad english lol

more over, we have add some features in this tetris game. such as The Hold Queue, The Next Queue, Ghost Piece.

I'm also making a Tetris game on a simple 8x8 dot matrix. I might be able to help you, post your code. Please use the code tags, it's the # sign above the smiley faces.

char currentMatrix[16][16] = { \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},\
};
/*char Map[16][16] = { \
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0},\
};// 16x16 LED ????? ???? ??*/
char holdMatrixtmp[4][4];

#define D  { \
    {  0, 129, 129, 129, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129, 129, 129, 129,   0}, \
}
    

#define Y  { \
    {  0, 129,   0,   0,   0, 129,}, \
    {  0, 129,   0,   0,   0, 129,}, \
    {  0,   0, 129,   0, 129,   0,}, \
    {  0,   0,   0, 129,   0,   0,}, \
    {  0,   0,   0, 129,   0,   0,}, \
    {  0,   0,   0, 129,   0,   0,}, \
}


#define G  { \
    {  0,   0, 129, 129, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0,   0}, \
    {  0, 129,   0, 129, 129, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0,   0, 129, 129, 129,   0}, \
}


#define A  { \
    {  0,   0,   0, 129,   0,   0}, \
    {  0,   0, 129,   0, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129, 129, 129, 129, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
}


#define M  { \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129, 129,   0, 129, 129}, \
    {  0, 129,   0, 129,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
}


#define E  { \
    {  0, 129, 129, 129, 129, 129}, \
    {  0, 129,   0,   0,   0,   0}, \
    {  0, 129, 129, 129, 129,   0}, \
    {  0, 129,   0,   0,   0,   0}, \
    {  0, 129,   0,   0,   0,   0}, \
    {  0, 129, 129, 129, 129, 129}, \
}


#define O  { \
    {  0,   0, 129, 129, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0,   0, 129, 129, 129,   0}, \
}


#define V  { \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0,   0, 129,   0, 129,   0}, \
    {  0,   0,   0, 129,   0,   0}, \
}


#define R  { \
    {  0, 129, 129, 129, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
    {  0, 129, 129, 129, 129,   0}, \
    {  0, 129,   0, 129,   0,   0}, \
    {  0, 129,   0,   0, 129,   0}, \
    {  0, 129,   0,   0,   0, 129}, \
}

//???? ??? ??1 - ?? ; 2 - ??; 3 - ??
#define Z1 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 1, 0, 0}, \
    {0, 1, 1, 0}, \
}
#define Z2 { \
    {0, 0, 0, 0}, \
    {0, 0, 1, 0}, \
    {0, 1, 1, 0}, \
    {0, 1, 0, 0}, \
}
#define S1 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {0, 1, 1, 0}, \
    {1, 1, 0, 0}, \
}
#define S2 { \
    {0, 0, 0, 0}, \
    {1, 0, 0, 0}, \
    {1, 1, 0, 0}, \
    {0, 1, 0, 0}, \
}
#define J1 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 1, 1, 0}, \
    {0, 0, 1, 0}, \
}
#define J2 { \
    {0, 0, 0, 0}, \
    {0, 1, 1, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 0, 0}, \
}
#define J3 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 0, 0, 0}, \
    {1, 1, 1, 0}, \
}
#define J4 { \
    {0, 0, 0, 0}, \
    {0, 0, 1, 0}, \
    {0, 0, 1, 0}, \
    {0, 1, 1, 0}, \
}
#define L1 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 1, 1, 0}, \
    {1, 0, 0, 0}, \
}
#define L2 { \
    {0, 0, 0, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 1, 0}, \
}
#define L3 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {0, 0, 1, 0}, \
    {1, 1, 1, 0}, \
}
#define L4 { \
    {0, 0, 0, 0}, \
    {0, 1, 1, 0}, \
    {0, 0, 1, 0}, \
    {0, 0, 1, 0}, \
}
#define T1 { \
    {0, 0, 0, 0}, \
    {0, 1, 0, 0}, \
    {1, 1, 1, 0}, \
    {0, 0, 0, 0}, \
}
#define T2 { \
    {0, 0, 0, 0}, \
    {0, 1, 0, 0}, \
    {1, 1, 0, 0}, \
    {0, 1, 0, 0}, \
}
#define T3 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 1, 1, 0}, \
    {0, 1, 0, 0}, \
}
#define T4 { \
    {0, 0, 0, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 1, 0}, \
    {0, 1, 0, 0}, \
}
#define I1 { \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {1, 1, 1, 1}, \
    {0, 0, 0, 0}, \
}
#define I2 { \
    {0, 1, 0, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 0, 0}, \
    {0, 1, 0, 0}, \
}
#define O1 {  \
    {0, 0, 0, 0}, \
    {0, 0, 0, 0}, \
    {0, 1, 1, 0}, \
    {0, 1, 1, 0}, \

}

First, this is how we set up the blocks..

void StartEffect()
{
 int i,j,k;
 for(i=0;i<32;i++)
 {
  if(i<16)
  {
   for(j=0;j<8;j++)
    for(k=0;k<i+1;k++)
    {
     currentMatrix[j*2][k] = 1;
     currentMatrix[j*2+1][15-k] = 2;
    }
  }
  else
  {
   for(j=0;j<8;j++)
    for(k=0;k<i-15;k++)
    {
     currentMatrix[j*2][k] = 0;
     currentMatrix[j*2+1][15-k] = 0;
    }
  }
  Effectdisplay();
 }
}

void StartEffect2()
{
  int i,j,k,z,z2;
 for(i=0;i<46;i++)
 {
  if(i<16)
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
   {
    if(i+j-16>0) currentMatrix[5+k][j] = StartLetter[z][k][z2];
   }
  }
  else if(i>30)
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
   {
    if (i+j-16 <= 30) currentMatrix[5+k][j] = StartLetter[z][k][z2];
   }
  }
  else
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
    currentMatrix[5+k][j] = StartLetter[z][k][z2];
  }
  Effectdisplay();
 }
}

/*void OneLC() {
  for(int j=0; j<10; j++)
    currentMatrix[15][j] = 0;
  for(int i=14; i>=0; i--)
    for(int j=0; j<10; j++)
      if(currentMatrix[i][j] == -1) currentMatrix[i+1][j] = -1;
  for(int j=0; j<10; j++)
    currentMatrix[0][j] = 0;
}

void TwoLC() {
  for(int j=0; j<10; j++)
  {
    currentMatrix[14][j] = 0;
    currentMatrix[15][j] = 0;
  }
  for(int i=13; i>=0; i--)
    for(int j=0; j<10; j++)
      if(currentMatrix[i][j] == -1) currentMatrix[i+2][j] = -1;
  for(int j=0; j<10; j++)
  {
    currentMatrix[0][j] = 0;
    currentMatrix[1][j] = 0;
  }
}

void ThreeLC() {
  for(int j=0; j<10; j++)
  {
    currentMatrix[14][j] = 0;
    currentMatrix[15][j] = 0;
    currentMatrix[13][j] = 0;
  }
  for(int i=12; i>=0; i--)
    for(int j=0; j<10; j++)
      if(currentMatrix[i][j] == -1) currentMatrix[i+3][j] = -1;
  for(int j=0; j<10; j++)
  {
    currentMatrix[0][j] = 0;
    currentMatrix[1][j] = 0;
    currentMatrix[2][j] = 0;
  }
}

void Arrblock() {
  int count[10] = {0};
  for(int j=0; j<10; j++)
    for(int i=0; i<16; i++)
      if (currentMatrix[i][j] == -1) count[j]++;
  for(int j=0;j<10;j++)
  {
    for(int i=15;i>15 - count[j];i--) currentMatrix[i][j] = -1;
    for(int i=15-count[j]; i>=0; i--) currentMatrix[i][j] = 0;
  }
}
void Clearmap()
{
  for(int i=0; i<16; i++)
    for(int j=0; j<16; j++)
      if(currentMatrix[i][j] == -1) currentMatrix[i][j] = 0;
} */
void Ghost()
{
          int i,j;
   int Ghostcol=currentPieceCol;        //??? ?? ???
          int Ghostrow=currentPieceRow;
   int Ghostcount = 0;                  //?????
   for (i=0;i<4;i++)                                         //??????
    for (j=0; j<4;j++)
     if (currentPiece[j][i]>0)
      if (Ghostrow+j+1<rows)
       if (currentMatrix[Ghostrow+j+1][Ghostcol+i] != -1)
       Ghostcount++;
   if(Ghostcount == 4)                                         
   {
    for(i=0;i<4;i++)
     for(j=0;j<4;j++)
      if ( currentPiece[j][i] > 0)
       currentMatrix[Ghostrow+j+1][Ghostcol+i] = 2;
    Ghostcount = 0;
    Ghostrow++;
    for (int i=0;i<4;i++)
     for (int j=0; j<4;j++)
      if (currentPiece[j][i]>0)
       if (Ghostrow+j+1<rows)
        if (currentMatrix[Ghostrow+j+1][Ghostcol+i] != -1)
         Ghostcount++;
    while(Ghostcount == 4)
    {
                          Ghostcount = 0;
     for(i=0;i<4;i++)
      for(j=0;j<4;j++)
       if ( currentPiece[j][i] > 0) currentMatrix[Ghostrow+j][Ghostcol+i] = 0;
                          for(i=0;i<4;i++)
      for(j=0;j<4;j++)
       if ( currentPiece[j][i] > 0)
        currentMatrix[Ghostrow+j+1][Ghostcol+i] = 2;
                          Ghostrow++;
     for (int i=0;i<4;i++)
      for (int j=0; j<4;j++)
       if (currentPiece[j][i]>0)
        if (Ghostrow+j+1<rows)
         if (currentMatrix[Ghostrow+j+1][Ghostcol+i] != -1)
          Ghostcount++;
    }
                  for(int i=0;i<4;i++)
                      for(int j=0;j<4;j++)
                          if(currentPiece[j][i] > 0) currentMatrix[j+currentPieceRow][i+currentPieceCol] = 1;
   }
}
void GameoverEffect()
{
  int i,j,k,z,z2;
  for(i=0;i<65;i++)
 {
  if(i<16)
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
   {
    if(i+j-16>0) currentMatrix[5+k][j] = gameOverLetters[z][k][z2];
   }
  }
  else if(i>48)
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
   {
    if (i+j-16 <= 48) currentMatrix[5+k][j] = gameOverLetters[z][k][z2];
   }
  }
  else
   for(j=0;j<16;j++)
  {
   z = (i+j-16)/6;
   z2= (i+j-16)%6;
   for(k=0;k<6;k++)
    currentMatrix[5+k][j] = gameOverLetters[z][k][z2];
  }
                for(k=0;k<6;k++)
  Effectdisplay();
 }
}
void clearTrace()                  //?? ?????? ?????(???)? ?? ??(CurrnetMatrix? ???)
{
  for (int i=0;i<4;i++) 
  {
    for (int j=0;j<4;j++)
 {
      if ((currentPieceRow+j>-1 && currentPieceRow+j<rows) && (currentPieceCol+i>-1 && currentPieceCol+i<cols)) //????? y?? -1???? rows?????,
                            //????? x?? -1???? cols?????
   {
        if (currentMatrix[currentPieceRow+j][currentPieceCol+i] == 1)                              // ????? ???? 16? ? ???? ?? 0?? ??
   currentMatrix[currentPieceRow+j][currentPieceCol+i]=0;
      }
    }
  }
}


void updateCurrentMatrix() //CurrnetMatrix? CurrentPiece? ??? ?? ????
{
  for (int i=0;i<4;i++)           //4x4 ??? ?? ??
  {
    for (int j=0;j<4;j++) 
 {
      if ((currentPieceRow+j>-1 && currentPieceRow+j<rows) && (currentPieceCol+i>-1 && currentPieceCol+i<cols)) // ????? y?? -1?? rows????
   {                           // ????? x?? -1?? cols????
        if (currentPiece[j][i]!=0)                                                                              // currentPiece?? ??? ?? ?? ??
   currentMatrix[currentPieceRow+j][currentPieceCol+i] = currentPiece[j][i];                           // currentMatrix? ??
      }
    }
  }
}
void move(int moveDirection) {  //updates: currentPieceRow, currentPieceCol, currentMatrix
  // moveDirection: 2-down; 3-left; 4-right
  if (moveDirection==2) {                         //???? ????
    currentPieceMovedRow=currentPieceRow+1;     //???? MovedRow ????
    currentPieceMovedCol=currentPieceCol;  //x?? ??????? MovedCol? ???
  }
  else if (moveDirection==3) {                   //?? ????
    currentPieceMovedRow=currentPieceRow;            
    currentPieceMovedCol=currentPieceCol-1;
  }
  else if (moveDirection==4) {                  //?? ????
    currentPieceMovedRow=currentPieceRow;
    currentPieceMovedCol=currentPieceCol+1;
  }
  // check if move is valid
  if (!checkmoveable(currentPieceIndex)) {      //??? ????? ????, xy? ??? ?? ??? ???? ?????? ?? 
    clearTrace();
    currentPieceRow=currentPieceMovedRow;                      //y? ? ??  
    currentPieceCol=currentPieceMovedCol;        //x? ? ??
    updateCurrentMatrix();                                     //CurrnetMatrix ??
  }
}


boolean checkmoveable(byte pieceIndex) { //pieceIndex = ????? ??? ??? ??. ?? ??? ????? ?? 
  int row=currentPieceMovedRow;        //??? ?? ???
  int col=currentPieceMovedCol;        //??? ?? ???
  for (int i=0;i<4;i++) {               //4X4??? ???
    for (int j=0; j<4;j++) {
      if (pieces[pieceIndex][j][i]>0) {   // (row+j>-1 && row+j<rows)   ???????? ??? ?? ??? ???
        if (!( row+j<rows && (col+i>-1 && col+i<cols))) return true;  // ??? x,y? ??? ??? true? ??
        else if (currentMatrix[row+j][col+i]<0) return true;  // ?? ??? ????? ???? ??? true??? 
      }
    }
  }
  return false;       //false???? ?????? ??? ??? ??
}


void rotate() {  // updates: currentPieceRotatedIndex, currentPieceIndex, currentPiece, and currentMatrix
  // obtain the currentPieceRotatedIndex
  if (currentPieceIndex==0) return;
  else if (currentPieceIndex<3) currentPieceRotatedIndex=3-currentPieceIndex;
  else if (currentPieceIndex<5) currentPieceRotatedIndex=7-currentPieceIndex;
  else if (currentPieceIndex<7) currentPieceRotatedIndex=11-currentPieceIndex;
  else if ((currentPieceIndex % 4)==2) currentPieceRotatedIndex=currentPieceIndex-3;
  else currentPieceRotatedIndex=currentPieceIndex+1;
  
  currentPieceMovedRow=currentPieceRow;
  currentPieceMovedCol=currentPieceCol;
    
  // check if the rotation is valid 
  if (!checkmoveable(currentPieceRotatedIndex)) {
    clearTrace();
    currentPieceIndex=currentPieceRotatedIndex;
    for (int i=0;i<4;i++) {
      for (int j=0;j<4;j++) {
        currentPiece[j][i]=pieces[currentPieceRotatedIndex][j][i];
      }
    }
    updateCurrentMatrix();
  }
}


boolean checkIfLanded()                              //??? ????? ????? ??
{
  for (int i=0;i<4;i++) 
  {                            //4x4??? ?? ??
    for (int j=0; j<4;j++) 
 {
      if ((currentPieceRow+j>-1 && currentPieceRow+j<rows) && (currentPieceCol+i>-1 && currentPieceCol+i<cols)) //?? ????
   {
        if (currentPiece[j][i] == 1)  //??? ?? ??
  {
          if (currentPieceRow+j==bottomLimit)         //???? ??? ????
     return true;            // if the piece has dropped on the bottom
    else if (currentMatrix[currentPieceRow+j+1][currentPieceCol+i]<0) //???? ????? ???? ????
     return true;  //if there is any deadblock just below the piece
        }
      }
    }
  }
  return false;     //??? ??, ??? ???? ??? ??
}


boolean generatePiece()  // ?????, ? ?? ?????? ??? generate???? ??
{  //updates: currentPiece, currentMatrix, currentPieceRow, currentPieceCol, currentPieceIndex
  byte pieceIndex=NextpieceIndex; 
  for (int i=0; i<4; i++) {
    for (int j=0; j<4; j++) {
      currentPiece[j][i]=pieces[pieceIndex][j][i];// ??? ??? ??? currentPiece? ??
    }
  }
  for (int i=0; i<4; i++) {                             // ??????? ???? ??for?
    for (int j=0; j<2; j++) {
      if (pieces[pieceIndex][j+2][i]>0) {     // ??
        if (currentMatrix[j][i+3]<0) return false;     // ??
      }
    }
  }
  for (int i=0; i<4; i++) {
    for (int j=0; j<2; j++) {
      if (pieces[pieceIndex][j+2][i]>0) {              // ??
        currentMatrix[j][i+3]=pieces[pieceIndex][j+2][i]; // ????
      }
    }
  }
  currentPieceRow = -2;
  currentPieceCol = 3;
  NextpieceIndex = random(7);
    switch (NextpieceIndex) {         //'????? ??' ??? ?? ??
    case 0:
      NextpieceIndex=0;
      break;
    case 1:
      NextpieceIndex=1;
      break;
    case 2:
      NextpieceIndex=3;
      break;
    case 3:
      NextpieceIndex=5;
      break;
    case 4:
      NextpieceIndex=7;
      break;
    case 5:
      NextpieceIndex=11;
      break;
    case 6:
      NextpieceIndex=15;
      break;
  }
  for(int i=0; i<4; i++)
   for(int j=0; j<4; j++)
    currentMatrix[i][j+12] = pieces[NextpieceIndex][i][j];
    currentPieceIndex = pieceIndex;
  return true;
}


void convertToDeadBlock() {               //???? ?? ??? ?? ?????? ???? ??? ????.
  for (int i=0;i<4;i++) {
    for (int j=0;j<4;j++) {
      if (currentPiece[j][i] == 1) {
        currentMatrix[j+currentPieceRow][i+currentPieceCol]=-1;
      }
    }
  }
}

void checkLinesCleared() {
  byte lineErased[rows];
  byte rowCounter=0;
  for (int j=0;j<rows;j++) {
    byte colCounter=0;
    for (int i=0;i<cols;i++) {
      if (currentMatrix[j][i]==0) {
        lineErased[j]=0;
        break;
      }
      else if (currentMatrix[j][i] == -1) colCounter+=1;
    }
    if (colCounter==10) {
      lineErased[j]=1;
      rowCounter+=1;
    }  
  }
  
  if (rowCounter==0) return;  // if no lines are cleared
  
  int tempMatrix[rows-rowCounter][cols];
  byte rowCounter2=0;
  for (int j=0;j<rows;j++) {
    if (lineErased[j]==0) {
      for (int i=0;i<cols;i++) tempMatrix[rowCounter2][i]=currentMatrix[j][i];
      rowCounter2+=1;
    }
  }
  for (int j=0;j<rowCounter;j++) 
  {
    for (int i=0;i<cols;i++) {
      currentMatrix[j][i]=0;
    }
  }
  for (int j=0;j<rows-rowCounter;j++) {
    for (int i=0;i<cols;i++) {
      currentMatrix[j+rowCounter][i]=tempMatrix[j][i];
    }
  }
  numberOfLinesCleared+=rowCounter;
}

 


/*void restartGame() {
  delay(5);
    for (int i=0;i<16;i++) {
      for (int j=0; j<16; j++) currentMatrix[j][i]=0;
    }
    numberOfLinesCleared=0;
    
    // reinitilize variables
    gameRunning=true;
    disableMove=false;
    pieceDropping=false;
    fastDrop= false;
    NextpieceIndex = random(7);
    holdcheck = 1;
    holdIndex = 20;
  //  startcheck = true;
  }*/

unsigned long lastButtonPressTime=millis();
unsigned long time= millis();
// 1-up; 2-down; 3-left; 4-right


void checkUp() {
  time= millis();
  delay(1);
  if (digitalRead(upArrow)==HIGH && time-lastButtonPressTime>110 && disableMove==false && gameRunning==true) {
    rotate();
    RC = 1;
    lastButtonPressTime=millis();
  }
}


void startDropping() {
  time = millis();
  if (digitalRead(downArrow)==HIGH && time-lastButtonPressTime>110 && disableMove==false && gameRunning==true) {
    pieceDropping=true;
    lastButtonPressTime=millis();
  }
}

void hold()
{
  time= millis();
  delay(1);
 if(holdcheck && digitalRead(holdButton)==HIGH && time-lastButtonPressTime>110 && gameRunning==true)
 {
  int i,j,tmp;
                RC = 1;
                lastButtonPressTime=millis();
  if(holdIndex == 20)
  {
   for(i=0;i<4;i++)
    for(j=0;j<4;j++)
                                {
     currentMatrix[5+i][12+j] = currentPiece[i][j];
                                }
                        holdIndex = currentPieceIndex;
                        holdcheck = 0;
                        clearTrace();
                        currentPieceRow = -2;
                        currentPieceCol = 3;
                        generatePiece();
  }
  else
  {
   for(i=0;i<4;i++)
    for(j=0;j<4;j++)
                                {
     holdMatrixtmp[i][j] = currentPiece[i][j];
     currentPiece[i][j] = currentMatrix[5+i][12+j];
     currentMatrix[5+i][12+j] = holdMatrixtmp[i][j];
                                }
   tmp = currentPieceIndex;
   currentPieceIndex = holdIndex;
   holdIndex = tmp;
                        clearTrace();
                        currentPieceRow = -2;
                        currentPieceCol = 3;
   holdcheck = 0;
  }
 }
}
void checkLeft()  {
  time= millis();
  delay(1);
  if (digitalRead(leftArrow)==HIGH && time-lastButtonPressTime>110 && disableMove==false && gameRunning==true) {
    move(3);
    RC = 1;
    lastButtonPressTime=millis();
  }
}


void checkRight() {  
  time= millis();
  delay(1);
  if (digitalRead(rightArrow)==HIGH && time-lastButtonPressTime>110 && disableMove==false && gameRunning==true) {
    move(4);
    RC = 1;
    lastButtonPressTime=millis();
  }
}


void checkDrop() {  
  time= millis();
  delay(1);
  if (digitalRead(drop)==HIGH && time-lastButtonPressTime>110 && disableMove==false && gameRunning==true) {
    while (!checkIfLanded()) move(2);
    disableMove = true;               
      
      convertToDeadBlock();               
      checkLinesCleared();                
      holdcheck = 1;
      if (generatePiece()==false)
      while(1)
          GameoverEffect();
        else disableMove=false;
    RC = 1;
    lastButtonPressTime=millis();
  }
}


//  All Interrupt Functions Go Above Here


void setup() {
  //Serial.begin(9600);


  //  random seed for color selection, direction selection, and food+snakehead generation  
  randomSeed(analogRead(A0));
  byte rand1=random(7);
  rand1=random(7);
  rand1=random(7);
  
  pinMode(output,OUTPUT);
  pinMode(redPin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(latchPin,OUTPUT);    
  pinMode(colClock,OUTPUT);
  pinMode(A0,OUTPUT);
  pinMode(A1,OUTPUT);
  pinMode(A2,OUTPUT);
  pinMode(A3,OUTPUT);
  //////////////////////   
  //  button pins
  pinMode(upArrow,INPUT);
  pinMode(downArrow,INPUT);
  pinMode(leftArrow,INPUT);
  pinMode(rightArrow,INPUT);
  pinMode(holdButton,INPUT);
  PCintPort::attachInterrupt(upArrow, &checkUp, RISING);
  PCintPort::attachInterrupt(downArrow, &startDropping, RISING);
  PCintPort::attachInterrupt(leftArrow, &checkLeft, RISING);
  PCintPort::attachInterrupt(rightArrow, &checkRight, RISING);
  PCintPort::attachInterrupt(holdButton,&hold,RISING);
  PCintPort::attachInterrupt(drop, &checkDrop, RISING);
  
  // talk to attiny85 to play music
  
  //  set all shift register pins to LOW
  digitalWrite(redPin,LOW);
  digitalWrite(greenPin,LOW);
  digitalWrite(colClock,LOW);
  digitalWrite(latchPin,LOW);
  
  //initialize variables
  numberOfLinesCleared=0;
  gameRunning = true;
  disableMove = false;
  fastDrop= false;
}

//  used in the main loop to calculate the time interval between moves
unsigned long lastMoveTime=millis();
unsigned long currentMoveTime=millis();

unsigned long lastLandedTime=millis();
unsigned long currentLandedTime=millis();

void loop() {  
  if (gameRunning==false)     
  {
    displayFrame();
  }
  else                       
  {
    if(startcheck)  
 {
     StartEffect();        
     StartEffect2();
     startcheck = 0;
            for(int i=0;i<16;i++)
            for(int j=0; j<16; j++)
            {
              if(j==10) currentMatrix[i][j] = 12;
              else currentMatrix[i][j] = 0;
            }
              NextpieceIndex = random(7);
            generatePiece();
 }
    displayFrame();       // ??
    currentMoveTime=millis();  // ???? ??
    
    if (fastDrop==true) {
      while (!checkIfLanded()) move(2);
      displayFrame();
      fastDrop=false;
    }
    if (digitalRead(downArrow)==LOW) pieceDropping=false;

    if(!pieceDropping && numberOfLinesCleared < 50) timeInterval = 800 - numberOfLinesCleared*(100-numberOfLinesCleared)/4;
    else timeInterval= 150;

    if (!checkIfLanded() && currentMoveTime-lastMoveTime>timeInterval) 
 {
      move(2);
      lastMoveTime=millis();
    }

    else if (checkIfLanded())                                        
 {
      RC = 0;
      lastLandedTime=millis();                                      

      while (currentLandedTime<lastLandedTime+400)                  
   { 
        currentLandedTime=millis();
        displayFrame();
      }
      if(!RC)
      {
      disableMove = true;                 
      
      convertToDeadBlock();               
      checkLinesCleared();                
      holdcheck = 1;
      if (generatePiece()==false)
      while(1)
          GameoverEffect();
        else disableMove=false;
      }
    }
  }
}
void displayFrame() {
  Ghost();
  byte color;
  for (int j=0; j<16; j++) 
  {                                    
    if (j > 7)
  digitalWrite(A3,HIGH);
 else
  digitalWrite(A3,LOW);
 if((j/4)%2)
  digitalWrite(A2,HIGH);
 else
  digitalWrite(A2,LOW);
 if ((j/2)%2)
  digitalWrite(A1,HIGH);
 else
  digitalWrite(A1,LOW);
 if(j%2)
  digitalWrite(A0,HIGH);
 else
  digitalWrite(A0,LOW);

    for (int i=7; i>-1; i--)                                   
 {
      if (currentMatrix[j][i]==0) color=0;                            
      else if (currentMatrix[j][i]<0) color=3;     
      else if (currentMatrix[j][i]==129) color=textColor;             
      else if (currentMatrix[j][i]==2) color = 2;
      else color=1;
      switch (color)                               
   {                                            
        case 0:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,LOW);
          break;
        case 1:
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,LOW);
          break;
        case 2:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,HIGH);
          break;
        case 3:                              //???????
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,HIGH);
          break; 
      }
      if(currentMatrix[j][i] == 2) currentMatrix[j][i] = 0;
      digitalWrite(colClock,HIGH);
      digitalWrite(colClock,LOW);
    }
    for (int i=15; i>7; i--)                                   
 {
      if (currentMatrix[j][i]==0) color=0;                            
      else if (currentMatrix[j][i]<0) color=3;     
      else if (currentMatrix[j][i]==129) color=textColor;            
      else if (currentMatrix[j][i]==2) color = 2;
      else color=1;
      switch (color)                               
   {                                           
        case 0:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,LOW);
          break;
        case 1:
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,LOW);
          break;
        case 2:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,HIGH);
          break;
        case 3:                              //???????
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,HIGH);
          break; 
      }
      if(currentMatrix[j][i] == 2) currentMatrix[j][i] = 0;
      digitalWrite(colClock,HIGH);
      digitalWrite(colClock,LOW);
    }
    digitalWrite(latchPin,HIGH);
    digitalWrite(latchPin,LOW);
    digitalWrite(output,LOW);
    delay(1);
    digitalWrite(output,HIGH);
  }
}

void Effectdisplay() {
  byte color;
  for (int j=0; j<16; j++) 
  {                                    
    if (j > 7)
  digitalWrite(A3,HIGH);
 else
  digitalWrite(A3,LOW);
 if((j/4)%2)
  digitalWrite(A2,HIGH);
 else
  digitalWrite(A2,LOW);
 if ((j/2)%2)
  digitalWrite(A1,HIGH);
 else
  digitalWrite(A1,LOW);
 if(j%2)
  digitalWrite(A0,HIGH);
 else
  digitalWrite(A0,LOW);


    for (int i=7; i>-1; i--)                                   
 {
      if (currentMatrix[j][i]==0) color=0;                            
      else if (currentMatrix[j][i]<0) color=3;     
      else if (currentMatrix[j][i]==129) color=textColor;             
      else if (currentMatrix[j][i]==2) color = 2;
      else color=1;
      switch (color)                               
   {                                            
        case 0:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,LOW);
          break;
        case 1:
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,LOW);
          break;
        case 2:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,HIGH);
          break;
        case 3:                              //???????
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,HIGH);
          break; 
      }
      digitalWrite(colClock,HIGH);
      digitalWrite(colClock,LOW);
    }
    for (int i=15; i>7; i--)                                   
 {
      if (currentMatrix[j][i]==0) color=0;                            
      else if (currentMatrix[j][i]<0) color=3;    
      else if (currentMatrix[j][i]==129) color=textColor;             
      else if (currentMatrix[j][i]==2) color = 2;
      else color=1;
      switch (color)                               
   {                                            
        case 0:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,LOW);
          break;
        case 1:
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,LOW);
          break;
        case 2:
          digitalWrite(redPin,LOW);
          digitalWrite(greenPin,HIGH);
          break;
        case 3:                              //???????
          digitalWrite(redPin,HIGH);
          digitalWrite(greenPin,HIGH);
          break; 
      }
      digitalWrite(colClock,HIGH);
      digitalWrite(colClock,LOW);
    }
    digitalWrite(latchPin,HIGH);
    digitalWrite(latchPin,LOW);
    digitalWrite(output,LOW);
    delay(1);
    digitalWrite(output,HIGH);
  }
}

Wow, I was not expecting that much code. Give me a few minutes to look over it. Actually since you know the code, where is the section that rotates the piece? Found it.

void rotate() {  // updates: currentPieceRotatedIndex, currentPieceIndex, currentPiece, and currentMatrix
  // obtain the currentPieceRotatedIndex
  if (currentPieceIndex==0) return;
  else if (currentPieceIndex<3) currentPieceRotatedIndex=3-currentPieceIndex;
  else if (currentPieceIndex<5) currentPieceRotatedIndex=7-currentPieceIndex;
  else if (currentPieceIndex<7) currentPieceRotatedIndex=11-currentPieceIndex;
  else if ((currentPieceIndex % 4)==2) currentPieceRotatedIndex=currentPieceIndex-3;
  else currentPieceRotatedIndex=currentPieceIndex+1;
  
  currentPieceMovedRow=currentPieceRow;
  currentPieceMovedCol=currentPieceCol;
    
  // check if the rotation is valid 
  if (!checkmoveable(currentPieceRotatedIndex)) {
    clearTrace();
    currentPieceIndex=currentPieceRotatedIndex;
    for (int i=0;i<4;i++) {
      for (int j=0;j<4;j++) {
        currentPiece[j][i]=pieces[currentPieceRotatedIndex][j][i];
      }
    }
    updateCurrentMatrix();
  }
}

here is the rotation code..

You may very well be running out of memory, but if it is just one piece that's causing a problem, it could be something else instead. How big did you make the 3D array "pieces"?

i made it 3D array as 304 byte.

I tried to find where you initialized it, but couldn't find it.

Another thing, at what point does the piece mess up? Asa it gets on screen or asa you rotate it? (asa -> As soon as)
I'm curious as to why you chose to have a 16 x 16 byte array, where each element is considered a single byte, whereas using a single array of words? It would cut down your code and memory usage greatly, but also probably make it more confusing too.

as soon as get on screen. futhermore, on the next queue, the block is messed up already. they shaped different when it appear. it have two situation: one, it change back to normal when we rotate; another, it can't be rotate and it move down to dead block with that messed up shape

It to me, does look like it could be a memory problem, but the thing is, the T is not the last piece, it's towards the middle of them. If it was the very last of the pieces, then I would be more confident to say it was a memory issue. Your rotate function seems ok, as it does rotate every other piece fine, so, I don't know.

Maybe someone else with better eyes can find the problem.

we kinda solve it lol
we change the array as [25][4][4], and blocks are functioning well now.
thinking that some garbage value was inturpting blocks.. we dont know why but well, it runs well right now lol
thank you for spending time on this :slight_smile:

You found the problem, see someone with better eyes. Lol