thanks Marco_c that has give me something to start with i have added it to the basic sdfatls code I have tried to fixup some of the references you had in their just checking how did you declare the midi_ext I thought it maybe like
char CSV_EXT[] = ".csv";
but I think I may be wrong some help to clean up the mess I created would be much appreciated
/*
* This sketch will list all files in the root directory and
* then do a recursive list of all directories on the SD card.
*
*/
#include <SdFat.h>
#include <SdFatUtil.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFat sd;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void setup() {
Serial.begin(9600);
PgmPrintln("Type any character to start");
// Serial.print("SS_PIN "); Serial.println(SS_PIN, DEC);
while (!Serial.available());
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
// if (!card.init(SPI_HALF_SPEED))
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
}
uint16_t createPlaylistFile(void)
// create a playlist file on the SD card with the names of the files.
// This will then be used in the menu.
{
SdFile plFile; // Playlist file
SdFile lFile; //list files
uint16_t count; // count of files
int FNAME_SIZE;
char fname[128];
char file_list[100];
char CSV_EXT[] = ".csv";
// open/create the playlist file
if (!plFile.open(file_list, O_CREAT|O_WRITE))
//LCDErrMessage("PL create fail", true);
count = 0;
while (lFile.openNext(sd.vwd(), O_READ))
{
if (lFile.isFile())
{
lFile.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0)
// only include files with MIDI extenstion
{
plFile.write(fname, 100 );
count++;
}
}
lFile.close();
}
// close the playlist file
plFile.close();
return(count);
}
void loop() {
createPlaylistFile(); }
What you have is more efficient if the compiler can't recognise multiple uses as the same thing.
Alternatively this should also work
char *csv_ext = ".csv";
You do understand that my code creates a file on the SD card with the list of names in it? This is probably not what you want, exactly.
You also should call the function once from setup() and not every time through the loop. The list of files will not change every a few milliseconds
Think about what you have done with this one - what is executed if the 'if' is true? if you are not handling an error then get rid of the if statement or call your error function instead.
if (!plFile.open(file_list, O_CREAT|O_WRITE))
//LCDErrMessage("PL create fail", true);
thanks for you that but what your code is doing is exactly what I need it to do is create a list of files and then upload those files that end with .csv to a server
I have made the changes that you recommended but still get nothing it does not create the file.
/*
* This sketch will list all files in the root directory and
* then do a recursive list of all directories on the SD card.
*
*/
#include <SdFat.h>
#include <SdFatUtil.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFat sd;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void setup() {
Serial.begin(9600);
PgmPrintln("Type any character to start");
// Serial.print("SS_PIN "); Serial.println(SS_PIN, DEC);
while (!Serial.available());
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
// if (!card.init(SPI_HALF_SPEED))
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
PgmPrintln("start of CSV List __________________>");
createPlaylistFile();
}
uint16_t createPlaylistFile(void)
// create a playlist file on the SD card with the names of the files.
// This will then be used in the menu.
{
SdFile plFile; // Playlist file
SdFile lFile; //list files
uint16_t count; // count of files
int FNAME_SIZE;
char fname[128];
char file_list[200] = "FILELIST.TXT" ;
char *CSV_EXT = ".csv";
// open/create the playlist file
!plFile.open(file_list, O_CREAT|O_WRITE);
//LCDErrMessage("PL create fail", true);
count = 0;
while (lFile.openNext(sd.vwd(), O_READ))
{
if (lFile.isFile())
{
lFile.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0)
// only include files with MIDI extenstion
{
plFile.write(fname, 100 );
count++;
}
}
lFile.close();
}
// close the playlist file
plFile.close();
return(count);
}
void loop() {
}
for some strange reason it is still not creating the file so of course it will not log to it?
/*
* This sketch will list all files in the root directory and
* then do a recursive list of all directories on the SD card.
*
*/
#include <SdFat.h>
#include <SdFatUtil.h>
Sd2Card card;
SdVolume volume;
SdFile root;
SdFat sd;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void setup() {
Serial.begin(9600);
PgmPrintln("Type any character to start");
// Serial.print("SS_PIN "); Serial.println(SS_PIN, DEC);
while (!Serial.available());
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
// if (!card.init(SPI_HALF_SPEED))
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
PgmPrintln("start of CSV List __________________>");
createPlaylistFile();
}
uint16_t createPlaylistFile(void)
// create a playlist file on the SD card with the names of the files.
// This will then be used in the menu.
{
SdFile plFile; // Playlist file
SdFile lFile; //list files
uint16_t count; // count of files
int FNAME_SIZE;
char fname[200];
char file_list[] = "FILELIST.TXT";
// #define *CSV_EXT "csv";
char *CSV_EXT = ".CSV";
!plFile.open("FILELIST.TXT", O_CREAT|O_WRITE);
// { sd.errorHalt("opening FILELIST.TXT for write failed");
// }
Serial.println(file_list);
count = 0;
while (lFile.openNext(sd.vwd(), O_READ))
{ if (lFile.isFile())
{ lFile.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
plFile.write(fname, 100);
count++;
}
}
lFile.close();
}
// close the playlist file
plFile.close();
Serial.print(fname);
Serial.println("finished");
return(count);
}
void loop() {
}
Not sure what the ! is doing there. I would also recommend that you do some error checking to see if the file is opened.
and add a ! it seems to go in to an infinite loop
which indicates that the call is failing.
The sdFat library uses openNext() exclusively. Many other libraries use a combination of openFirst() and then openNext(). You may need to check if that applies to the library you are using. If that is the case, then you need to change the file checking logic to be initialised outside the loop, something like:
file = OpenFirst()
while (file is open)
do stuff
file = openNext()
end while
If you think about what the code is doing, it's not strange at all. The only strange thing is why you think the command "isn't being run at all". In both cases, the command will always be run at least once if execution reaches that piece of code.
Since the SDFat and SD libraries only support fat16 format, which means 8.3 names, pissing away 200 bytes to hold a 12 character (including the trailing NULL) file names seems wasteful.
plFile.write(fname, 100);
You know (or should) how long the name is. Why are you stuffing 100 bytes of mostly garbage in the file?
it was from the original code from marco that I have changed to get working with what I need, I have cleaned up the code a bit more.
and I have tried to change the way it logs to a file separately they both work but together they don't instead of writing the list of files to a file can I write it to a buffer?
Do you actually have any csv files on the SD card?
Can you read the SD card on a computer? Is it formatted? Is the write protection off?
Have you tried any of the example programs that come with the libraries. There is usually at least one that displays the files on the SD card? If you run those, do they display files?
Are you sure that there are no hardware conflicts (eg, Arduino pins for chip select)?
The odd formatting from my code seen when using the IDE comes from the fact I don't use the Arduino IDE and the editor uses real tabs. Just reformat as required.
and I have tried to change the way it logs to a file separately they both work but together they don't instead of writing the list of files to a file can I write it to a buffer?
I would suggest that you start by writing the file names to the serial console. Once you have mastered that you can write them anywhere.
if I run this code this is what I get with out creating the file so it is doing what it is meant to but just not writing it to a file.
program started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
3V_FILE.CSVStarted
Started
Started
TEST.CSVStarted
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
10000101.CSVStarted
10000219.CSVStarted
10000308.CSVcount
okay I worked it out it was the sdfile reference it was meant to be SdFileBase all working and writing to the log file the last issues is it only seems to be logging the first name and a bit
okay I have got a bit further now it is logging all the filename
it ended up to do with
lFile.write(fname, 12);
if the file is not 12 characters it will stuff it up so I got rid of the shorter name files now am a stuck to how to get it to write a new line instead of putting them all in a line any ideas?