ArduinoBLE.begin slows everything down

I have a application on a nano33BLE that was already working fine (in my campervan), reading data from a Modbus485 slave and displaying it on an OLED screen, I am using;
OLED display using u8g2 library
Modbus485 using ModbusMaster.h
The IMU to display if the van is level
A quadrature encoder dial to navigate the display (uses interrupts)

I tried to add some bluetooth functionality, simply to check if a particular device exists or not, but I found this slows the whole program down and the .scanForAddress() does not work consistently when the device is present. I took all of the new code out except for the .begin() and this still slows everything down.

My question is; is there some way to limit the cpu time assigned to the bluetooth?

Can you post your code?

Apparantly not. It exceeds the 9000 character limit, by a lot, theres over 800 lines of code, plus some of my own libraries. I will have to write some kind of test program that shows similar behaviour.

In the meantime, here is the bit that includes the BLE.begin, if I comment out all the BLE code, it runs fine, then add back in only the BLE.begin, everything slows right down.

  // Libraries
  #include <Arduino.h> // to fix the digital pin problem (always PULLUP);
  #include <U8g2lib.h>
  #include <Arduino_LSM9DS1.h>
  #include <ModbusMaster.h>
  #include <myFunctions.h>
  #include <ArduinoBLE.h>

  // ModbusMaster.h needs ModbusMaster.cpp to be modified as the link here:
  // without this mod, 2 stop bits can't work for some reason

  // In the ModbusMaster.cpp file line 712, find this
  // for (i = 0; i < u8ModbusADUSize; i++) { _serial->write(u8ModbusADU[i]); }
  // and add a +1 like so;
  // for (i = 0; i < u8ModbusADUSize+1; i++) { _serial->write(u8ModbusADU[i]); }
  // OR ADD A delay(2) AFTER IT!!

  // Definitions
  #define ENC_A     3
  #define ENC_B     4
  #define ENC_PB    5
  #define MAX485_DE 2 //Modbus RTU

  // Encoder dial
  volatile int EncVal = 0;
  int OldEncVal, EncChange;

  // Declare functions - not essential in Arduino IDE, essential in PIO
  void sub_Clocks();
  void sub_modbus();
  void preTransmission();
  void postTransmission();

  // OLED configuration
  U8G2_SSD1322_NHD_256X64_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);  // Enable U8G2_16BIT in u8g2.h line 71

  // Modbus instance
  ModbusMaster node;

  // Structures
  struct Clock {bool Pulse; bool Clock; bool mem;};

  // Global variables
  int   Select=1, Page=0, mSecCount;//, powerSaveTime=60, Contrast=100;
  byte  ClockByte=0;
  int   ComFailCnt=0, ParAdjust=0, Writes;
  unsigned long  Millis_mem=0, showMenuMillis, pbTime;
  bool  ImuOK, ShowMenu, CalibLevel, SavePars;
  bool  ParModified[16]; //array of flags to identify if a parameter was modified, and needs writing to EEPROM
  bool  UpPbStateMem, DnPbStateMem, SelPbStateMem;
  float accel_center_x, accel_center_y, accel_center_z, accel_angle_x, accel_angle_y;//, offset_x, offset_y;
  float accel_value_x, accel_value_y, accel_value_z;
  float angle_x, angle_y, angle_x_filt, angle_y_filt;
  char modbusResult[60];
  float LR_error, FR_error;
  Clock Clk500ms, Clk1s, Clk2s, Clk4s, Clk8s;
  debounce UpPb, DnPb, SelPb;
  int systemInfoCounter=0;
  bool EHUalive;

  const float t6wheelbase=340.0, t6track=170.0; // in cm

  //bool debug1=true; // when debug1 is true, the program will not run without a serial monitor connected
  bool debug1=false;

  // Global variables - Renogy system information   
  byte  SI_MaxV;
  byte  SI_RatedI;
  char  SI_Model[17]="-----------";
  char  SI_SwVersion[17]="V-.-.-.-";
  char  SI_HwVersion[17]="V-.-.-.-";
  char  SI_SerNo[9]="--------";

  // Global variables - Renogy Data scaled to appropriate units  
  char  AuxSOC[5];       //2 byte      %
  char  AuxV[6];         //2 byte      0.1V
  char  MaxChargeA[7];   //2 byte      0.01A
  char  ContrTemp[8];    //high byte   deg C (bit7 is sign)
  char  AuxTemp[8];      //low byte    deg C (bit7 is sign)
  char  AlterV[6];       //2 byte      0.1V
  char  AlterA[7];       //2 byte      0.01A
  char  AlterW[5];       //2 byte      W
  char  SolarV[6];       //2 byte      0.1V
  char  SolarA[7];       //2 byte      0.01A
  char  SolarW[5];       //2 byte      W
  char  DayCount[6];
  int   ChargeState;  //low byte      
  word  FaultBits1;
  word  FaultBits2;

  // Global variables - Renogy Data used for logical purposes
  float AlternatorV;

