Hello community, im a beginner with arduino and currently implementing my first project where im trying do delete files from sd card incase it becomes full.
when i try to use an openNext arduino example everything is cool and working, however if i try to implement a remove file capeability with " sd.remove", i get unexpected output/results. i have been stuck with this bug for 5 days please your help will be appreceiated.
#include <SPI.h>
#include "SdFat.h"
// SD default chip select pin.
const uint8_t chipSelect =4;
// file system object
SdFat sd;
SdFile root;
SdFile file;
void setup() {
Serial.begin(9600);
// Wait for USB Serial
while (!Serial) {
SysCall::yield();
}
Serial.println("Type any character to start");
while (!Serial.available()) {
SysCall::yield();
}
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
if (!root.open("/")) {
sd.errorHalt("open root failed");
}
// Open next file in root.
// Warning, openNext starts at the current directory position
// so a rewind of the directory may be required.
while (file.openNext(&root, O_RDONLY)) {
char filename;
file.getName(&filename,20);
if (file.isDir()) {
// Indicate a directory.
Serial.print(&filename);
Serial.println(" :directory detected");
}else{
Serial.println(&filename);
if(sd.remove(&filename)){
Serial.println(" :removed");
}else{
Serial.println(" failed to remove");
}
}
Serial.println();
file.close();
}
if (root.getError()) {
Serial.println("openNext failed");
} else {
Serial.println("Done!");
}
}
void loop() {}
Here is the output in serial monitor if you enter charactors four times,
Type any character to start
Type any character to start
Type any character to start
Type any character to start
Yes please, if i do remove the if statement with sd.remove, everything is fine if i just serial print the &filename, the issue/bug comes when i add the if statement with " sd.remove()".
the card is filled with files, im assured of that please.
Thanks for your reply please,
What im trying to archieve is,to loop through each file on sd card and store file name in filename variable every iteration.
That means filename variable has to hold only one file name on each iteration as i delete the file.
No, everything is NOT fine. You are overrunning your variable. You can not stuff a filename into a 1 char length variable. You are just getting LUCKY that the program is not crashing.
No, it is NOT fine. Please stop saying that. You are just getting LUCKY since you are using memory you do not own. I have pointed out the issue, why do you not want to correct it?
while (file.openNext(&root, O_RDONLY)) {
char filename[20];
file.getName(filename,20);
if (file.isDir()) {
// Indicate a directory.
Serial.print(filename);
Serial.println(" :directory detected");
}else{
Serial.println(filename);
}
Serial.println();
file.close();
}
if (root.getError()) {
Serial.println("openNext failed");
} else {
Serial.println("Done!");
}