Problem with saving data on SD card - GPS Logger

Good morning!

I have a problem with jump to LOOP after check the SD card. Here is code

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

#define SD_CS 4
#define FILE_BASE_NAME "Trac"

TinyGPS gps;
SoftwareSerial serial(10, 11);

File myFile;

const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";

void setup(){
  Serial.begin(9600);
  serial.begin(9600);

  Serial.println("SD card initialization...");
  if(!SD.begin(SD_CS))
  {
    Serial.println("Failed! Check card");
    while(1);
  }
  Serial.println("Initialization done");
  
  while (SD.exists(fileName)){
    if (fileName[BASE_NAME_SIZE + 1] != '9') 
    {
      fileName[BASE_NAME_SIZE + 1]++;
    } 
    else if (fileName[BASE_NAME_SIZE] != '9') 
    {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } 
    else{
      Serial.println("Can't create file name");
      return;
    }
  }
  
  myFile = SD.open(fileName, FILE_WRITE);
  if (!myFile) {
    Serial.println("open failed");
    return;
  }
  Serial.print("opened: ");
  Serial.println(fileName);
}

void loop(){
  while (serial.available())
  {
    int c = serial.read();
    if (gps.encode(c))
    {
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);
      Serial.print("Latitude:");Serial.println(lat);
      Serial.print("Longitude:");Serial.println(lon);
      myFile = SD.open("fileName", FILE_WRITE);  
      myFile.print(lat);
      myFile.print(", ");
      myFile.print(lon);
      myFile.println("");
      myFile.close();
    }
  }
}

Code in function setup works half good, it create file, every file got next number. But the problem is that the program not jump to function loop. Need some help to solve it

You have proven to yourself that it is possible to make "setup" loop. Take the "return" out of the setup code.

Paul

Thanks for reply Paul.

I check step by step every loop. And the problem is here:

  while (serial.available())
  {
    int c = serial.read();
    if (gps.encode(c))
    {
      Serial.println("I'm not get here");
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);
      Serial.print("Latitude:");Serial.println(lat);
      Serial.print("Longitude:");Serial.println(lon);
      myFile = SD.open(fileName, FILE_WRITE);  
      myFile.print(lat);
      myFile.print(", ");
      myFile.print(lon);
      myFile.println("");
      myFile.close();
    }
  }

I getting to loop, it answer me all time, also the while loop allows me get inside. Problem is after if(gps.encode(c))

elemeeent:
Thanks for reply Paul.

I check step by step every loop. And the problem is here:

  while (serial.available())

{
    int c = serial.read();
    if (gps.encode(c))
    {
      Serial.println("I'm not get here");
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);
      Serial.print("Latitude:");Serial.println(lat);
      Serial.print("Longitude:");Serial.println(lon);
      myFile = SD.open(fileName, FILE_WRITE); 
      myFile.print(lat);
      myFile.print(", ");
      myFile.print(lon);
      myFile.println("");
      myFile.close();
    }
  }




I getting to loop, it answer me all time, also the while loop allows me get inside. Problem is after `if(gps.encode(c))`

What is gps.encode supposed to return? As you have it coded, it must return either true or false. As coded, it just loops doing the call to gps.encode.

Paul

It should return true, then i should be able to get a various data.

But, I'm sorry i jump into "while loop" one time, I think it was a mistake:/ I stuck here

while (serial.available())

When I writing this code I was watch "tutorial":

#define RXPIN 3
#define TXPIN 2
SoftwareSerial nss(RXPIN, TXPIN);
void loop()
{
  while (nss.available())
  {
    int c = nss.read();
    if (gps.encode(c))
    {
      // process new gps info here
    }
  }
}

Also if I use code like this:

#include <SoftwareSerial.h>
#include <TinyGPS.h>
 
TinyGPS gps;
SoftwareSerial nss(10, 11);
void setup(){
  nss.begin(9600);
  Serial.begin(9600);
}
void loop()
{
  while (nss.available())
  {
    int c = nss.read();
    if (gps.encode(c))
    {
      long lat, lon;
      unsigned long fix_age;
      gps.get_position(&lat, &lon, &fix_age);
 
      Serial.print("Lat:");Serial.println(lat);
      Serial.print("Lon:");Serial.println(lon);
 
    }
  }
}

It give me what I want, and what is important It works. I don't know what I'm doing wrong in the code with log on SD card.

Problem solved. The mistake was in configuration SoftwareSerial

SoftwareSerial nss(10, 11);

SD card use SPI and on Arduio it was pins from 10-13. Just needed to change the pins example 5 and 6, and it works.