0
사용자가 SD card에서 파일 하나 또는 여러 파일을 삭제할 수있는 메뉴를 만들려고합니다.Arduino SD 카드 선택 삭제
모든 파일의 전체 목록을 쉽게 얻을 수 있지만 실행과 사용자에게 각 파일을 많이 삭제할 것인지 묻는다면 필자는 처음 7 개 파일 만 얻은 다음 7 번째 파일을 반복합니다. 최대 파일 수에 도달 할 때까지 여기
내 코드입니다 :#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.
나뿐만 아니라 더 많은 파일이있는 경우이 문제가 발생하지만, 항상 7시에 정지하고 그런 다음 반복합니다. 왜?
if (entry.isDirectory())
{
Serial.println("Test")
}
내 생각 엔 당신이 디렉토리 이름을 얻는 방법에 문제가 있다는 것입니다 : 당신이 디렉토리인지 알아보기 위해 각 항목을 검사하는 라인이 필요한 것처럼