我正在尝试使用SD卡模块写入SD卡。我正在使用这段代码,它的工作非常好!
void setup(void)
{
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println();
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines and turn on backlight
// ------- Quick 3 blinks of backlight -------------
lcd.clear();
Serial.print("Initializing SD card..."); // initialize the SD card
if (!SD.begin(CS_pin)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename))
{
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
date_time();
#if ECHO_TO_SERIAL
Serial.println(" ");
#endif //ECHO_TO_SERIAL
logfile.println("SECONDS, ML, PSI, FLOW");
#if ECHO_TO_SERIAL
Serial.println("SECONDS, ML, PSI, FLOW");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
data();
}
这是完美的工作但是当我将它添加到更大的程序时,我使用相同的方法,除了我创建它后无法打开文件!
此行返回0天气或文件是否存在!
logfile = SD.open(filename, FILE_WRITE);
我觉得我犯了一个愚蠢的错误,但我无法理解!正在创建文件但未打开!
非常感谢您的帮助