I started using Arduino and Arduino IDE approximately a week ago. For one of my larger programs I am writing, I want to make separate a code which can make a logData sheet on a Micro SD card attached via the Micro SD module. This separate code makes the .txt file and gives all the columns a title. For training purposes I am trying to use more of the SD library functionalities.
One of these functions is the SD.remove function. I added it to my code and the first time I ran my code it deleted my file. On consecutive runs it does not anymore. If I look at the Example>SD>File code it looks exactly the same as in there. The example code works, mine does not.
Now what is happening is that I delete the file, check if it exists, which it does and then the column titles are added again. I tried multiple things and now I have around 30 times my column titles in the file
I am using a Arduino Uno Rev.3 and also attached my .ino file to this post. If anything is missing please tell me and I will add it to the post. If anything is clear, I'll explain in another way.
Looking forward to the replies.
Sincerely,
Loekatoni
EDIT 1:
The code did not attach as I expected. I would like to add as seen in different posts, but I do not know how yet. For now I'll just copy and past into this area..
EDIT 2: I learned the proper way to post code..
// Global declarations
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // begin serial communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Setting up Mirco SD module
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("Initialization done.");
Serial.println("Removing logData.txt");
SD.remove("logData.txt");
if (SD.exists("logData.txt")) {
Serial.println("logData.txt exists.");
} else {
Serial.println("logData.txt doesn't exist.");
}
myFile = SD.open("logData.txt", FILE_WRITE);
if (myFile) { // if the file opened oke
Serial.println("Writing column titles to logData.csv.");
myFile.println("Time[-] Temperature[C] Humidity[%]");
Serial.println("Done writing column titles to logData.csv.");
} else { // If filed didn't open properly
Serial.println("Error opening logData.txt.");
}
myFile.close();
myFile = SD.open("logData.txt");
if (myFile) {
Serial.println("logData.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}
myFile.close();
}
void loop() {
// Nothing needs to happen to initialize the logData.txt file.
} // end of loop
loekatoni:
One of these functions is the SD.remove function. I added it to my code and the first time I ran my code it deleted my file. On consecutive runs it does not anymore. If I look at the Example>SD>File code it looks exactly the same as in there. The example code works, mine does not.
Now what is happening is that I delete the file, check if it exists, which it does and then the column titles are added again. I tried multiple things and now I have around 30 times my column titles in the file
/*
myFile = SD.open("logData.txt", FILE_WRITE);
if (myFile) { // if the file opened oke
Serial.println("Writing column titles to logData.csv.");
myFile.println("Time[-] Temperature[C] Humidity[%]");
Serial.println("Done writing column titles to logData.csv.");
} else { // If filed didn't open properly
Serial.println("Error opening logData.txt.");
}
myFile.close();
myFile = SD.open("logData.txt");
if (myFile) {
Serial.println("logData.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}
myFile.close();*/
}
void loop() {
// Nothing needs to happen to initialize the logData.txt file.
} // end of loop
You seem to have commented out all of the code that would create the file and put headers in it. I don't know how you would end up with 30 copies of the headers.
What is your Serial Monitor output saying?
In the mean time I have been trying to write the time, temperature and humidity to the file via my main code. For the time I do not have my DS1307 RTC module yet, so for now this is depicted by an x. This has been succesfull, so FYI that you know where those values come from.
I have also deactivated some of the code I posted before. Namely the parts that give titles to the columns. Below is the code with the deactivated lines:
// Global declarations
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // begin serial communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Setting up Mirco SD module
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("Initialization done.");
Serial.println("Removing logData.txt");
SD.remove("logData.txt");
if (SD.exists("logData.txt")) {
Serial.println("logData.txt exists.");
} else {
Serial.println("logData.txt doesn't exist.");
}
/*
myFile = SD.open("logData.txt", FILE_WRITE);
if (myFile) { // if the file opened oke
Serial.println("Writing column titles to logData.csv.");
myFile.println("Time[-] Temperature[C] Humidity[%]");
Serial.println("Done writing column titles to logData.csv.");
} else { // If filed didn't open properly
Serial.println("Error opening logData.txt.");
}
myFile.close();
*/
myFile = SD.open("logData.txt");
if (myFile) {
Serial.println("logData.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}
myFile.close();
}
void loop() {
// Nothing needs to happen to initialize the logData.txt file.
} // end of loop
And consecutively, below is what my Serial monitor output shows:
Initializing SD card...
Initialization done.
Removing logData.txt
logData.txt exists.
logData.txt:
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 22 40
x 22 39
x 22 39
x 22 39
x 22 39
x 22 39
Please read the sticky post at the top of the forum and learn how to properly post your code and serial monitor output. It will help others help you. They are called code tags.
The SD.remove() function returns a true/false value. Why are you not checking it? (and printing it?)
I can't see any reason for that to happen if the code you show is the code that produces the serial output. Are you sure your new versions of the sketch are uploading without error?
I uploaded the wrong Serial monitor output. I updatet the text in the respective post. The code you mention produces:
logData.txt exists, so it is functioning properly and it tells me the file is not deleted.
The function I found to return the true/false statement is:
return SD.remove("logData.txt");
Which I introduce in the complete code as follows.
// Global declarations
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // begin serial communication
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Setting up Mirco SD module
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("Initialization done.");
Serial.println("Removing logData.txt");
SD.remove("logData.txt");
return SD.remove("logData.txt");
if (SD.exists("logData.txt")) {
Serial.println("logData.txt exists.");
} else {
Serial.println("logData.txt doesn't exist.");
}
/*
myFile = SD.open("logData.txt", FILE_WRITE);
if (myFile) { // if the file opened oke
Serial.println("Writing column titles to logData.csv.");
myFile.println("Time[-] Temperature[C] Humidity[%]");
Serial.println("Done writing column titles to logData.csv.");
} else { // If filed didn't open properly
Serial.println("Error opening logData.txt.");
}
myFile.close();
*/
myFile = SD.open("logData.txt");
if (myFile) {
Serial.println("logData.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
}
myFile.close();
}
void loop() {
// Nothing needs to happen to initialize the logData.txt file.
} // end of loop
This does not seem like the function I need. The code "gets stuck" there. It does not continue to consecutive lines. I also don't understand why I want to get this specific statement, as I have put an exist function in the code, which tells me if the file exists or doesn't.
Have you looked at the sd card on a computer? Possibly you have some odd situation where you incorrectly did something previously that has corrupted the file system.
I think this could be the problem indeed. I tried it with a different file name and this seemed to work fine. When I can attach it to a PC, I'll check out if the file is corrupted and maybe delete it from the windows explorer screen.
Took me some time. The initial file was corrupted. Maybe I did not close it was and tried to do some things in the wrong order. I do not know. I deleted all the files and formatted my SD and am now able to use the code on the initial file name again.