Problems with writing data on SD card - GPS logger

Hello everyone!

I have some troubles with my GPS logger project. At the begin here is what i use:
-Arduino Uno
-GPS NEO-6M uBlox
-SD card module

Code what i use:

#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>

File logger;

#define RX_PIN 10
#define TX_PIN 9
#define SD_CS 4

TinyGPS GPS;
SoftwareSerial mySerial(TX_PIN, RX_PIN);

char filename[16];
  
void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  Serial.println("SD card checking..");
  if (!SD.begin(SD_CS)){
    Serial.println("Failure");
    return;
  }

  Serial.println("SD card ready");
 
  // make it long enough to hold your longest file name, plus a null terminator
  int n = 0;
  snprintf(filename, sizeof(filename), "data%03d.txt", n); // includes a three-digit sequence number in the file name
  while(SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }
  logger = SD.open(filename,FILE_READ);
  Serial.println(n);
  Serial.println(filename);
  logger.close();
  //now filename[] contains the name of a file that doesn't exist 
}

void loop() {

  while (mySerial.available()){
    int var = mySerial.read(); 
    
    logger = SD.open(filename,FILE_WRITE);

    if (GPS.encode(var))
    {
      long lat, lon;
      unsigned long fix_age;

      GPS.get_position(&lat, &lon, &fix_age);

      Serial.print("Szerokosc:");
      Serial.println(lat);
      Serial.print("Dlugosc:");
      Serial.println(lon);

      logger.print(lat);
      logger.print(":");
      logger.print(lon);
      logger.println();

    }
      logger.close();
  }
}

The method of file creation I found on forum, and I was hoping it will works, but no...

The problem is, the GPS getting position as I want, file is creating as it should. When I insert the SD card port monitor is getting freeze at:

SD card checking -> SD card ready -> show file name and there is freeze, nothing happen

but when I take the SD card out, automatically port monitor show position...

And now the question what I do wrong? I know in code is some messy, but is only a sketch i will make it better when it start work.

Not an expert by any means, but I'm also debugging other aspects of using SD cards and can share some thoughts.

First thing's first of course: have you confirmed the SD card even works (do the read/write examples work)?

Specifically regarding the pulling the SD card out makes data spit out: that's happening because (I don't know which specifically) some of the SD library APIs are blocking calls. Having the card in makes those calls block infinitely because it's failing, therefore stopping the rest of your code (the serial prints...etc) from executing.

Hi, thanks for replay.

SD card works for 100%, code like this:

#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>

File logger;

#define RX_PIN 10
#define TX_PIN 9
#define SD_CS 4

TinyGPS GPS;
SoftwareSerial mySerial(TX_PIN, RX_PIN);

void setup() {
    mySerial.begin(9600);
  Serial.begin(9600);
  Serial.println("SD card checking..");
  if (!SD.begin(SD_CS)){
    Serial.println("Failure");
    return;
  }
  Serial.println("SD card ready");


}

void loop() {
  while (mySerial.available()){
    int var = mySerial.read();
    
    logger = SD.open("tracker.csv", FILE_WRITE);
       
    if (GPS.encode(var))
    {
      long lat, lon;
      unsigned long fix_age;

      GPS.get_position(&lat, &lon, &fix_age);
 
      Serial.print("Szerokosc:");
      Serial.println(lat);
      Serial.print("Dlugosc:");
      Serial.println(lon);
      logger.print(lat);
      logger.print(":");
      logger.print(lon);
      logger.println();
    }
    logger.close();
  }
}

saving the data on the SD card. But I want to add this loop to "generate" next file, not to replece the last by creating new one with the same name.

  while(SD.exists(filename)) {
    n++;
    snprintf(filename, sizeof(filename), "data%03d.txt", n);
  }

It's likely getting stuck here. The exists function has a nice forever loop that should be fun to debug. Just gotta peel the layers of the onion and figure out what's going on.