Go Down

Topic: arduino + SD card Question (Read 2 times) previous topic - next topic

a760109

Hi everyone,

I use Arduino uno + SD shield and use external power, save data in sd card
but sometime I read SD card from PC , data write to SD card stop
how can I solve the question ?    Thanks.
my code.
Code: [Select]

void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");

myFile = SD.open("test.txt", FILE_WRITE);

if (myFile) {
Serial.print("Writing to test.txt...");
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}

myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

myFile.close();
} else {
Serial.println("error opening test.txt");
}
}


void loop()
{
myFile = SD.open("test.txt", FILE_WRITE);
......
myFile.print(temp, DEC);
......
myFile.close();
delay(1000);
}

SurferTim

I don't know if this is your problem, but my rule for files is if you open a file, you should close it.
Code: [Select]
myFile = SD.open("test.txt", FILE_WRITE);

if (myFile) {
Serial.print("Writing to test.txt...");
Serial.println("done.");

                // close the file here, just like below.
myFile.close();

} else {
Serial.println("error opening test.txt");
}

        // with your sketch, myFile is still open from above
       
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

myFile.close();
} else {
Serial.println("error opening test.txt");
}

a760109

hi
my problem ,example..
i save data to SD card every second from Arduino
if it operation 30 min ,i should get 600 data
but i see SD card , maybe write 10min stopped
so i lose after 10 minutes of data

thank.

SurferTim

#3
Jun 27, 2012, 02:24 pm Last Edit: Jun 27, 2012, 02:27 pm by SurferTim Reason: 1
You should post your code. Your code above does not compile. No headers, no global variables, and my compiler does not like the "......" deal in the loop function.

Here are some of my other rules:
If the user thinks it isn't important enough to include, it probably is.
If the user says "don't worry about that", you probably should.
If the user says "it is taken care of", it probably isn't.


a760109

hi SurferTim

Code: [Select]

#include <SD.h>

File myFile;
int t;

void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");

myFile = SD.open("test.txt", FILE_WRITE);

if (myFile) {
Serial.print("Writing to test.txt...");
Serial.println("done.");
                myFile.close();
} else {
Serial.println("error opening test.txt");
}

myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}

void loop()
{
int analogValue0;
t++;
myFile = SD.open("test.txt", FILE_WRITE);   
analogValue0 = analogRead(0);
myFile.print(t);
myFile.print("\t");
myFile.println(analogValue0);
myFile.close();
delay(1000);
}

Go Up