No matching function for call to 'SDClass::open(String&, int)'

Hi all.
the sketch got error at: File dataFile = SD.open(filename, FILE_WRITE);
if modified as: File dataFile = SD.open( "filename", FILE_WRITE); got compiling passed, but nothing wrote on SD card. why?
Thanks
Adam

 // Arduino code for NEO-6M + SD Card GPS Tracker
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

TinyGPS gps; // GPS initialization
SoftwareSerial ss(6, 5); // pins for GPS (D5 -> GPS RX, D6 -> GPS TX)

static void smartdelay(unsigned long ms); // delay that also takes into account GPS
bool valid_date = false; // used for naming sd file
String filename = ""; // dummy placeholder, this will be updated later based on timestamp
String prev_date = ""; // ensuring the same data points aren't saved more than once

void setup() {
  Serial.begin(9600);
 
  if (!SD.begin(chipSelect)) {
    while (1); // do nothing if SD is not started
  }
  ss.begin(9600); // start GPS module
}

void loop() {
 
  float flat, flon;
  unsigned long age;
  String date_str;
  gps.f_get_position(&flat, &flon, &age);
  String datastring = "";
  date_str = find_date(gps,prev_date);
  prev_date = date_str;
  datastring+=date_str; datastring+=",";
  datastring+=String(flon,6); datastring+=",";
  datastring+=String(flat,6); datastring+=",";
  datastring+=String(gps.f_altitude(),6); datastring+=",";
  datastring+=String(gps.f_course(),2); datastring+=",";
  datastring+=String(gps.f_speed_kmph(),5);

  if (date_str=="NaN"){
  } else {
    if (valid_date==false){
      valid_date = true;
      filename = filenamer(gps);
      Serial.println(filename);
      File dataFile = SD.open(filename, FILE_WRITE);
      if (dataFile){
        dataFile.println("Date [mm/dd/yyyy HH:MM:SS],Longitude [deg],"
        "Latitude [deg],Altitude [m],Heading [deg],Speed [kmph]"); // alter based on data
        dataFile.close();
      } else {
        Serial.println("Issue with saving header");
      }
    } else {
      // open file, write to it, then close it again
      File dataFile = SD.open(filename, FILE_WRITE);
      if (dataFile) {
        Serial.print("data string: ");
        Serial.println(datastring);
        dataFile.println(datastring);
        dataFile.close();
      }
    }
  }

  smartdelay(1000);
}

static void smartdelay(unsigned long ms) {
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

String find_date(TinyGPS &gps,String prev_date) {
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  String date_str = "";
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    date_str = "NaN";
  else
  {
    int byte_size = 19;
    char chars[byte_size];
    sprintf(chars, "%02d/%02d/%02d %02d:%02d:%02d",
          month,day, year, hour, minute, second);
    for (int ii=0;ii<=byte_size-1;ii++){
      date_str+=String(chars[ii]);
    }
   
  }
  if (date_str==prev_date){
    return "NaN";
  }
  return date_str;
}

String filenamer(TinyGPS &gps) {
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  String filename = "";
  int byte_size = 8;
  char chars[byte_size];
  sprintf(chars, "%02d%02d%02d%02d",
        day, hour, minute, second);
  for (int ii=0;ii<=byte_size-1;ii++){
    filename+=String(chars[ii]);
  }
  return filename+".csv";
}

ERROR:

Arduino: 1.8.19 (Windows 7), Board: "Arduino Uno"

E:\ENGINEERING\DIY\Electronic\ESP32\good\3 EYE_clock_game_Stream_health_GPS_sth\00_map\Arduino GPS Tracker\Arduino_GPS_Tracker\Arduino_GPS_Tracker.ino: In function 'void loop()':

Arduino_GPS_Tracker:55:51: error: no matching function for call to 'SDClass::open(String&, int)'

       File dataFile = SD.open(filename, FILE_WRITE);

                                                   ^

Multiple libraries were found for "SoftwareSerial.h"

 Used: C:\Users\HUA.DELLV-PC\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SoftwareSerial

 Not used: C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\SoftwareSerial-master

In file included from E:\ENGINEERING\DIY\Electronic\ESP32\good\3 EYE_clock_game_Stream_health_GPS_sth\00_map\Arduino GPS Tracker\Arduino_GPS_Tracker\Arduino_GPS_Tracker.ino:12:0:

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\SD/SD.h:74:8: note: candidate: File SDClass::open(const char*, uint8_t)

   File open(const char *filename, uint8_t mode = FILE_READ);

        ^~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\SD/SD.h:74:8: note:   no known conversion for argument 1 from 'String' to 'const char*'

Arduino_GPS_Tracker:65:51: error: no matching function for call to 'SDClass::open(String&, int)'

       File dataFile = SD.open(filename, FILE_WRITE);

                                                   ^

In file included from E:\ENGINEERING\DIY\Electronic\ESP32\good\3 EYE_clock_game_Stream_health_GPS_sth\00_map\Arduino GPS Tracker\Arduino_GPS_Tracker\Arduino_GPS_Tracker.ino:12:0:

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\SD/SD.h:74:8: note: candidate: File SDClass::open(const char*, uint8_t)

   File open(const char *filename, uint8_t mode = FILE_READ);

        ^~~~

C:\Users\HUA.DELLV-PC\Documents\Arduino\libraries\SD/SD.h:74:8: note:   no known conversion for argument 1 from 'String' to 'const char*'

exit status 1

no matching function for call to 'SDClass::open(String&, int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

It's telling you that you can't use a String for the filename. You have to use a char array to hold the filename. The String class has a method that will return the underlying char array if you just want to use that. See the documentation on String.

Particularly, this function:

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