Gps logger

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.

EDIT: ughh, disregard this post.

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 have allready tryed and nothing happens, puting it that way it only log the name of the file with nothing on it.

If all else fails, look at this:

no, nothing at all, just logs the filename with nothing on it

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]
}

same result, it logs only the first line

Ok well then Im stumped, try the link I posted and copy and paste what you need to get data from the GPS onto the SD card.

Maybe someone else can figure out what is wrong with your code. I can't figure it out.

ok, thaks a lot for your time

You need to open the file, write to it, and close the file ALL IN ONE FUNCTION. Show the code where you are doing that!

Here is the code
[code#include <SD.h>

#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(6, 5); // RX, TX (TX not used)
const int sentenceSize = 80;

char sentence[sentenceSize];

#define LOG_INTERVAL 100

const int chipSelect = 10;
File logfile;
long idd = 0;
long ids = 0;
long idm = 0;
long idh = 0;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}

void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(9600);
  
 
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card Failed or not present");
    return;
  }
  Serial.println("card initialized.");
  
  // 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!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
}

void loop()
{
  static int i = 0;
  if (gpsSerial.available())
  {
    char ch = gpsSerial.read();
    if (ch != '\n' && i < sentenceSize)
    {
      sentence[i] = ch;
      i++;
    }
    else
    {
     sentence[i] = '\0';
     i = 0;
     displayGPS();
    }
  }
  
}

void displayGPS()
{
  char field[20];
  getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
    Serial.print("Tiempo: ");
    getField(field, 1);
    Serial.println(field);
    logfile.print(field);
    logfile.print("\t");   
    
    Serial.print("Lat: ");
    getField(field, 3);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 4); // N/S
    Serial.println(field);
    logfile.print(field);
    logfile.print("\t");
    
    Serial.print("Long: ");
    getField(field, 5);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 6);  // E/W
    Serial.println(field);
    logfile.print(field);
    logfile.print("\t");
    
    Serial.print("Velocidad: ");
    getField(field, 7);
    Serial.println(field) * 1.854;
    logfile.println(field);
    logfile.close();
  }
}

void getField(char* buffer, int index)
{
  int sentencePos = 0;
  int fieldPos = 0;
  int commaCount = 0;
  while (sentencePos < sentenceSize)
  {
    if (sentence[sentencePos] == ',')
    {
      commaCount ++;
      sentencePos ++;
    }
    if (commaCount == index)
    {
      buffer[fieldPos] = sentence[sentencePos];
      fieldPos ++;
    }
    sentencePos ++;
  }
  buffer[fieldPos] = '\0';
} ]

Ohhh, I see what PaulS is saying, ok

look at this sample code again: http://arduino.cc/en/Tutorial/Datalogger

more to the point this part:

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]
}

This code:

  // 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.

Thanks to all, I have solved the problem, I just replace the function logfile.close() with logfile.flush()

I post the code if anyone likes to use it

#include <SD.h>

#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(6, 5); // RX, TX (TX not used)
const int sentenceSize = 80;

char sentence[sentenceSize];

#define LOG_INTERVAL 100

const int chipSelect = 10;
File logfile;
long idd = 0;
long ids = 0;
long idm = 0;
long idh = 0;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}

void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(9600);
  
 
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card Failed or not present");
    return;
  }
  Serial.println("card initialized.");
  
  // 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!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
}

void loop()
{
  static int i = 0;
  if (gpsSerial.available())
  {
    char ch = gpsSerial.read();
    if (ch != '\n' && i < sentenceSize)
    {
      sentence[i] = ch;
      i++;
    }
    else
    {
     sentence[i] = '\0';
     i = 0;
     char field[20];
      getField(field, 0);
  if (strcmp(field, "$GPRMC") == 0)
  {
    
    Serial.print("Tiempo: ");
    getField(field, 1);
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");   
    
    Serial.print("Lat: ");
    getField(field, 3);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 4); // N/S
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    
    Serial.print("Long: ");
    getField(field, 5);  // number
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    getField(field, 6);  // E/W
    Serial.print(field);
    logfile.print(field);
    logfile.print("\t");
    
    Serial.print("Velocidad: ");
    getField(field, 7);
    Serial.println(field) * 1.854;
    logfile.println(field);
    logfile.flush();   
  }
    }
  }
  
}

void getField(char* buffer, int index)
{
  int sentencePos = 0;
  int fieldPos = 0;
  int commaCount = 0;
  while (sentencePos < sentenceSize)
  {
    if (sentence[sentencePos] == ',')
    {
      commaCount ++;
      sentencePos ++;
    }
    if (commaCount == index)
    {
      buffer[fieldPos] = sentence[sentencePos];
      fieldPos ++;
    }
    sentencePos ++;
  }
  buffer[fieldPos] = '\0';
}

Now does anyone know how to configure the venus gps in 10hz with the gps viewer?