String and Sd issue [HELP]

Hello,

I have a probleme because i'm writing datas coming from a GPS in a file .txt ]:smiley:

The probleme is that I use the date of the first LOOP to create the file.txt.

My code looks like that :

String sDate = String(year) + String(month) + String(day)

And i would to use sDate to create the .txt file...

myfile = sd.open(sDate , FILEWRITE)

The problem is that it doesn't work because the good use of sd.open is

myfile = sd.open("test.txt",FILEWRITE)

Do you have any idea to use the sDate variable and not always the same name "test.txt" ??? :grin:

Thank you

You can copy the String object to a character array:

  char buffer[20];
  sDate.toCharArray(buffer, 20);
  myfile = sd.open(buffer , FILEWRITE);

yes but it doesn't work because the sd.open function need the .txt to create the file... :fearful:

so i have tried String sDate = String(year) + String(month) + String(day) +String(".txt") but it doesn't work...

You could use "sprintf" and avoid using String altogether

You think to that ???

char cDate[32];
sprintf(cDate,"%s.txt", sDate);
myfile = sd.open(cDate, FILEWRITE);

I was thinking more like

char cDate[32]; // whatever
sprintf(cDate,"%02d%02d%02d.txt", year, month, day);

COOOOOOOOOOOOOOOOL $)

It works good...but the complete sentence doesn't works :

** sprintf(cDate,"Log_du_%02d%02d%02d_a_%02d%02d%02d.txt",day,month,year,hour,minute,second);**

Do you have any idea ?

clement60200:
Do you have any idea ?

Post the full code, or at least, a small sketch that compiles and demonstrates the problem.

Sigh…
http://forum.arduino.cc/index.php/topic,148850.msg1118324.html#post_codetags

Sorry i’m a beginner :roll_eyes:
the full code is here :

#include <SD.h>

File myFile;
int test = 0;
int iteration =0;

#include <SoftwareSerial.h>
#include <TinyGPS.h>

// Define which pins you will use on the Arduino to communicate with your 
// GPS. In this case, the GPS module's TX pin will connect to the 
// Arduino's RXPIN which is pin 3.
#define RXPIN 2
#define TXPIN 3
//Set this value equal to the baud rate of your GPS
#define GPSBAUD 4800

// Create an instance of the TinyGPS object
  TinyGPS gps;
// Initialize the NewSoftSerial library to the pins you defined above
SoftwareSerial uart_gps(RXPIN, TXPIN);

// This is where you declare prototypes for the functions that will be 
// using the TinyGPS library.
void getgps(TinyGPS &gps);

// In the setup function, you need to initialize two serial ports; the 
// standard hardware serial port (Serial()) to communicate with your 
// terminal program an another serial port (NewSoftSerial()) for your 
// GPS.
void setup()
{
  // This is the serial rate for your terminal program. It must be this 
  // fast because we need to print everything before a new sentence 
  // comes in. If you slow it down, the messages might not be valid and 
  // you will likely get checksum errors.
  Serial.begin(115200);
  //Sets baud rate of your GPS
  uart_gps.begin(GPSBAUD);
  
  Serial.println("");
  Serial.println("GPS Shield QuickStart Example Sketch v12");
  Serial.println("       ...waiting for lock...           ");
  Serial.println("");
 
 
 
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  if (SD.exists("log.txt")) {
    Serial.println("log.txt exists.");
  }
  else {
    Serial.println("log.txt doesn't exist.");
  }

  // open a new file and immediately close it:
  Serial.println("Creating log.txt...");
 
 test =0;
 iteration =0;
 
 
}

// This is the main loop of the code. All it does is check for data on 
// the RX pin of the ardiuno, makes sure the data is valid NMEA sentences, 
// then jumps to the getgps() function.
void loop()
{
  while(uart_gps.available())     // While there is data on the RX pin...
  {
      int c = uart_gps.read();    // load the data into a variable...
      if(gps.encode(c))      // if there is a new valid sentence...
      {
        getgps(gps);         // then grab the data.
      }
  }
}

// The getgps function will get and print the values we want.
void getgps(TinyGPS &gps)
{
  // To get all of the data into varialbes that you can use in your code, 
  // all you need to do is define variables and query the object for the 
  // data. To see the complete list of functions see keywords.txt file in 
  // the TinyGPS and NewSoftSerial libs.
  
  // Define the variables that will be used
  float latitude, longitude;
  String date;
  // Then call this function
  gps.f_get_position(&latitude, &longitude);
  // You can now print variables latitude and longitude
 
  Serial.print("Lat/Long: "); 
  Serial.print(latitude,4); 
  Serial.print(", "); 
  Serial.println(longitude,5);

  // Same goes for date and time
  int year;
  byte month, day, hour, minute, second, hundredths;
  gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
  // Print data and time

  Serial.print("Date: "); Serial.print(month, DEC); Serial.print("/"); 
  Serial.print(day, DEC); Serial.print("/"); Serial.print(year);
  Serial.print("  Time: "); Serial.print(hour, DEC); Serial.print(":"); 
  Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); 
  Serial.print("."); Serial.println(hundredths, DEC);
  //Since month, day, hour, minute, second, and hundr
  
  // Here you can print the altitude and course values directly since 
  // there is only one value for the function
  Serial.print("Altitude (meters): "); Serial.println(gps.f_altitude());  
  // Same goes for course
  Serial.print("Course (degrees): "); Serial.println(gps.f_course()); 
  // And same goes for speed

  Serial.print("Speed(kmph): "); Serial.println(gps.f_speed_kmph());
  Serial.println();
  
  // Here you can print statistics on the sentences.
  unsigned long chars;
  unsigned short sentences, failed_checksum;
  gps.stats(&chars, &sentences, &failed_checksum);
  
  
  char cDate[100]; // whatever

if (iteration >4){

  if (test==0){
     sprintf(cDate,"Log_du_%02d%02d%02d_a_%02d%02d%02d.txt",day,month,year,hour,minute,second);
      test=1;}
   
        
  myFile = SD.open(cDate, FILE_WRITE);
  if (myFile) { 
    
    Serial.print("Writing to file");   
    myFile.println(latitude,5);myFile.println(",");myFile.println(longitude,5);myFile.println(",");}
    myFile.close();}
    delay(1000);
    iteration = iteration+1;
    
}

In fact it compiles but it doesn’t work…

That’s strange because the first code below works but not the second… :grin:

sprintf(cDate,"%02d%02d%02d.txt", year, month, day); sprintf(cDate,"Log_du_%02d%02d%02d_a_%02d%02d%02d.txt",day,month,year,hour,minute,second)

Is it possible that the FAT filesystem only allows 8 character filenames?

hummm yes... so it means that there's no solution ? :frowning:

See reply #3, or if you've got more than one file type per day, change the extension.