Allora, ho installato lo shield ethernet sull'arduino, ed ho inserito nell'apposita scheda una sd card da 2 gb formattata fat16.
Volevo provare ad utilizzare l'arduino per scrivere sulla sd ma non sono riuscito. Ho provato sia le librerie fat16 che le sdfatlib, ho provato ad eseguire gli esempi ma in entrambi i casi compare il seguente errore:
type any character to start
card.init failed
SD error
errorCode: 1
errorData: 0
Ho provato anche un altra sdcard ed il problema e' sempre lo stesso. Che sbaglio? Ho letto da qualche parte che potrebbe trattarsi di qualche problema di saldatura, anche se ho acquistato lo shield ethernet già assemblato.
Ho trovato risposta da solo (cercando, cercando e cercando su internet).
In pratica il tutto è dovuto dal fatto che l'sd reader condivide il bus con la parte ethernet della scheda. Per tale ragione le due funzioni non possono essere eseguite contemporaneamente.
Mi pare molto strana questa cosa, visto che lo shield viene usato per realizzare web server che leggono dati dalla scheda e li mettono in rete! Io stesso ho scritto un programma che ogni secondo salva un dato sulla card e lo invia su una pagina web.
Anche a me dava dei problemi con gli esempi trovati in giro, poi ho scoperto che c'era una riga sbagliata in molti esempi!
L'inizializzazione corretta dovrebbe essere questa, anche se stupidamente non mi sono segnato qual è la riga sbagliata:
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// create a new file
char name[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error ("file.create");
Serial.print("Logging to: ");
Serial.println(name);
// write header
file.writeError = 0;
file.print("millis");
Leggendo su questo forum, mi pare di capire comunque che c'e' un errore nella documentazione: prima c'era il pin 10 condiviso tra rete e sd-card, ora la rete usa il 10 e la card il 4... ma la libreria non lo sa!
Quindi forse la riga "incriminata" è:
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
Con il rilascio dell'ultimo IDE (0022) la libreria per l'accesso all'SD è stata integrata
Se ricordo bene è la sdfatlib con l'aggiunta di qualche classe che rende l'uso più immediato.
Come si evince dall'esempio che segue, per la lettura/scrittura su file, (incluso con la libreria nell'IDE), l'inizializzazione avviene impostando il pin 10 come output e usando il pin 4...:
/*
SD card read/write
This example shows how to read and write data to and from 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
updated 2 Dec 2010
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
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(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
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.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}