Never exit from while loop

I post here because of it looks the program run to the GetHeadingDegrees() and won’t return.
I fork Player(60) after one IR remote button pushed. The last printed message from serial is:
“Enter while loop of Player”.
Can anyone help me on it? I would like to turn one specific angle with DC motor. Thanks

int Player(int pos)
{
 pos = pos % 360;
 int prePos = GetHeadingDegrees() - pos;

 if (DEBUG){
   Serial.print("parameter pos is:");
   Serial.println(pos);
   Serial.print("first prePos is:");
   Serial.println(prePos);
 }
 digitalWrite(MotorApin_d, HIGH); // start player motor
 if (DEBUG) Serial.println("Motor A pin d set to high");
 digitalWrite(MotorBpin_r, HIGH); // start player motor
 if (DEBUG) Serial.println("Motor B pin r set to high");
//  int prePos = GetHeadingDegrees() - pos;
//  if (DEBUG){
//    Serial.print("Pos is:");
//    Serial.println(pos);
//    Serial.print("prePos is:");
//    Serial.println(prePos);
//  }
 unsigned long t = millis();
 while ((millis() - t) < iIntervalofgotoplayer){
   if (DEBUG) Serial.println("Enter while loop of Player");
   //delay(5);
   int curPos = GetHeadingDegrees() - pos;
/*
   if (DEBUG){
     Serial.print("prePos is                :");
     Serial.println(prePos);
 Serial.print("curPos is                :");
     Serial.println(curPos);
   }
*/
   if ((prePos <= 0) && (curPos >= 0)){
     ////delay(50);
     digitalWrite(MotorApin_d, LOW); // stop player motor
     if (DEBUG) Serial.println("Motor A pin d set to low");
     digitalWrite(MotorBpin_r, LOW); // stop player motor
     if (DEBUG) Serial.println("Motor B pin r set to low");
     if (DEBUG) Serial.println("Turn to right angle");
   ////delay(50);
     return 1;
   }
   prePos = curPos;
 }
 if (DEBUG) Serial.println("Time exceed 4s");
 ////delay(50);
 digitalWrite(MotorApin_d, LOW); // stop player motor
 if (DEBUG) Serial.println("Motor A pin d set to low");
 digitalWrite(MotorBpin_r, LOW); // stop player motor
 if (DEBUG) Serial.println("Motor B pin r set to low");
 ////delay(50);
 return 0;
}


int GetHeadingDegrees()
{
 Vector norm = compass.readNormalize();

 // Calculate heading
 float heading = atan2(norm.YAxis, norm.XAxis);

 // Set declination angle on your location and fix heading
 // You can find your declination on: http://magnetic-declination.com/
 // (+) Positive or (-) for negative
 // For Bytom / Poland declination angle is 4'26E (positive), Shanghai magnetic declination angle is -5'57
 // Formula: (deg + (min / 60.0)) / (180 / PI); 
 float declinationAngle = (-(5 + (57.0 / 60.0))) / (180 / PI);
 heading += declinationAngle;
 if(DEBUG){
   Serial.print("heading = ");
   Serial.println(heading);
   }
 // Correct for heading < 0deg and heading > 360deg
 if (heading <= 0){
   heading += 2 * PI;
 }
 if (heading > 2 * PI){
   heading -= 2 * PI;
 }
 // Convert to degrees
 float headingDegrees = heading * 180/PI; 
 if (DEBUG){
   Serial.print(" Heading = ");
   Serial.print(heading);
   Serial.print(" Degress = ");
   Serial.println(headingDegrees);
 }
 ////delay(10);
 return headingDegrees;
}

Please edit your post to add code tags. Consult the "How to use this forum" post.

You should also post the whole sketch: no setup() and loop() there afaics.

The whole sketch exceeds the maximum allowed length, so I post most below

/*-----( Import needed libraries )-----*/
#include "IRremote.h"
#include "IRremoteInt.h"
#include "DFRobot_QMC5883.h"
#include "Wire.h"
#define DEBUG 1
/*-----( Declare Constants )-----*/
#define  REPEAT_DELAY  500   // Delay before checking for another button / repeat
int receiver = 11;           // pin 1 of IR receiver to Arduino digital pin 11
                             // NOTE: Other pins can be used, except pin 3 and 13
