功能和SD文件创建

时间:2014-11-09 09:08:12

标签: c++ arduino arduino-uno

我是Arduino和C ++的新手我正在尝试编写一些函数,这些函数用于在SD卡上存储带有可变文件名的数据,我遇到了麻烦。循环期间的输出显示:“错误打开:9/11/2014 15:57:04,行内容= 2014-records.txt”

提前致谢

#include <SD.h> //SD Card Library
include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>


File myFile;


//Functions

String record_to_file(String(content)){
  String filename = String(filenamecreate()); 
  char fileNameCharArray[filename.length() + 1];
  filename.toCharArray(fileNameCharArray, filename.length()+1);
  myFile = SD.open(fileNameCharArray, FILE_WRITE);
  if (myFile) {
    myFile.println(content);
    myFile.close();
    return String("Data Written: " + content + "=" + filename);
  } else {
    return String("Error Opening: " + content + "=" + fileNameCharArray);
  }
}

String print2digits(int number) {
  if (number >= 0 && number < 10) {
    return(String('0' + String(number)));
  }
  return(String(number));
}

String dateString() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    String timeLog = String(tm.Day) + "/" + String(tm.Month) + "/" + String(tm.Year + 1970) + " " + String(print2digits(tm.Hour)) + ":" + String(print2digits(tm.Minute)) + ":" + String(print2digits(tm.Second));  
    return(timeLog);
  } else {
    if (RTC.chipPresent()) {
      return("The DS1307 is stopped please run the SetTime");
    } else {
      return("Clock may be broken");
    }
  }  
}

String filenamecreate(){
  tmElements_t tm;
  if (RTC.read(tm)) {
    return(String(tm.Year + 1970) + "-records.txt");
  } else {
   return("UnknownDate"); 
  }
}

void setup()
{
  Serial.begin(9600); while (!Serial) { ; }
  Serial.print("Initializing SD card..."); pinMode(10, OUTPUT);
  if (!SD.begin(10)) {Serial.println("Initialization of SD failed!"); return; }                              Serial.println("initialization of SD done.");

}

void loop()
{

  delay(2000);
  Serial.println(record_to_file(dateString() + "," + "Line Content"));
}

1 个答案:

答案 0 :(得分:0)

根据thisthis,文件名必须为8.3格式。