Calculate the degrees out of a magnetometer

Hello,

Im trying to get North, East, South, West out of the Magnetometer.
I’m using the Arduino ZERO + NB-IoT shield, with Sodaq_LSM303AGR library.
The code i use is:

Init Magnetometer:

void initAccMagSensor(int8_t timer, int16_t timeout) {
accMagSensor = new Sodaq_LSM303AGR();
accMagSensor->rebootAccelerometer();
delay(timeout);
accMagSensor->rebootMagnetometer();
delay(timeout);
accMagSensor->enableAccelerometer(accMagSensor->HighResMode,accMagSensor-HrNormalLowPower100Hz, accMagSensor->XYZ, accMagSensor->Scale2g);
accMagSensor->enableMagnetometer(accMagSensor->MagHighResMode,accMagSensor-Hz100,accMagSensor->Continiuous);
delay(timer);
}

For getting the Axes:

double getMXAs() {
return accMagSensor->getMagX();
}

double getMYAs() {
return accMagSensor->getMagY();
}

double getMZAs() {
return accMagSensor->getMagZ();
}

I’ll get the average of 10 samples to get stabe numbers with the next code:

double middleAxes(String type, int8_t factor) {
double middle = 0;
for (int i = 0; i < (factor-1); i++) {
if (type.equals(“axas”)) {
middle += getAXAs();
} else if (type.equals(“ayas”)) {
middle += getAYAs();
} else if (type.equals(“azas”)) {
middle += getAZAs();
} else if (type.equals(“mxas”)) {
middle += getMXAs();
} else if (type.equals(“myas”)) {
middle += getMYAs();
} else if (type.equals(“mzas”)) {
middle += getMZAs();
}
}
return middle / factor;
}

Next up, I’m calculating the degrees with this code (this is done with the blog found on: https://blog.digilentinc.com/how-to-convert-magnetometer-data-into-compass-heading/):

int32_t getDegrees(){
float heading = 0;
double xGaussData = middleAxes(“mxas”, middleFactor);
double yGaussData = middleAxes(“myas”, middleFactor);
xGaussData *= 0.48828125;
yGaussData = 0.48828125;
if (xGaussData == 0 && yGaussData < 0){
heading = 90;
} else if (yGaussData >= 0){
heading = 0;
} else if (xGaussData != 0) {
heading = atan(yGaussData/xGaussData)
(180/PI);

if (heading > 360){
heading = heading - 360;
}
if (heading < 0){
heading = heading + 360;
}
}

DEBUG_STREAM.println("\t heading: " + String(heading));
getCourse(heading);
return 0;
}

To get a string out of the degrees i use the next code:

void getCourse(float heading){
String Course;
if (heading > 337.25 || heading < 22.5){
Course = “North”;
} else if (heading > 292.5 && heading < 337.25) {
Course = “North-West”;
} else if (heading > 247.5 && heading < 292.5) {
Course = “West”;
} else if (heading > 202.5 && heading < 247.5) {
Course = “South-West”;
} else if (heading > 157.5 && heading < 202.5) {
Course = “South”;
} else if (heading > 112.5 && heading < 157.5) {
Course = “South-East”;
} else if (heading > 67.5 && heading < 112.5) {
Course = “East”;
} else if (heading > 0 && heading < 37.5) {
Course = “North-East”;
}
DEBUG_STREAM.println("\t Course: " + Course);
}

The result i get is not what you should expect,
By what should be 0 degrees:

getMagX: -229.50 getMagY: -6.00 getMagZ: -720.00 heading: 1.61
Course: North

By what should be 90 degrees:

getMagX: 48.00 getMagY: -180.00 getMagZ: -711.00 heading: 286.24
Course: West

By what should be 180 degrees:

getMagX: -121.50 getMagY: -447.00 getMagZ: -738.00 heading: 75.56
Course: East

By what should be 270 degrees:

getMagX: -412.50 getMagY: -270.00 getMagZ: -733.50 heading: 33.12
Course: North-East

Like you see above this is nothing like it should be,
What im doing wrong?

Thanks in advance,
Paul