logfile.close(); // this is the command that think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name
ok so then did you try to clear Ch after it goes to displayGPS() ?
print the CH and sentence data to the serial monitor, and see if any more data goes goes into them. If not then it should tell you where your problem is.
if (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence = ch;
i++;
}
else
{
sentence = '\0';
i = 0;
displayGPS();
}
logfile.close(); // try to put it here
}
if (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence = ch;
i++;
}
else
{
sentence = '\0';
i = 0;
displayGPS();
}
}
else {
logfile.close(); //OR try to put it here
}
Data continues going out, the thing is that it no longer logs to the sd card, if I hit the restart button in the arduino it generates another file but it does the same thing again, it logs the first line.
I wonder if display GPS is not returning back to the loop after it writes the data, try this.
void displayGPS()
{
char field[20];
getField(field, 0);
if (strcmp(field, "$GPRMC") == 0)
{
Serial.print("Tiempo: ");
getField(field, 1);
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Lat: ");
getField(field, 3); // number
logfile.print(field);
logfile.print("\t");
Serial.print(field);
getField(field, 4); // N/S
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Long: ");
getField(field, 5); // number
logfile.print(field);
logfile.print("\t");
Serial.print(field);
getField(field, 6); // E/W
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Velocidad: ");
getField(field, 7);
logfile.println(field);
logfile.close(); // this is the command that think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name
Serial.println(field) * 1.854;
}
return; //[b] MAKE THIS CHANGE[/b]
}
File dataFile = SD.open("datalog.txt", FILE_WRITE); //[b]Right here it opens the file[/b]
// if the file is available, write to it:
if (dataFile) { //[b]Does some work[/b]
dataFile.println(dataString); //[b]It then writes the data to that file[/b]
dataFile.close(); //[b]Then right after, it closes the file, all in one shot![/b]
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
I have seen it, the thing is that this part of the sketch is in the loop, that is my problem, I can`t get to put the log command in the loop.
Do you have some idea for parsing the gps data on a string and put it on the loop?
Because if I open the file in the void gpsdisplay it generates lots of diferent files with only one line of data.
Also this is more like what you need to do, STUDY THE CODE CAREFULLY!!!
RE-EDIT:
From what I see, you dont need to have the logfile.open in your SETUP, what you can try is to make the change I said above and just comment // out the one you have in your setup and see if the data goes in properly.
OK you tried to put logfile.open() in displayGPS, and that created many files, all with one line in them.
You want just ONE file with multiple lines.
well it seems that because the logfile.close() is being done when the code HAS AVAILABLE data, it only writes one line then closes the file and does NOT reopen it. You need to take that .close line out and put it somewhere when there is NO available data. That way it will still write the data to the SD card and only close when there is no data. You may need to tell it to close with a button when you go to shut down the ARD.
I'm not sure if Arduino has a way to update the file when it is created instead of constantly creating new files each time. Look into that.
I found this for you, look at what it puts into the file.
www.ladyada.net/products/microsd/
Also one last look at your code,
void displayGPS()
{
char field[20];
logfile.println(); //CHANGE: I think you had it correct the first time but it was not being told to make a new line
getField(field, 0); //when there was new data, so it put everything back to back.
if (strcmp(field, "$GPRMC") == 0)
{
Serial.print("Tiempo: ");
getField(field, 1);
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Lat: ");
getField(field, 3); // number
logfile.print(field);
logfile.print("\t");
Serial.print(field);
getField(field, 4); // N/S
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Long: ");
getField(field, 5); // number
logfile.print(field);
logfile.print("\t");
Serial.print(field);
getField(field, 6); // E/W
logfile.print(field);
logfile.print("\t");
Serial.println(field);
Serial.print("Velocidad: ");
getField(field, 7);
logfile.println(field);
logfile.close(); // this is the command that think is doing that problem, the thing is that if I take it off it doesn`t log anything, just the file name
Serial.println(field) * 1.854;
}
return; //[b] MAKE THIS CHANGE[/b]
}
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
determines the name of a file that does not already exist, and leaves that file open. Move char filename[] out of the setup() function, to make it a global variable.
Then, after the SD.open(), before the break, close the file.
Then, copy the SD.open() statement to displayGPS(). This will open the same file, not create a new one.