Hello!
I am currently having problems with a Nextion display and SD module. I am trying to print on a text box the present files in the SD card. However, i have been trying and I can't manage to make it work. Here is the code:
void printDirectory(File dir, int numTabs) {
root = SD.open("/");
Serial2.print(F("sdFeedback2.txt=\""));
while (true) {
File entry = dir.openNextFile();
if (! entry) {
if (numTabs == 0) {
Serial2.print("**Done**");
Serial2.print(F("\""));
Serial2.print("\xFF\xFF\xFF");
return;
}
}
Serial2.print(entry.name());
if (entry.isDirectory()) {
Serial2.print("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial2.print(entry.size());
Serial2.print(F("\r\n"));
}
entry.close();
}
root.close();
}
to call the function I place it in the code as : printDirectory(root, 0); and all I get on the textbox is "Done". Any idea to why this is happening?
There are 2 parts to your question: Dealing with SD cards, about which I know nothing, and printing to a Nextion, which I can help with.
Split your problem in 2 parts, start with getting text on your Nextion, when that works deal with the SD card and what you want to print, and slot that into your now working methods for printing on a Nextion. See Using Nextion displays with Arduino for help printing to a Nextion text box.
Do you really want to get stuck in a while() loop?
You send the start of printing to a text box with:
Serial2.print(F("sdFeedback2.txt=\""));
Then the rest is conditional on other things, which might or might not be true. What do you think happens if after the sdFeeback2.txt etc bit is sent the next part is not sent? You are now in your while() loop with no opportunity to ever send sdFeeback2.txt etc again.
Look at my examples, the complete message to the Nextion is all in one block, not split into bits of code that might or might not do things.
As to whether you ever get to the return in the while() loop I have no idea because getting there is too convoluted. To get to the return numTabs has to equal 0, but I cannot see anything in your while() loop that would change it. I'm not sure anything every changes entry either.