int MotorApin_d = 2;
int MotorApin_r = 4;
int MotorBpin_d = 7;
int MotorBpin_r = 8;
int MotorCpin_d = 12;
int MotorCpin_r = 10;
int iPlayers = 4;           //set 4 default players
int iCardEach = 13;         //set 13 playing cards for each one as default
int iPlayerDistance = 90;   //set 90 degrees for players distance
unsigned long iDealCards = 2000;      //dealing one card with 2 seconds
unsigned long iIntervalofgotoplayer = 4000;
bool isSetPlayer = false;   //will set players number if true
bool isSetCardForEach = false;//will set card for each player if true
bool isDoneSetCardForEach = false;//completed set card for each player if true
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

/*-----( Declare Variables )-----*/
int inPin = 3;       // pin 3 for get signal from the infrared obstacle avoidance module
DFRobot_QMC5883 compass;
int dx = 0;
int dy = 0;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  if (DEBUG){
    Serial.begin(9600);
    Serial.println("YourDuino.com IR Infrared Remote Control Kit V2");  
    Serial.println("IR Receiver Raw Data + Button Decode Test");
  }
  irrecv.enableIRIn(); // Start the receiver
  pinMode(MotorApin_d, OUTPUT); //Motor A and Motor B drive the wheel of the card dealer car
  pinMode(MotorApin_r, OUTPUT);
  pinMode(MotorBpin_d, OUTPUT);
  pinMode(MotorBpin_r, OUTPUT);
  pinMode(MotorCpin_d, OUTPUT); //Motor C is the drive wheel to deal the card
  pinMode(MotorCpin_r, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(MotorApin_d, LOW);
  digitalWrite(MotorApin_r, LOW);
  digitalWrite(MotorBpin_d, LOW);
  digitalWrite(MotorBpin_r, LOW);
  digitalWrite(MotorCpin_d, LOW);
  digitalWrite(MotorCpin_r, LOW);
  //randomSeed(analogRead(9));  //random number for random card dealing
  InitCompass();
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    if (DEBUG) Serial.println(results.value, HEX); // UN Comment to see raw values
    
    TranslateIR();
    irrecv.resume(); // receive the next value
    ////delay(REPEAT_DELAY);    // Adjust for repeat / lockout time
    //digitalWrite(13, HIGH);
    //delay(3000);
    //digitalWrite(13, LOW);
    //delay(50);
  }
  //delay(10);
}/* --(end main loop )-- */


int Player(int pos)
{
  pos = pos % 360;
  int prePos = GetHeadingDegrees() - pos;

  if (DEBUG){
    Serial.print("parameter pos is:");
    Serial.println(pos);
    Serial.print("first prePos is:");
    Serial.println(prePos);
  }
  digitalWrite(MotorApin_d, HIGH); // start player motor
  if (DEBUG) Serial.println("Motor A pin d set to high");
  digitalWrite(MotorBpin_r, HIGH); // start player motor
  if (DEBUG) Serial.println("Motor B pin r set to high");
//  int prePos = GetHeadingDegrees() - pos;
//  if (DEBUG){
//    Serial.print("Pos is:");
//    Serial.println(pos);
//    Serial.print("prePos is:");
//    Serial.println(prePos);
//  }
  unsigned long t = millis();
  while ((millis() - t) < iIntervalofgotoplayer){
    if (DEBUG) Serial.println("Enter while loop of Player");
    //delay(5);
    int curPos = GetHeadingDegrees() - pos;
/*
    if (DEBUG){
      Serial.print("prePos is                :");
      Serial.println(prePos);
  Serial.print("curPos is                :");
      Serial.println(curPos);
    }
*/
    if ((prePos <= 0) && (curPos >= 0)){
      ////delay(50);
      digitalWrite(MotorApin_d, LOW); // stop player motor
      if (DEBUG) Serial.println("Motor A pin d set to low");
      digitalWrite(MotorBpin_r, LOW); // stop player motor
      if (DEBUG) Serial.println("Motor B pin r set to low");
      if (DEBUG) Serial.println("Turn to right angle");
    ////delay(50);
      return 1;
    }
    prePos = curPos;
  }
  if (DEBUG) Serial.println("Time exceed 4s");
  ////delay(50);
  digitalWrite(MotorApin_d, LOW); // stop player motor
  if (DEBUG) Serial.println("Motor A pin d set to low");
  digitalWrite(MotorBpin_r, LOW); // stop player motor
  if (DEBUG) Serial.println("Motor B pin r set to low");
  ////delay(50);
  return 0;
}