//====================================== Flash handling ======================================

// nFR52 NVMC registers
#define NVMC_BASE       (0x4001E000U)
#define NVMC_READY      (NVMC_BASE + 0x400U)
#define NVMC_READYNEXT  (NVMC_BASE + 0x408U)
#define NVMC_CONFIG     (NVMC_BASE + 0x504U)
#define NVMC_ERASEPAGE  (NVMC_BASE + 0x508U)
#define NVMC_ERASEALL   (NVMC_BASE + 0x50CU)
#define NVMC_ERASEUICR  (NVMC_BASE + 0x514U)
#define NVMC_ICACHECNF  (NVMC_BASE + 0x540U)
#define NVMC_IHIT       (NVMC_BASE + 0x548U)
#define NVMC_IMISS      (NMVC_BASE + 0x54cU)

//Data forced into flash only for use with bug fix
const uint32_t flash2 __attribute__ ((section("FLASH2"), aligned (0x1000))) = 0x09090909;

//Structure containing all data to be saved into flash. To allow deletion of a flash page this struct MUST be 256 bytes in length
typedef struct flash_mem {
//struct flash_mem {
    int powerUpCounter;
    byte contrast;
    long powerSaveTime;
    float angleOffsetX;
    float angleOffsetY;
    byte padding[236];
} flash_mem_t;
//int bytesToFlash=20; //the useful length of the above struct (to keep memcpy length within the ?? byte limit)

const flash_mem_t myFlash __attribute__ ((section("FLASH2"), aligned (0x1000)))=

flash_mem_t myFlashCopy;
bool saveToFlash;

//====================================== Encoder Dial ======================================

void encoder() {
  if(digitalRead(ENC_B)) --EncVal;
  else ++EncVal;

//====================================== SETUP ==========================================

  void setup()

    if (debug1)
      while (!Serial); // wait fot the serial port to connect
      Serial.println("Serial started");

    // Set the pin functions
    //pinMode (UpPbPin, INPUT_PULLDOWN);
    //pinMode (DnPbPin, INPUT_PULLDOWN);
    pinMode (ENC_A, INPUT);
    pinMode (ENC_B, INPUT);
    pinMode (ENC_PB, INPUT);
    attachInterrupt(digitalPinToInterrupt(ENC_A), encoder, RISING);
    pinMode(MAX485_DE, OUTPUT);

    // Start the OLED and display a text
    u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print()
      u8g2.drawStr(0,16,"PLEASE WAIT");    
    } while (u8g2.nextPage());
    if (debug1){Serial.print("OLED started");}

    // Init in receive mode
    digitalWrite(MAX485_DE, 0);

    // Modbus communication runs at 9600 baud
    Serial1.begin(9600, SERIAL_8N2);
    if (debug1){Serial.print("Modbus started");}
    // Modbus slave ID 1 and serial port 1 (for nano 33 BLE)
    node.begin(1, Serial1);
    // Callbacks allow us to configure the RS485 transceiver correctly

    // Start the on board sensor (nano 33 BLE)
    if (debug1){Serial.print("IMU started");}
    // Start the BLE device
    //if (!BLE.begin()) {
    //  Serial.println("starting BLE failed!");
    //while (1);
    // Initialise the millisecond memory value to start the timers working properly

    // Get parameter values from EEPROM
    //TO DO

    // Copy the flash memory to a global structure
    memcpy((uint8_t *)(&myFlashCopy),(uint8_t *)(&myFlash), 256);

    // Turn off the onboard power LED to save power
    digitalWrite(LED_PWR, LOW);

    // debug message
    if (debug1){Serial.print("SETUP complete");}