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.
出现这种情况,如果我有更多的文件为好,但它总是七点停止,然后重复。为什么?