Micro Sd card slot trouble

Hi,
I am working on a way to log data from a pressure sensor onto a micro SD card but it is proving difficult. My problem is that after I run the sketch and open up the micro SD card the file is there but there is no data on the file.
I have been able to identify the micro SD card using the "Cardinfo" Arduino library. I have been able to log example data to it using the "Datalogger" Arduino library. And I have tried printing out the values that would be logged and the results are perfect proving that the sensor works. I am clueless as to what is going on and need help.
Thanks for your time.

I am using an Adafruit Micro SD card slot

Here is my code


#include <Wire.h>
#include <LPS.h>
#include <SPI.h>
#include <SD.h>
float altitude_difference = 0;
const int chipSelect = 10;

float TimeKeeper=0;
float alt_present = 0;
float  startingPressure=0;
float Velocity = 0;
float Altitude=0;
float Altitude1 = 0;
  int launch = 0;
  int trigger=0;
float Apogee=1;
float Maxvel = 0;

LPS ps;

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  if (!ps.init())
  {
    Serial.println("Failed to autodetect pressure sensor!");
    while (1);
  }

pinMode(10, OUTPUT);
SD.begin(chipSelect);
  ps.enableDefault();
   float pressure = ps.readPressureMillibars();
  float altitude = ps.pressureToAltitudeMeters(pressure);
 Altitude1 = altitude;
 

}

void loop()
{

  File flight1 = SD.open("Fli.txt", FILE_WRITE);
  
  float pressure = ps.readPressureMillibars();
  float altitude = ps.pressureToAltitudeMeters(pressure);
 Altitude= altitude - Altitude1;
 float alt_past = altitude;
float Velocity_past = Velocity;
 

  {
    delay(30);
    float pressure = ps.readPressureMillibars();
  float altitude = ps.pressureToAltitudeMeters(pressure);
  float alt_present = altitude;
  
float altitude_difference= alt_present - alt_past;
 

Velocity = altitude_difference+ 0.05 /0.094;
float Velocity_past = Velocity;

delay(30);

 


  }
  

  if (flight1) {
    flight1.println(Altitude);
  if (launch <= 1) {
 if(Altitude>1.5) {  Serial.print("p: ");

  
 flight1.print(pressure);
  flight1.print(" mbar\ta: ");
  flight1.print(altitude);
  flight1.print(" m\tt: ");
 // Serial.print( temp.temperature );
 flight1.print(" deg C");
  flight1.print(Altitude);
  flight1.print("      ");

  flight1.println(Velocity);
 
  launch = 1;

 }
  }

pressure = ps.readPressureMillibars();
 altitude = ps.pressureToAltitudeMeters(pressure);
  alt_present = altitude;

  altitude_difference= alt_present - alt_past;
 

Velocity = altitude_difference + 0.05/0.094;
 float Velocity_present = Velocity;

if (Maxvel <= 0) {
  if (Velocity_past > Velocity_present) {
float    Max_velocity = Velocity;
flight1.println(Max_velocity);
 Maxvel = 1;
  }
}
  

 
  
   delay(30); 




  
  //unsigned long currentTime = millis();
  
 // if (currentTime - previousTime >= eventInterval) {
    //previousTime = currentTime;
 if (launch>=1) {
 if (Apogee <= 1) {
  if (Velocity < 1.5) {
 launch = 2;
  
flight1.println ("Apogee");

//Serial.print ("    ");
//Serial.print (Altitude);

  }
 }
 }
 
if(launch>=2) {
  if ( Velocity <= -1) {
flight1.print("      ");
flight1.print ("descent");
flight1.print(pressure);
  flight1.print(" mbar\ta: ");
  flight1.print(altitude);
  flight1.print(" m\tt: ");
 // Serial.print( temp.temperature );
  flight1.print(" deg C");
  flight1.print(Altitude);
  flight1.print("      ");

  flight1.println(Velocity);

Apogee = 2;
flight1.close();
  }
}
 }
}





I use append instead of open.

void fSendToFile( void * parameter )
{
  if (!SD.begin())
  {
    log_i("Card Mount Failed");
  } else {
    xSemaphoreGive( sema_SD );
  }
  String toFile = "";
  toFile.reserve( toFileSize );
  for (;;)
  {
    if ( xQueueReceive(Q_toFIle, &toFile, portMAX_DELAY) == pdTRUE )
    {
      //log_i( "%s %d\n", toFile.c_str(), toFile.length() );
      xSemaphoreTake( sema_SD, portMAX_DELAY );
      appendFile(SD, "/data.txt", toFile.c_str() + '\n');
      xSemaphoreGive( sema_SD );
      toFile = "";
    }
    //log_i( " high watermark %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}
////
void writeFile(fs::FS &fs, const char * path, const char * message) {
  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    log_i("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    log_i("File written");
  } else {
    log_i("Write failed");
  }
}
////
void appendFile(fs::FS &fs, const char * path, const char * message) {
  //log_i("Appending to file: %s\n", path);
  File file = fs.open(path, FILE_APPEND);
  if (!file)
  {
    log_i("Failed to open file for appending");
    return;
  } else {
    file.print(message);
    file.close();
  }
}
////
void renameFile(fs::FS &fs, const char * path1, const char * path2) {
  log_i("Renaming file %s to %s\n", path1, path2);
  if (fs.rename(path1, path2)) {
    log_i("File renamed");
  } else {
    log_i("Rename failed");
  }
}
////
//void renameFile(fs::FS &fs, const char * path1, const char * path2) {
//  log_i("Renaming file %s to %s\n", path1, path2);
//  if (fs.rename(path1, path2)) {
//    log_i("File renamed");
//  } else {
//    log_i("Rename failed");
//  }
//}
////
void deleteFile(fs::FS &fs, const char * path) {
  log_i("Deleting file: %s\n", path);
  if (fs.remove(path)) {
    log_i("File deleted");
  } else {
    log_i("Delete failed");
  }
}

Be careful, you are always opening the file at the start of loop, but only closing it under certain conditions (at the end of the last if statement).

If you uncomment the lines that print what is being written to the file does the data look correct ?

Incidentally, this line

  Altitude = altitude - Altitude1;

is a best confusing. Variable names where the only difference is the case of the first letter is not really the best way to name them

Yes.

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