Method not error but project not run when call this method.

LCD Nokia 5110 48*84
LCD Nokia lib for Proteus in Attach file.
I have a project with some method, my project can’t run when I call " XoayKhoiGach(currKhoi)" method in “setup()”, but this method haven’t error. M project can run without call " XoayKhoiGach(currKhoi)" method.
Please help me :slight_smile:

this is my code project:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
#define MaxI 26
#define MaxJ 10
uint16_t Board[MaxI][MaxJ];//mang chua cac phan tu
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
//thong tin khoi gach
typedef struct 
{
	uint16_t **arr;
	uint16_t Row,Col;
	uint16_t iBoard,jBoard;
}KhoiGach;

KhoiGach *TaoKhoiGach(uint8_t ID);
//kiem tra
int16_t Inside(uint16_t i, uint16_t j);

int16_t Down(uint16_t i, uint16_t j);

int16_t RoiXuong(KhoiGach *pkhoigach);

void XoayKhoiGach(KhoiGach *pkhoigach);

void VeKhoiGach(KhoiGach *pkhoigach);




void setup() {
  // put your setup code here, to run once:
  display.begin();
  display.display(); // show splashscreen
  delay(2000);
  display.clearDisplay();
  KhoiGach *currKhoi;
  currKhoi=TaoKhoiGach(58);
  int16_t i,j;
  for(i=0;i<MaxI;i++)
    for(j=0;j<MaxJ;j++)
      Board[i][j]=0;
      
  VeKhoiGach(currKhoi);
  delay(500);
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  VeKhoiGach(currKhoi);
  delay(500);
  
  XoayKhoiGach(currKhoi);
  VeKhoiGach(currKhoi);
  delay(500);
  
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  RoiXuong(currKhoi);
  VeKhoiGach(currKhoi);
  }

void loop() {
  // put your main code here, to run repeatedly:

}

//tao khoi gach
KhoiGach *TaoKhoiGach(uint8_t ID){
  KhoiGach *pkhoigach=(KhoiGach*)malloc(sizeof(KhoiGach));
  switch(ID)
  {
    case 15:
      pkhoigach->Row=4;
      pkhoigach->Col=1;
      pkhoigach->iBoard=0;
      pkhoigach->jBoard=5;
      break;
    case 31:
      pkhoigach->Row=pkhoigach->Col=2;
      pkhoigach->iBoard=2;
      pkhoigach->jBoard=5;
      break;
    default:
      pkhoigach->Row=2;
      pkhoigach->Col=3;
      pkhoigach->iBoard=2;
      pkhoigach->jBoard=5;
    break;
  }
  pkhoigach->arr=(uint16_t**)malloc(pkhoigach->Row*sizeof(uint16_t*));
  for(uint16_t i=0;i<pkhoigach->Row;i++)
  {
    pkhoigach->arr[i]=(uint16_t*)malloc(pkhoigach->Col*sizeof(uint16_t));
  }
  for(int k=0;k<pkhoigach->Col*pkhoigach->Row;k++)
  {
    pkhoigach->arr[k/pkhoigach->Col][k%pkhoigach->Col]=(ID>>(pkhoigach->Col*pkhoigach->Row-1-k))&1;
  }
  return pkhoigach;}

//kiem tra va cham khi khoi gach roi xuong
int16_t Inside(uint16_t i, uint16_t j){
  return (i>=0&&i<MaxI&&j>=0&&j<MaxJ);
}

int16_t Down(uint16_t i, uint16_t j){
  if(i<MaxI-1&&Inside(i,j)&&Board[i+1][j]==0) return 1;
  return 0;
}

int16_t RoiXuong(KhoiGach *pkhoigach){
  for(uint16_t i=0;i<pkhoigach->Row;i++)
    for(uint16_t j=0;j<pkhoigach->Col;j++)
      if(pkhoigach->arr[i][j]==1)
      {
        if(Down(pkhoigach->iBoard+i,pkhoigach->jBoard+j)==0) return 0;
      }
pkhoigach->iBoard+=1; //Roi xuong 1 so voi Board[26][10]
return 1;
}

//xoay khoi gach
void XoayKhoiGach(KhoiGach *pkhoigach){
  uint16_t i,j;
  uint16_t ** tmpArr;
  uint16_t tmpRow=pkhoigach->Col;
  uint16_t tmpCol=pkhoigach->Row;
  //C?p phát b? nh? cho ma tr?n ph? tmpArr.
  tmpArr=(uint16_t**)malloc(tmpRow*sizeof(uint16_t*));
  for( i=0;i<tmpRow;i++)
  {
    tmpArr[i]=(uint16_t*)malloc(tmpCol*sizeof(uint16_t));
  }
  ///////////////////////////////////////////////
  for(i=pkhoigach->Row-1;i>=0;i--)
  for(j=pkhoigach->Col-1;j>=0;j--)
  {
    tmpArr[j][pkhoigach->Row-i-1]=pkhoigach->arr[i][j];
  }
	//Ki?m tra h?p l?.
  for (i=0;i<tmpRow;i++)
  {
    for(j=0;j<tmpCol;j++)
    if(!Inside(pkhoigach->iBoard+i,pkhoigach->jBoard+j)||Board[pkhoigach->iBoard+i][pkhoigach->jBoard+j]==1)
    return;
  }
  for(i=0;i<pkhoigach->Row;i++) free(pkhoigach->arr[i]);
  free(pkhoigach->arr);
  //C?p nh?t thay ??i sau khi xoay.
  pkhoigach->Col=tmpCol;
  pkhoigach->Row=tmpRow;
  pkhoigach->arr=tmpArr;
}


void VeKhoiGach(KhoiGach *pkhoigach){
  uint16_t i,j; 
  for(i=0;i<pkhoigach->Row;i++)
    for(j=0;j<pkhoigach->Col;j++)
      if(pkhoigach->arr[i][j]==1)
	{
	  display.drawRect((pkhoigach->iBoard+i)*3,(pkhoigach->jBoard+j)*3,3,3,BLACK);
          display.display();
	}
}

Proteus-Model-Nokia3310LCD.zip (427 KB)

Problem:

  for(i=pkhoigach->Row-1;i>=0;i--)
  for(j=pkhoigach->Col-1;j>=0;j--)

Solution: http://arduino.land/FAQ/content/2/31/en/why-does-this-loop-never-end.html

I don’t see anything in this link.
can you give me other link :slight_smile:

While the unsigned value is greater than or equal to 0, keep looping. How will an unsigned value ever get to be less than 0?

YES :) this is. Thank you verymuch. Close topic. Thank you