Hey. I'm using the following code to open files from an sd card in sequence, read the data, and send it to a dmx device. It's working fine (<20ms between each file opening) with a small (around 100 or less) number of files, but when I increase the number of files the read speed increases dramatically.
What's actually happening now, with 900 files, is the first 10 or so files take ~300 ms each to open, then the next file opens in~70ms, then each consecutive file takes slightly longer to open, until it takes ~500 ms for each file to open.
Can anyone explain why this is happening, and how to get around it?
thanks
// #define MEGA_SOFT_SPI 1 must be set in 2d3card.h to use dmx shield sd !!!!!
// DmxSimple.h must be edited to include Arduino.h, not Wiring.h !!!!
#include <SD.h>
#include <DmxSimple.h>
File myFile;
unsigned long millisPre;
unsigned long millisPost;
unsigned long m1;
unsigned long m2;
int testLoopCount;
void setup()
{
DmxSimple.usePin(2);
DmxSimple.maxChannel(3);
Serial.begin(57600);
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
unsigned char testBuff[513];
Serial.println("beginning read");
millisPre = millis();
//Serial.println(millisPre);
// testLoopCount = 1;
// for(int cycles = 0; cycles < testLoopCount; cycles++)
//
while(1)
{
static char filename[] = "fr00000#.txt";
static char filepath[] = "frames/";
filename[7] = '#';
filename[6] = '0';
filename[5] = '0';
filename[4] = '0';
for (int thous = '0'; thous <= '9'; thous++)
{
filename[4] = thous;
for (int hnds = '0'; hnds <= '9'; hnds++)
{
filename[5] = hnds;
for (int tens = '0'; tens <= '9'; tens++)
{
filename[6] = tens;
for (int ones = '0'; ones <='9'; ones++)
{
filename[7] = ones;
m1 = millis();
// open the file for reading:
myFile = SD.open(filename);
//myFile = SD.open(strcat(filepath,filename));
//myFile = SD.open(filename);
if (myFile)
{
Serial.println(filename);
// read from the file until there's nothing else in it:
int byteCount = 1;
while (myFile.available())
{
DmxSimple.write(byteCount, (int)myFile.read());
//testBuff[byteCount] = myFile.read();
//testBuffInt[byteCount] =(int)testBuff[byteCount];
byteCount++;
}
//testBuff[byteCount] = NULL;
///////////////
// close the file:
myFile.close();
}
else
{
// if the file didn't open, print an error:
//Serial.print("error opening ");
//Serial.print(filename);
//Serial.println(" test.txt");
{goto bailout;}
}
m2 = millis()-m1;
Serial.print(" ");
Serial.println(m2);
m1 = m2;
}
//filename[6]++;
}
}
}
bailout:;
}
Serial.println();
millisPost = millis();
//Serial.println(millisPost);
Serial.println((millisPost-millisPre));
}
void loop()
{
// nothing happens after setup
}