Buogiorno a tutti.
Sto tentando di realizzare un datalogger per monitorare dei dati di pressione per fare un grafico, ma il primo problema che ho incontrato è sulla scheda SD, in particolare riesco ad inizializzarla ma non riesco a scrivere.
Sto usando Arduino Uno rev 3 e una Ethernet shield con lettore di scheda SD, ma non so identificare il modello.
Riporto di seguito il codice usato, ho preso due programmi che ho trovato sia qui (su arduino.cc) sia in rete:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;
const int chipSelect = 13;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.println();
Serial.print("Card type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
while (1);
}
Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print("Volume size (Kb): ");
Serial.println(volumesize);
Serial.print("Volume size (Mb): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Gb): ");
Serial.println((float)volumesize / 1024.0);
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("Testing text 1, 2 ,3...");
myFile.close(); // close the file
Serial.println("Done.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening test.txt");
}
// Reading the file
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Read:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("error opening test.txt");
}
/*Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
*/
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop() {
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.println("Scrivo il file...");
// Write to file
myFile.println("Testo di prova 1, 2 ,3...");
myFile.close(); // close the file
Serial.println("Finito.");
}
// if the file didn't open, print an error:
else {
Serial.println("errore nell'apertura del file test.txt");
}
// Reading the file
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("Leggo:");
// Reading the whole file
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
}
else {
Serial.println("errore nell'apertura di test.txt");
}
delay(10000);
}
Caricando il programma, dal monitor seriale vedo:
Initializing SD card...Wiring is correct and a card is present.
Card type: SDHC
Clusters: 976512
Blocks x Cluster: 64
Total Blocks: 62496768
Volume type is: FAT32
Volume size (Kb): 31248384
Volume size (Mb): 30516
Volume size (Gb): 29.80
error opening test.txt
error opening test.txt
errore nell'apertura del file test.txt
errore nell'apertura di test.txt
Quindi la card viene vista correttamente, ma non riesco a scrivere e aprire il file. Non so come intercettare il tipo di errore per capire come risolverlo.
Ho provato a cambiare il parametro definito con
chipSelect
usando i valori, 4, 8,10,11,12,13 e 53 (so che quest ultimo è solo per arduino mega, ma provare costava poco) e solo usando 10 mi dice che la card non è presente.
Ho provato a togliere la scheda, mettere un file di testo e rimetterla su arduino e riesco a leggere la presenza del file, quindi il problema credo sia solo in scrittura.
Grazie in anticipo