Code:
#include <SD.h>
const int TEENSY_LED=13; // teensy++ 2.0 pin = 6
const int chipSelect=10; // teensy++ 2.0 pin = 20
int iStatus = 0;
unsigned long lastTime = 0;
char* myFilename = "Cube010.txt";
void setup() {
Serial.begin(9600);
pinMode( TEENSY_LED, OUTPUT );
while( !Serial ) {
if( iStatus == 1 && millis() - lastTime > 500 )
{
digitalWrite( TEENSY_LED, HIGH );
lastTime = millis();
iStatus = 1 - iStatus;
}
else if( millis() - lastTime > 500 )
{
digitalWrite( TEENSY_LED, LOW );
lastTime = millis();
iStatus = 1 - iStatus;
}
}
pinMode(chipSelect, OUTPUT);
if( !SD.begin(chipSelect) )
{
Serial.println("SD Error!");
}
else
{
Serial.println("SD card has initialized.");
}
getUnusedFilename();
Serial.println("Done booting!");
}
void loop()
{
}
void getUnusedFilename()
{
int x = 0;
int y = 0;
int z = 0;
//Find the last file on the sd card
while( SD.exists(myFilename) )
{
for (word i = 0; i < 1000; i++)
{ // create new filename w/ 3 digits 000-999
x = i/100;
myFilename[4] = x + '0'; // calculates hundreds position
y = (i-(x*100))/10;
myFilename[5] = y + '0'; // calculates tens position
z = i - (x*100) - (y*10);
myFilename[6] = z + '0'; // subtracts hundreds & tens for single digit
if (! SD.exists(myFilename)) { // only open a new file if it doesn't exist
Serial.print("Next file: ");
Serial.println(myFilename);
break; // leave the loop after finding new filename
}
}
}
Serial.println(myFilename);
}