Strange data logging errors

I have been working on a custom model rocket flight computer and im having the weirdest error when data logging data from a BN0O85 and BMP390. What happens is that after data logging for a few seconds the Sd fails to open the file and disconnects. I tried running the logging code with just the BMP390 and it worked fine. Im very confused why its not working with the BN0O85 hooked up. It also makes like 3 flies in the Sd card and they are corrupted sometimes.
I am using a Teensy 4.0 but the code is exactly the same for now you would run it on arduino.
I also have a Bluetooth module hooked up.

Here is the code that worked :

/*
  SD card test 
   
 This example shows how use the utility libraries on which the'
 SD library is based in order to get info about your SD card.
 Very useful for testing a card when you're not sure whether its working or not.
 	
 The circuit:
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila, pin 7 on Teensy with audio board
 ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
 ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila, pin 14 on Teensy with audio board
 ** CS - depends on your SD card shield or module - pin 10 on Teensy with audio board
 		Pin 4 used here for consistency with other Arduino examples

 
 created  28 Mar 2011
 by Limor Fried 
 modified 9 Apr 2012
 by Tom Igoe
 */
 // include the SD library:
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
int alt = 0;


#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp;

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
//SdSpiConfig(
  //SD_SCK_MHZ(16);
int ok;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// Teensy audio board: pin 10
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
// Wiz820+SD board: pin 4
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
const int chipSelect = 10;

void setup()
{
   Serial.println("Adafruit BMP388 / BMP390 test");

  if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
  //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode  
  //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
    Serial.println("Could not find a valid BMP3 sensor, check wiring!");
    
  }

  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);
  //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  //SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  //SPI.setSCK(14);  // Audio shield has SCK on pin 14
  
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect.
  }


  Serial.print("\nInitializing SD card...");


  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
  
    while(1);
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }
File dataFile = SD.open("datalog.txt", FILE_WRITE);
  Serial.println(dataFile);
}

void loop() {
    if (! bmp.performReading()) {
    Serial.println("Failed to perform reading :(");
   
    while(1);
  }
 if(abs(bmp.readAltitude(1013.25) >= alt)) {
          alt++;
        }
        if(abs(bmp.readAltitude(1013.25) <= alt)) {
          alt--;
        }
        Serial.println(bmp.readAltitude(1013.25) - alt);
  
 File dataFile = SD.open("datalog.csv", FILE_WRITE);
 if (dataFile) {
dataFile.println(bmp.readAltitude(1013.25) - alt);
  }
}

And here is the code that doesn't work:

#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_UART.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#include <SD.h>
#include "Adafruit_BNO08x_RVC.h"
#include <Arduino.h>
#include "BluefruitConfig.h"

const int chipselect = 10;

#if SOFTWARE_SERIAL_AVAILABLE
  #include <SoftwareSerial.h>
#endif
 #define FACTORYRESET_ENABLE         1
 #define MINIMUM_FIRMWARE_VERSION    "0.6.6"
 #define MODE_LED_BEHAVIOUR          "MODE"

 #ifdef Serial1    
  #define BLUEFRUIT_HWSERIAL_NAME      Serial1
#endif

File file_85;
 Adafruit_BluefruitLE_UART ble(Serial1, BLUEFRUIT_UART_MODE_PIN);
Adafruit_BNO08x_RVC rvc = Adafruit_BNO08x_RVC();

// Boot status rgb led
Sd2Card card;
SdVolume volume;
SdFile root;

const int blue = 8;
const int red = 7;
const int green = 6;

// Flight status rgb led

const int blue2 = 5;
const int red2 = 4;
const int green2 = 3;
int apogee2;
int stopalt;
int alt = 0.00;
int startime;
int stoptime;
int apogee;
int flightime;
int launchdet;
int fulltime;
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BMP3XX bmp;

boolean onetime = false;
boolean onetime1 = false;
boolean onetime2 = false;
boolean onetime3 = false;
boolean onetime4 = false;
boolean onetime5 = false;
boolean onetime6 = false;
int flight = 0;
int flight2;

unsigned long previousTime= 0;
const unsigned long check = 30000;
unsigned long previousTime2= 0;
const unsigned long check2 = 10;
unsigned long previousTime3 = 0;

