写入SD卡arduino

时间:2014-05-06 21:09:30

标签: c++ c arduino sd-card

我的Arduino遇到了麻烦。我试图为我的文件创建一个动态名称,并将其转换为char数组,就像我在本网站上的一些帖子中看到的那样。

这是我编写函数的方式:

void writeSD()
{ 
  DateTime dt = RTC.now();

  String temp = "DIVE";
  temp += dt.day();
  temp += dt.month();
  temp += dt.year();
  temp += ".txt";

  Serial.println(temp);

  char filename[temp.length()+1];
  temp.toCharArray(filename, sizeof(filename));
  myFile = SD.open(filename, FILE_WRITE);  

  if (myFile) {
    Serial.print("Writing to txt file...");
    myFile.print(dt.day(), DEC);
    myFile.print('-');
    myFile.print(dt.month(), DEC);
    myFile.print('-');
    myFile.print(dt.year(), DEC);
    myFile.print(' ');
    myFile.print(dt.hour(), DEC);
    myFile.print(':');
    myFile.print(dt.minute(), DEC);
    myFile.print(':');
    myFile.print(dt.second(), DEC);
    myFile.println();
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening txt file");
  }
}

我只有"error opening txt file"并且我不知道为什么。我的文件名称是正确的,当我在控制台上打印时,它看起来像这样:DIVE652014.TXT

提前致谢!

PS:对不起我的英语,这不是我的母语

1 个答案:

答案 0 :(得分:1)

当我从Adafruit更准确地阅读有关SD分线板的文档时,我想出了为什么这不起作用:

  

SD卡库不支持长文件名'比如我们   以前。相反,它使用8.3格式的文件名,所以保留文件   名字短!例如IMAGE.JPG很好,datalog.txt很好   "我的GPS日志文件。文字"不是!

所以我有10个字符,但它只接受文件名中的8个字符。

感谢您试图帮助我! :)

相关问题