问题
I'm facing a specific problem with SD card and Arduino. I want to create a function that creates a new file on the SD card for a different day. For now, I'm simulating calendar with ints. I know that problem was already discussed but I can't seem to find a similarly discussed problem.
Code:
#include <stdlib.h> // included for floatToString
#include <math.h>
#include <SPI.h>
#include <SD.h>
int year = 2014;
int month = 11;
int day = 4;
char dateTitle[20]; //= "0000000000.txt";
void printDateTitle(char* dateTitle, int Y, int M, int D){
//char dateTitle[20];
sprintf(dateTitle, "%4d-%02d-%02d.txt", Y, M, D);
return;
}
const int chipSelect = 4;
void setup() {
//printDateTitle(dateTitle, year, month, day);
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
}
void loop() {
Serial.print(dateTitle);
delay(1000);
File dataFile = SD.open(dateTitle, FILE_WRITE);
if (dataFile){
dataFile.println("something");
dataFile.close();
Serial.print(day);
Serial.println("something");
delay(1000);
} else
Serial.println("Error");
}
In the code, I have a function void printDateTitle
that formats the inputs from the calendar to string which I want to use as a title for the file.
And also, when I define a function in the void loop()
with printDateTitle(dateTitle, year, month, day);
I get an "Error" in the output meaning SD.open = false.
The problem is that even though SD.open returns true it doesn't create a file on the SD card. .txt is included in the char array. I have also used capital .TXT.
I would be grateful for all the bits of advice regarding the problem.
For the reference, I'm using Arduino Uno and Micro SD card Adapter with Arduino IDE.
回答1:
The Arduino SD Library documentation states that it uses the "short 8.3 names for files". Therefore only files with 8 characters as name and 3 for file extension are valid. For example: 12345678.txt is valid, 123456789.txt would be invalid.
Your date string (2014-11-04.txt) is to long because it has 10 characters instead of only 8.
Also make sure you've formatted the SD card to a FAT16 or FAT32 file system.
来源:https://stackoverflow.com/questions/47664817/sd-open-returns-true-but-does-not-create-a-file