Hi!
I need to store a single line of data in csv file stored in sd-card module,but the data was written two times on two line at every upload the code on arduino.
what is the problem?Can you help me? thank you.
I uploaded a screen shot for the result on the serial monitor(1.png).
This is the code:
#include<SD.h>
#include<SPI.h>
File myfile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing card...");
// declare default CS pin as OUTPUT
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization of the SD card failed!");
return;
}
Serial.println("initialization of the SDcard is done.");
myfile = SD.open("textFile.txt", FILE_WRITE);
if (myfile)
{
Serial.print("Writing to the text file...");
myfile.println("Congratulations! You have successfully wrote on the text file.");
myfile.close(); // close the file:
Serial.println("done closing.");
} else
{
// if the file didn't open, report an error:
Serial.println("error opening the text file!");
}
// re-open the text file for reading:
myfile = SD.open("textFile.txt");
if (myfile)
{
Serial.println("textFile.txt:");
// read all the text written on the file
while (myfile.available())
{
Serial.write(myfile.read());
}
// close the file:
myfile.close();
} else
{
// if the file didn't open, report an error:
Serial.println("error opening the text file!");
}
}
void loop()
{
}
wildbill:
I can't see why, unless your sketch writes and then gets restarted somehow.
What does your serial output say?
After every uploading the sketch two lines of data were written in the serial monitor as in the file
for example:
Congratulations! You have successfully wrote on the text file.
Congratulations! You have successfully wrote on the text file.
I still can't see the issue. I'd try dropping the section of code that reads the file and check the content on a PC. I suspect that something non obvious is wrong - library, hardware, wiring? Do you have another Arduino with an SD card. Might be worth trying a different SD card too.
wildbill:
I still can't see the issue. I'd try dropping the section of code that reads the file and check the content on a PC. I suspect that something non obvious is wrong - library, hardware, wiring? Do you have another Arduino with an SD card. Might be worth trying a different SD card too.