void setup() {
  Serial.begin(115200);
  while(!Serial); //<------------ REMOVE BEFORE FLIGHT!!!!!!!
   Serial1.begin(115200);
   Serial4.begin(115200);
  pinMode(blue, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue2, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(green2, OUTPUT);

  digitalWrite(blue, HIGH);
// bluetooth setup
Serial.println("Bluetooth setup");
  if (!ble.begin(&Serial1)) {
    Serial.println("Bluetooth out!");
    digitalWrite(blue, LOW);
    digitalWrite(red, HIGH);
    while(1);
  }
  delay(20);
  Serial.println("Bluetooth found!");
  
  if(FACTORYRESET_ENABLE) {
    Serial.println("Starting bluefruit");
    if(!ble.factoryReset()) {
      Serial.println("failed startup :(");
    }
  }
  delay(20);
  ble.echo(false);
  Serial.println("Blutooth info:");
  ble.info();
  delay(20);
  Serial.println("Bluetooth all set up!");
  delay(20);
  ble.println("Howdy, welcome to Ultimate!");
  ble.println("Setting up chips..");
  Serial.println("BNO085 setup next");
delay(20);
  if(!rvc.begin(&Serial4)) {
    Serial.println("BNO085 out!");
    ble.print("BNO085 out!");
    digitalWrite(blue, LOW);
    digitalWrite(red, HIGH);
    while(1);
  }
  delay(20);
  Serial.println("BNO085 all set up!");
  ble.print("..");
  delay(20);
  Serial.println("BMP390 next");
  delay(20);

 bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);

  if(!bmp.begin_I2C()) {
    Serial.println("BMP390 not found :(");
    digitalWrite(blue, LOW);
    digitalWrite(red, HIGH);
    ble.print("BMP390 not found :(");
    while(1);
  }
 

  Serial.println("BMP390 all good!");
  ble.print("..");
  Serial.println("SD card up next!");
  
  if(!SD.begin(chipselect )){
    Serial.println("No SD card found! Is it in the soket?");
    ble.print("No SD card found! Is it in the soket?");
    digitalWrite(blue, LOW);
    digitalWrite(red, HIGH);
    while(1); 
  }
  delay(20);
  Serial.println("SD card found!");
  ble.println("Done!");

  digitalWrite(blue, LOW);
 
  
  
 
File file_85 = SD.open("Datalog.csv", FILE_WRITE);
if(file_85){
  file_85.println("------------");
  file_85.println("            ");
  file_85.println(" powered on ");
  file_85.println("            ");
  file_85.println("------------");
  file_85.close();
}

 

  
  
  
 

}
void loop() {
 
  digitalWrite(green, HIGH); 
  BNO08x_RVC_Data heading;   
    if (!rvc.read(&heading)) {
    return;
  }
  Serial.println(bmp.readAltitude(1013.25) - alt);
  digitalWrite(green2, HIGH);
  if(abs(heading.z_accel >= 5.0)) {
    if(onetime2 == false){
      digitalWrite(green2, LOW);
      digitalWrite(blue2, HIGH);
      ble.println("Launch!");
       
     // file_events.println(millis());
        // file_events.print("   ");
         //file_events.println("Acceleration detected launch");
         //file_events.close();
      onetime2 = true;
      
    }
    launchdet++;
    
    
  }else{

  if(bmp.readAltitude(1013.25) - alt > 0.0) {
      
    if(onetime == false) {
      ble.println("Zeroing alt....");
       digitalWrite(green2, LOW);
      digitalWrite(red2, HIGH);
      onetime = true;
    }
    if(abs(bmp.readAltitude(1013.25) >= alt)) {
      alt++;
    }
    if(abs(bmp.readAltitude(1013.25) <= alt)) {
      alt--;
    }
  }else{
    if(onetime1 == false) {
      ble.print("Done!");
      ble.println("Now ready for flight.");

      
        digitalWrite(red2, LOW);
         digitalWrite(green2, HIGH);
      onetime1 = true;
    }
  }
  }
    flight2 = flight;
    flight = bmp.readAltitude(1013.25) - alt;
if(launchdet >= 1.0){
    if (flight > flight2) {
       startime = millis();
       if(onetime2 == true){
       
        // file_events.println(millis());
        // file_events.print("   ");
        // file_events.print("Altitude detected launch");
         //file_events.close();
         onetime2 = true;
       }
    }else if (flight < flight2) {
       if(onetime3 == false) {
         // file_events = SD.open("Events.csv", FILE_WRITE);
         //file_events.println(millis());
         //file_events.print("   ");
         //file_events.print("Apogee");
         //file_events.println(bmp.readAltitude(1013.25) - alt);
         apogee2 = bmp.readAltitude(1013.25) - alt * 3.3;
         apogee = millis();

         if(apogee > 1.0) {
          flightime = apogee - startime;
         }
        //file_events.println("flight time =");
      //  file_events.print(flightime);
       // file_events.close();
         onetime3 = true;
       }
    }
    if(bmp.readAltitude(1013.25) - alt > 20) {
      if(bmp.readAltitude(1013.25) - alt < 20){
        if(onetime5 == false){
        stoptime = millis();
       //file_events = SD.open("Events.csv", FILE_WRITE);
        // file_events.println(millis());
        // file_events.print("   ");
        // file_events.print("Rocket has landed");
        
         fulltime = stoptime - startime;
         digitalWrite(blue2, LOW);
         digitalWrite(green2, HIGH);
         //file_events.println("Full flight time:");
         //file_events.print("   ");
        // file_events.print(fulltime);
        //file_events.close();
         onetime5 = true;
        }
      }
    }
}
 unsigned long currentTime = millis();
  if (currentTime - previousTime >= check){
    ble.println("Checking chips...");
    if (!rvc.begin(&Serial4)) { // connect to the sensor over hardware serial
    Serial.println("Could not find BNO08x!");
    ble.println("BNO085 Out!");
    // file_events = SD.open("Events.csv", FILE_WRITE);
        // file_events.println(millis());
         //file_events.print("   ");
         //file_events.print("BNO085 out!");
         //file_events.close();
    digitalWrite(blue2, LOW);
    digitalWrite(green2, LOW);
    digitalWrite(red2, HIGH);
  }
  if (SD.begin(chipselect)) {
    Serial.println("Card Good");
     digitalWrite(blue2, LOW);
     digitalWrite(green2, LOW);
    digitalWrite(red2, HIGH);
  
  }
  if (!bmp.begin_I2C()) {   
    Serial.println("Could not find a valid BMP3 sensor, check wiring!");
    ble.println("BMP390 out!");
    //File file_events = SD.open("Events.csv", FILE_WRITE);
        // file_events.println(millis());
        // file_events.print("   ");
        // file_events.print("BMP390 out!");
        // file_events.close();
          digitalWrite(blue2, LOW);
          digitalWrite(green2, LOW);
          digitalWrite(red2, HIGH);
}
 //file_events = SD.open("Events.csv", FILE_WRITE);
         //file_events.println(millis());
         //file_events.print(" All good!");
  ble.print("All good!");
   previousTime = currentTime;
}


 //file_events.close();
if (!SD.begin(chipselect)) {
    Serial.println("Card out");
     digitalWrite(blue2, LOW);
     digitalWrite(green2, LOW);
    digitalWrite(red2, HIGH);
  
  }
   if (currentTime - previousTime >= check2){
file_85 = SD.open("BNO085.csv", FILE_WRITE);
 if(!file_85){
  Serial.println("File log fail");
 }


 if(file_85){
 file_85.println(bmp.readAltitude(1013.25) - alt);
 file_85.print(heading.x_accel);
 file_85.print("   ");
 file_85.print(heading.yaw);
 file_85.print("   ");
 file_85.print(heading.pitch);
 file_85.print("   ");
 file_85.print(heading.y_accel);
 file_85.print("   ");
file_85.print(heading.z_accel);
 file_85.print("   ");
 file_85.close();
 }
 


if (apogee2 > 20) {
  if(!ble.isConnected()) {
    if(onetime4 == false){
    ble.println(" Max Alt: ");
    ble.print(apogee2);
    ble.println("    ");
    ble.println(" Flight time ");
    ble.println(flightime);
    onetime4 = true;
    }
  }
}

   }
}

Could it be a power issue? Does your microSD module have a voltage regulator on it? If so, are you powering the module with 5V? Or maybe you're right on the bleeding edge, and adding the BNO085 is a step too far.

Guess it's a power issue, too. Remove the BT-Module and see if it works.

It is bad practice to open the SD file each time round in the loop. Do it once in setup. Just because it works in the code that works, it doesn't mean that it isn't the problem in the code that doesn't.

Its on a PCB and the powering seems ok, at about 3.29V to the SD card module. With the BMP390 data logging only all of the other chips where powered on but not doing anything , and worked. For the SD card module i am using this.

Have you checked the power with a oscilloscope?

I dont have one so im going to try re writing the code without the bt module and if it works build the code from there.

I rewrote it without the bluetooth and it worked, added it back and it didn't, so there's my problem.

So it could be your Bluetooth code, or the additional power drawn by the Bluetooth module when it's connected, or when it's transmitting.

Managed to get it to work, turns out some code for the BT was messing with the CS pin, i don't know why it still connected but i got it to work.