I'm working on a device that is used to log frequencies from a metal detector. The purpose of my code is to detect when a USB connection has been established and transfer all files on the card to a PC. I am not able to test the code currently, so would like a bit of feedback on whether this will function as intended.
I am communicating with a python script through the serial to determine connection. The code should determine the number of files on the card, store the file names in an array and write each file to serial.
The code to check for USB connection:
//check for usb connection
if (Serial.available() > 0) {
File root;
root = SD.open("/");
printDirectory(root, 0);
while (true);
}
printDerectory function:
void printDirectory(File dir, int numFiles)
{
numFiles = 0;
while (true)
{
while (!Serial);
{} //wait for serial to open
if (!SD.begin(chipSelect))
{
serial.write("2"); //error message to pyserial
while (true);
}
File entry = dir.openNextFile();
if (!entry)
{
dir.rewindDirectory();
break;
}
else
{
numFiles++;
}
entry.close();
}
fileList = new String[numFiles];
for (int i = 0; i < numFiles; i++)
{
File entry = dir.openNextFile();
fileList[i] = entry.name();
entry.close();
File dataFile = SD.open(fileList[i]); //need testing to see if this works
while (dataFile.available())
{
Serial.write(dataFile.read()); //^^^
}
dataFile.close();
}
}