Bonjour à toutes et à tous,
J’ai déjà posté ce sujet sur le site en anglais. Si vous pensez qu’il fait ici doublon, supprimez-le.
Après avoir passé pas mal d’heures sur mon application, je pense que le problème vient de la bibliothèque SD. C’est-à dire :
Si je veux faire en première action un listing puis après des lectures/écritures, tout se passe bien. Mais si mes premières actions sont des lectures/ écritures et qu’ensuite, je souhaite faire un listing, celui-ci se passe mal : il manque des fichiers.
Pour vous montrer ce problème, j’ai modifié l’exemple “listfiles” dans lequel j’ai ajouté l’écriture d’un fichier entre deux listing. Le premier listing se passe bien, dans le second, il manque des fichiers.
Pour le test, j’utilise une carte Mega2560 avec un écran tactile 320 x 240 qui supporte le lecteur de carte SD. J’utilise une carte SD de 2 Mb sur laquelle j’ai placé quelques fichiers dans la racine.
Avez-vous déjà rencontré ce problème ? Pouvez-vous essayer le sketch ci-après et m’indiquer les résultats que vous obtenez.
NOTE : faire attention à la bonne valeur à placer dans : “SD.begin(xx)” ( xx = 53 dans mon cas)
Merci de votre aide
Pierre
/*
SD card basic file example
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
File myFile;
File root;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
Serial.println();
ecritFichier();
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
}
void loop()
{
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
}
else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void ecritFichier() {
Serial.println();
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
Serial.println("testing 1, 2, 3.");
// close the file:
myFile.close();
}
Serial.println("done!");
Serial.println();
}