ArduinoBLE with MLX90640 library from adafruit

Hello everyone,

I am currently working on a project using an Arduino equipped with an MLX90640 sensor to transmit temperature images via Bluetooth Low Energy (BLE). When I use only the MLX90640 library from Adafruit, the program functions correctly and displays the temperature image as expected. However, when I include the ArduinoBLE library, the sensor operates for just 3 to 5 minutes before it fails.

Additionally, I've noticed that the dynamic memory usage spikes to 70% when both libraries are included.

Best regards,
Youssef

Here's the code that i'm using :

#include <Adafruit_MLX90640.h>
#include <ArduinoBLE.h>
Adafruit_MLX90640 mlx;
float frame[32*24]; // buffer for full frame of temperatures
uint8_t frame_ble[32*24];

// Définir les UUIDs des services et caractéristiques
const char * deviceServiceUuid = "1523";
const char * deviceServiceRequestCharacteristicUuid = "1526";
const char * deviceServiceResponseCharacteristicUuid = "1527";

BLEService ArduinoMLXService(deviceServiceUuid);
BLECharacteristic ArduinoMLXRequestCharacteristic(deviceServiceRequestCharacteristicUuid, BLEWrite, 4);
BLECharacteristic ArduinoMLXResponseCharacteristic(deviceServiceResponseCharacteristicUuid, BLENotify, 768);

void setup() {
  while (!Serial) delay(10);
  Serial.begin(115200);
  delay(100);

  Serial.println("Adafruit MLX90640 Simple Test");
  if (! mlx.begin(MLX90640_I2CADDR_DEFAULT, &Wire)) {
    Serial.println("MLX90640 not found!");
    while (1) delay(10);
  }
  Serial.println("Found Adafruit MLX90640");

  Serial.print("Serial number: ");
  Serial.print(mlx.serialNumber[0], HEX);
  Serial.print(mlx.serialNumber[1], HEX);
  Serial.println(mlx.serialNumber[2], HEX);

  //mlx.setMode(MLX90640_INTERLEAVED);
  mlx.setMode(MLX90640_CHESS);
  Serial.print("Current mode: ");
  if (mlx.getMode() == MLX90640_CHESS) {
    Serial.println("Chess");
  } else {
    Serial.println("Interleave");
  }

  mlx.setResolution(MLX90640_ADC_18BIT);
  Serial.print("Current resolution: ");
  mlx90640_resolution_t res = mlx.getResolution();
  switch (res) {
    case MLX90640_ADC_16BIT: Serial.println("16 bit"); break;
    case MLX90640_ADC_17BIT: Serial.println("17 bit"); break;
    case MLX90640_ADC_18BIT: Serial.println("18 bit"); break;
    case MLX90640_ADC_19BIT: Serial.println("19 bit"); break;
  }

  mlx.setRefreshRate(MLX90640_2_HZ);
  Serial.print("Current frame rate: ");
  mlx90640_refreshrate_t rate = mlx.getRefreshRate();
  switch (rate) {
    case MLX90640_0_5_HZ: Serial.println("0.5 Hz"); break;
    case MLX90640_1_HZ: Serial.println("1 Hz"); break;
    case MLX90640_2_HZ: Serial.println("2 Hz"); break;
    case MLX90640_4_HZ: Serial.println("4 Hz"); break;
    case MLX90640_8_HZ: Serial.println("8 Hz"); break;
    case MLX90640_16_HZ: Serial.println("16 Hz"); break;
    case MLX90640_32_HZ: Serial.println("32 Hz"); break;
    case MLX90640_64_HZ: Serial.println("64 Hz"); break;
  }

    /* Initialiser BLE */
  BLE.setDeviceName("ArduinoMLX");
  BLE.setLocalName("ArduinoMLX");

  if (!BLE.begin()) {
    Serial.println(F("- Starting Bluetooth® Low Energy module failed!"));
    while (1);
  }

  BLE.setAdvertisedService(ArduinoMLXService);
  ArduinoMLXService.addCharacteristic(ArduinoMLXRequestCharacteristic);
  ArduinoMLXService.addCharacteristic(ArduinoMLXResponseCharacteristic);
  BLE.addService(ArduinoMLXService);
  ArduinoMLXResponseCharacteristic.writeValue((uint8_t*)"0", 1);

  BLE.advertise();

  Serial.println(F("Arduino R4 WiFi BLE (Peripheral Device)"));
  Serial.println(" ");

}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      delay(500);
      if (mlx.getFrame(frame) != 0) {
        Serial.println("Failed");
        return;
      }
      Serial.println();
      for (uint8_t h=0; h<24; h++) {
        for (uint8_t w=0; w<32; w++) {
          // float t = frame[h*32 + w];
          frame_ble[h*32 + w] = frame[h*32 + w];
          Serial.print(frame_ble[h*32 + w]);
          Serial.print(", ");
        }
        Serial.println();
      }
      sendLargeDataOverBLE();
      // delay(1000);
    }
    Serial.println("* Disconnected to central device!");
  }
  
}
void sendLargeDataOverBLE() {
  for (int i = 0; i < sizeof(frame_ble); i += 20) {
    // Create a buffer for the chunk
    uint8_t chunk[20];
    int chunkSize = min(20, sizeof(frame_ble) - i);

    // Copy the chunk from the large data array
    memcpy(chunk, frame_ble + i, chunkSize);

    // Send the chunk
    ArduinoMLXResponseCharacteristic.writeValue(chunk, chunkSize);

    // Small delay to ensure the BLE stack can handle the next packet
    delay(10);
  }
}

Yes my question is why it is failing after 3 or 5 minutes ?

Best Regards