Continued problems with DFPlayer readFileCountsInFolder method

FOUND IT! Took the better part of a morning but finally figured it out. Jump to the end if you just want to see the fix.

When I looked more closely at the reads that didn’t align after the first call that returned the -1, I noticed that they were off by one folder. So, clearly the data was getting back, but it was out of sync. I messed around putting a bunch of delays in, playing with the setTimeOut function, making sure the serial port was flushed, but nothing made any difference. Next, I dug into the DFPlayer code and found there is a debug flag (nice!). With debug enabled I could see that the read routines were indeed reporting back before the read info had been provided from the DFPlayer, but I also saw that there was some sort of acknowledgement from the DFPlayer for every DFPlayer library call - even the write calls. The read calls generate two responses, one with the requested data and then another for the ack. What is happening is that every call to the DFPlayer generates an ack that is interpreted by a subsequent read call as the requested data. This continues with every call because there is always one unread ack call hanging around. Clearing the serial buffer after a call with available() does work, but ONLY if you also put in a small delay to give time for the real response, since the library interprets the ack as the response and returns immediately.

Digging into the code more, I saw there is an ack flag setting, and that this flag can be set or disabled in the DFPlayer.begin call (it’s one of the optional parameters). In the example files this flag is explicitly set to true, and if it’s not supplied it also defaults to true. BUT when explicitly set it to false in my DFPlayer.begin call, then the acks went away and everything worked. Sooooo, it looks like the library is set up to enable or disable the ack feature, but that it does not appropriately deal with acks when they are enabled.

I tested this on both my official DFRobot player as well as some clones and it was consistent - setting the ack=true in the DFPlayer.begin call worked and not setting it caused any reads to fail.

Thanks much to all who replied to my original post - you gave me some debugging ideas that helped put me on the path.