Reading magnetic field values from the magnetometer (LIS2MDL)

This code is supposed to let the x-nucleo-IKS01A3 and NUCLEO-F446RE work as a compass. Once the code is ran, the compass direction and x, y, z values are returned once. These values do not change when the microcontroller is rotated or the program re-executed unless it is plugged out and in again. Is there an issue with the board or the code?

#include “mbed.h”
#include “XNucleoIKS01A3.h”

static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;

InterruptIn mybutton(BUTTON1);
DigitalOut myled(LED1);

int main() {
int32_t axes[3];

printf(“\r\n— Starting new run —\r\n”);

while (1) {

    magnetometer->get_m_axes(axes);

    int32_t x = axes[0];
    int32_t y = axes[1];
    int32_t z = axes[2];

    if (-300 <= x && x < -180 && y >= 120) {
        printf("WEST\n");
    } else if (-180 <= x && x < -40 && 0 <= y && y < 120) {
        printf("NORTH-WEST\n");
    } else if (x >= -40 && -200 <= y && y < 0) {
        printf("NORTH\n");
    } else if (-300 <= x && x < -60 && y < -100) {
        printf("NORTH-EAST\n");
    } else if (-380 <= x && x < -300 && -250 <= y && y < -50) {
        printf("EAST\n");
    } else if (x <= -380 && -80 <= y && y < 10) {
        printf("SOUTH-EAST\n");
    } else if (x <= -380 && 10 <= y && y < 100) {
        printf("SOUTH\n");
    } else if (-400 <= x && x < -300 && 100 <= y && y < 180) {
        printf("SOUTH-WEST\n");
    
    } else {
        printf("No specific direction.\n");
    }

printf(“Magnetic Field (X, Y, Z): %ld, %ld, %ld\n”, x, y, z);
ThisThread::sleep_for(1000); // Adjust delay as needed

}

}