/* Press number it has 3 cases: 
1. After VOL-, set player numbers
2. After VOL+, set cards for each player
3. Just the number, turn to the player 
*/
void NumberProcessing(int number){
    if(DEBUG) Serial.println(number);
    if (isSetPlayer){
      iPlayers = number;
      isSetPlayer = false;
      }
    else if (isSetCardForEach){
      if (isDoneSetCardForEach){
        iCardEach = number;
        isDoneSetCardForEach = false;
      }
      else
      {
        iCardEach = iCardEach*10 + number;
        isSetCardForEach = false;
      }
      }
    else
      Player((number-1)*iPlayerDistance+60);
  }
/*-----( Declare User-written Functions )-----*/

void TranslateIR() // takes action based on IR code received // describing Car MP3 IR codes
{
switch (results.value)
{

  case 0xFF30CF:
    if(DEBUG) Serial.println(" 1 ");
    NumberProcessing(1);
    break;
  default:
    if(DEBUG) Serial.println(" other button ");
    break;
}
delay(REPEAT_DELAY);
} //END TranslateIR
/* ( THE END ) */

Or, you can also trim your code and post the minimum that still exhibits the problem...

The latest log printed after I push button on remote control:

1
YAxis is       :XAxis is       :heading = -2.46
 Heading = 3.82 Degress = 219.05
parameter pos is:60
first prePos is:159
Motor A pin d set to high
Motor B pin r set to high
Enter while loop of Player

And the motor A and B will run forever and won't be set back to LOW.

 Vector norm = compass.readNormalize();

No idea what it does but based on your code and provided output this line would be my prime suspect for things getting stuck.
Nothing else blocking, and the next print doesn't appear.

Below log was printed with the return value of GetHeadingDegrees() {... Vector norm = compass.readNormalize(); ...}
Heading = 3.82 Degress = 219.05

The vector is just defined as one structure
struct Vector
{
float XAxis;
float YAxis;
float ZAxis;
};

This one Vector norm = compass.readNormalize(); is from
one library of QMC5883

Vector DFRobot_QMC5883::readNormalize(void)
{
  int range = 10;
  float Xsum = 0.0;
  float Ysum = 0.0;
  float Zsum = 0.0;
  if(isHMC_){
    while (range--){
      v.XAxis = ((float)readRegister16(HMC5883L_REG_OUT_X_M )) * mgPerDigit;
      v.YAxis = ((float)readRegister16(HMC5883L_REG_OUT_Y_M )) * mgPerDigit;
      v.ZAxis = (float)readRegister16(HMC5883L_REG_OUT_Z_M) * mgPerDigit;
      Xsum += v.XAxis;
      Ysum += v.YAxis;
      Zsum += v.ZAxis;
    }
    v.XAxis = Xsum/range;
    v.YAxis = Ysum/range;
    v.ZAxis = Zsum/range;
    if(firstRun){
      initMinMax();
      firstRun = false;
    }
    calibrate();
    v.XAxis= map(v.XAxis,minX,maxX,-360,360);
    v.YAxis= map(v.YAxis,minY,maxY,-360,360);
    v.ZAxis= map(v.ZAxis,minZ,maxZ,-360,360);
    return v;
  }else if(isQMC_){
    while (range--){
      v.XAxis = ((float)readRegister16(QMC5883_REG_OUT_X_M)) * mgPerDigit;
      v.YAxis = ((float)readRegister16(QMC5883_REG_OUT_Y_M)) * mgPerDigit;
      v.ZAxis = (float)readRegister16(QMC5883_REG_OUT_Z_M) * mgPerDigit;
      Xsum += v.XAxis;
      Ysum += v.YAxis;
      Zsum += v.ZAxis;
    }
    v.XAxis = Xsum/range;
    v.YAxis = Ysum/range;
    v.ZAxis = Zsum/range;
    if(firstRun){
      initMinMax();
      firstRun = false;
    }
    
    calibrate();
    v.XAxis= map(v.XAxis,minX,maxX,-360,360);
    v.YAxis= map(v.YAxis,minY,maxY,-360,360);
    v.ZAxis= map(v.ZAxis,minZ,maxZ,-360,360);
  }
  return v;
}