SdFat file creation error

I have been working on a model rocket flight computer, and im having some problems with data logging. My code is a bit of a mess right now so i put some markers where the problematic code is, basically im using the SdFat SoftwareSpi set up in the code to enable dedicated SPI pins, the sd card is being found but im having issues when trying to open a file, i made sure my code was the same as the example, and it was, except for the logging stuff. When i try to run the code it fails to open the file and gives the the error code "0XC, 0XFF". I tried to fix the problem but had no luck, im not really sure what i did wrong and how to fix it.
For the microprocessor im using a Nano 33 BLE.

Here is the code, its a bit of a mess because im not great at coding.

#include "Adafruit_EEPROM_I2C.h"
#include "Adafruit_FRAM_I2C.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL375.h>
#include <SPI.h>
#include <SdFat.h>
#include "RF24.h"
#include "Adafruit_BMP3XX.h"


int stopalt;
int alt = 0.00;


#define SD_FAT_TYPE 0

const uint8_t SD_CS_PIN = 5;
const uint8_t SOFT_MISO_PIN = 8;
const uint8_t SOFT_MOSI_PIN = 7;
const uint8_t SOFT_SCK_PIN = 6;
unsigned long pre = 0;
const int inter = 180000;
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(7, 8);
const byte address[6] = "00001";
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BMP3XX bmp;
Adafruit_ADXL375 imu = Adafruit_ADXL375(12345);
Adafruit_EEPROM_I2C i2ceeprom;
#define EEPROM_ADDR 0x50
SoftSpiDriver<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> softSpi;
// Speed argument is ignored for software SPI.
#if ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)
#else  // ENABLE_DEDICATED_SPI
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(0), &softSpi)
#endif  // ENABLE_DEDICATED_SPI

#if SD_FAT_TYPE == 0
SdFat sd;
File file;
#elif SD_FAT_TYPE == 1
SdFat32 sd;
File32 file;
#elif SD_FAT_TYPE == 2
SdExFat sd;
ExFile file;
#elif SD_FAT_TYPE == 3
SdFs sd;
FsFile file;
#else  // SD_FAT_TYPE
#error Invalid SD_FAT_TYPE
#endif  // SD_FAT_TYPE

struct package 
{
  float altitude;
  float acceleration;
  char text[32];
  char text2[32];
};
typedef struct package Package;
Package data;


void displayDataRate(void)
{
  Serial.print  ("Data Rate:    ");

  switch(imu.getDataRate())
  {
    case ADXL343_DATARATE_3200_HZ:
      Serial.print  ("3200 ");
      break;
    case ADXL343_DATARATE_1600_HZ:
      Serial.print  ("1600 ");
      break;
    case ADXL343_DATARATE_800_HZ:
      Serial.print  ("800 ");
      break;
    case ADXL343_DATARATE_400_HZ:
      Serial.print  ("400 ");
      break;
    case ADXL343_DATARATE_200_HZ:
      Serial.print  ("200 ");
      break;
    case ADXL343_DATARATE_100_HZ:
      Serial.print  ("100 ");
      break;
    case ADXL343_DATARATE_50_HZ:
      Serial.print  ("50 ");
      break;
    case ADXL343_DATARATE_25_HZ:
      Serial.print  ("25 ");
      break;
    case ADXL343_DATARATE_12_5_HZ:
      Serial.print  ("12.5 ");
      break;
    case ADXL343_DATARATE_6_25HZ:
      Serial.print  ("6.25 ");
      break;
    case ADXL343_DATARATE_3_13_HZ:
      Serial.print  ("3.13 ");
      break;
    case ADXL343_DATARATE_1_56_HZ:
      Serial.print  ("1.56 ");
      break;
    case ADXL343_DATARATE_0_78_HZ:
      Serial.print  ("0.78 ");
      break;
    case ADXL343_DATARATE_0_39_HZ:
      Serial.print  ("0.39 ");
      break;
    case ADXL343_DATARATE_0_20_HZ:
      Serial.print  ("0.20 ");
      break;
    case ADXL343_DATARATE_0_10_HZ:
      Serial.print  ("0.10 ");
      break;
    default:
      Serial.print  ("???? ");
      break;
  }
  Serial.println(" Hz");
}

void setup() {
  Serial.begin(9600);
   while(!Serial);
    delay(900);
  if (i2ceeprom.begin(0x50)) {
    Serial.println("fram online");
  }else{
    Serial.println("fram in trouble");
    digitalWrite(3, HIGH);
    while(1);
  }
  if(!imu.begin(0x53)){
    Serial.println("ADXL375 all in trouble");
    digitalWrite(3, HIGH);
    while(1);
  }
  if(!bmp.begin_I2C(0x77)) {
    Serial.println("BMP390 in trouble!");
    digitalWrite(3, HIGH);
    while(1);
  }
  if(!sd.begin(SD_CONFIG)) {    
    Serial.println("SD in trouble!");
    digitalWrite(3, HIGH);
   
    while(1);
  }
  if (!radio.begin()) {
    Serial.println(F("Radio not found!"));
    
    
  }
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

  bmp.setPressureOversampling(BMP3_OVERSAMPLING_32X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_127);
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setOutputDataRate(BMP3_ODR_200_HZ);
  
  
  
  delay(2000);
if(!file.open("data_log", O_RDWR | O_CREAT)) { // this is where i think something is going wrong
  sd.errorHalt(F("open failed"));
 }
   Serial.println("we all good amego!");
}
  void loop() {
    Wire.beginTransmission(0x77);
    Wire.beginTransmission(0x53);
    Wire.beginTransmission(0x50);
 
   
      sensors_event_t a, g, temp, event;
      imu.getEvent(&event);

      if(abs(event.acceleration.z >= 3.0)) {
        stopalt++;
      }
      if(stopalt >= 1.0) {
        //stop zeroing alt
      }else{
        if(abs(bmp.readAltitude(1013.25) >= alt)) {
          alt++;
        }
        if(abs(bmp.readAltitude(1013.25) <= alt)) {
          alt--;
        }
      }
    
        int sensorValue = analogRead(A6);
        float voltage = sensorValue * (5.0 / 1023.0);
   
     for (uint8_t i = 0; i < 0.001; i++) {
       file.print("alt");
       file.print(bmp.readAltitude(1013.25) - alt );
       file.print(" M| ");
       file.print(stopalt);
       file.print(" ASB| ");
       file.print(event.acceleration.z);
       file.print(" acc X|");

   }
   Serial.println(bmp.readAltitude(1013.25) - alt );
  }
  

It will be much easier to debug the problem if you start simple.

Verify that the SD card does everything you want it do, just using the software SPI library and nothing but the code required to open the file, write some random nonsense, and close it.

No radios, sensors, etc., just one of the simple examples from the SDFat library.

Yes, Software spi worked. At the moment im only doing stuff with 2 other sensors in this code, both are i2c. I will try that.

Then your mistake was to add more than one other item at a time. Start with a working system, and proceed stepwise, adding one feature at a time, to isolate the problem.

2 Likes

I did what you said and modified the example to what i was doing in my code for data logging and it worked, so im not sure what is going on.

Glad it works. That sort of mysterious self-repair happens to me from time to time, and I attribute it to some mistake that I overlooked the first time around.

sorry, i meant it didnt work on my code. It worked when i replicated the logging in Software spi.

At least you know that software SPI is not the problem.

1 Like

For some bizarre reason some code to set up another chip was messing with it, but its fixed now!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.