2013-06-06 81 views
0

我試圖做一種菜單來允許用戶從SD card刪除一個文件或多個文件。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. 

出現這種情況,如果我有更多的文件爲好,但它總是七點停止,然後重複。爲什麼?

回答

0

我覺得你需要檢查每一個條目,看它是否是一個目錄的行:

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

我的猜測是,有一個與你的目錄名的方式的問題。