SD-карта Arduino выбрать удалить

Я пытаюсь создать своего рода меню, позволяющее пользователю удалить файл или несколько файлов с SD-карты. .

Я могу получить полный список всех файлов достаточно легко, но когда дело доходит до прогона и запроса пользователя, следует ли удалить каждый файл, я каким-то образом получаю только первые семь файлов, а затем седьмой повторяется до максимального числа файла достигнуты.

Вот мой код:

#include <SD.h>

File datafile;

void setup()
{
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    Serial.print("Initializing SD card...");
    pinMode(10, OUTPUT);

    if (!SD.begin(10)) {
        Serial.println("initialization failed!");
        return;
    }
    Serial.println("initialization done.");
    Serial.println("done!");
}

void loop()
{
    while (true)
    {
        char c;  // A choice var this will be changed later to a button so for now this method works
        datafile = SD.open("/");
        printDirectory(datafile);  // Function to print the full list of files in one go
        Serial.println("Do you want to delete a file? (y = 1 / n = 2)");

        while(!Serial.available()) // Wait until the user inputs something.
        {}
        c = Serial.read();
        if (c=='2')
            hold();                // If no, go to hold function ( basically end program )
        else
            if (c=='1')            // If yes, go to the delete function
            {
                datafile = SD.open("/");  // Give the data link to the SD card
                deletnum(datafile);
            }
            else
                Serial.println("Choice not correct try again");  // If choice is wrong, I'm not too concerned about this now.
    }
}

void deletnum(File dir)
{
    Serial.println("Scrolling numbers now");  // Scroll though the numbers and pick what to delete

    int c=0;
    File entry;
    int x=1;
    while(true)
    {
        entry =  dir.openNextFile();
        // if (! entry)
        // { // no more files
        //   break;}
        Serial.print("This one ? 1=yes , 2= no : ");
        Serial.println(entry.name());
        while(!Serial.available())
        {;}

        c = Serial.read();
        if(c=='1')                             // If they picked yes then delete it and go to the next file and ask.
        {
            SD.remove(entry.name());
            Serial.println(" file deleted")
        }
        else
            if (c=='2')
                Serial.println("not that one then");
    }
}

void printDirectory(File dir)   // Function to print the full list of files in one go
{
    int x=1;
    while(true)
    {
        File entry =  dir.openNextFile();
        if (! entry)
        { // No more files
            break;
        }
        Serial.print(x);
        Serial.print(") ");
        Serial.println(entry.name());
        x++;
    }
}

void hold() // A function just to "hold" or stop the program if he user does not want to delete any more files.
{
    while(true)
    {
      Serial.println("Holding");  // here just to show me that it is in the loop
    }
}

Но после всего этого я получаю этот вывод:

Initializing SD card...initialization done.

done!

1) TEMPDATA.TXT

2) DTAD.TXT

3) 84.TX

4) 104.TX

5) TEMPDA00.TXT

6) TEMPDA02.TXT

7) TEMPDA03.TXT

8) TEMPDA04.TXT

9) TEMPDA05.TXT

Do you want to delete a file ? (y = 1 / n = 2)

Scrolling numbers now

This one ? 1=yes , 2= no : TEMPDATA.TXT

not that one then

This one ? 1=yes , 2= no : DTAD.TXT

not that one then

This one ? 1=yes , 2= no : 84.TX

not that one then

This one ? 1=yes , 2= no : 104.TX

not that one then

This one ? 1=yes , 2= no : TEMPDA00.TXT

not that one then

This one ? 1=yes , 2= no : TEMPDA02.TXT

not that one then

This one ? 1=yes , 2= no : TEMPDA03.TXT

not that one then

This one ? 1=yes , 2= no : TEMPDA04.TXT

not that one then

This one ? 1=yes , 2= no : TEMPDA04.TXT      // Here lies the problem. This should be TEMPDA05.TXT.

not that one then

This one ? 1=yes , 2= no :     // There are not more files so there are no more names.

Это происходит, если у меня есть еще файлы, но это всегда останавливается на семи, а затем повторяется. Почему?


person Spider999    schedule 06.06.2013    source источник
comment
вам не нужно закрывать файл перед его удалением?   -  person pezet    schedule 06.07.2018
comment
Привет, извините за эту очень долгую задержку с ответом, в итоге я не закрыл файл, я просто не видел его по какой-то причине, довольно сильно пнул себя за это, когда у меня все заработало, ха-ха.   -  person Spider999    schedule 02.09.2018


Ответы (1)


Я чувствую, что вам нужна строка, которая проверяет каждую запись, чтобы увидеть, является ли она каталогом:

if (entry.isDirectory())
{
    Serial.println("Test")
}

Я предполагаю, что есть проблема с тем, как вы получаете имена каталогов.

person John b    schedule 06.06.2013