Loop not working properly.

This is the first tme using Arduino and I don't have any experience with coding but
I put 2 seperate codes together to try and make it work. So i got the code so far that my gps works together with my SD card.
however my code doesn't loop properly so i dont have constant updates for my gps.

/* TEST
 */

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

File myFile;
int pinCS = 10; // Pin 10 on Arduino Uno

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup() {
    
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);
  ss.begin(GPSBaud);
  
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  
  // Create/Open file 
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("Writing to file...");
    // Write to file
    myFile.println("Testing text 1, 2 ,3...");
    myFile.close(); // close the file
    Serial.println("Done.");
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
  // Reading the file
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (myFile.available()) {
      Serial.write(myFile.read());
   }
    myFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  }
  
}
void loop() {

  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("Writing to file...");
    // Write to file
    myFile.println("Testing text 1, 2 ,3...");
    
    while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
    Serial.println("Writing to file...");
    // Write to fileif (gps.location.isUpdated()){
      myFile.println("Latitude= "); 
      myFile.println(gps.location.lat(), 6);
      myFile.println(" Longitude= "); 
      myFile.println(gps.location.lng(), 6);
    
    myFile.close(); // close the file
    Serial.println("Done.");
    }
   }
  }
}

You are opening and closing that file as fast as possible, probably 100's or 1000's of times a second.

You don't want to do that I'm sure!

Your logic should be the other way round:

read input from the GPS until you have a whole string (using loop and "if (ss.available() > 0)").

When you have a whole string, call a function to write it to the file.

open the file in setup(), flush it after each write. Why are you closing it at all?

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

gps.encode() returns a value. It is usually a bad idea to ignore that value.

MarkT:
You are opening and closing that file as fast as possible, probably 100's or 1000's of times a second.

You don't want to do that I'm sure!

Your logic should be the other way round:

read input from the GPS until you have a whole string (using loop and "if (ss.available() > 0)").

When you have a whole string, call a function to write it to the file.

open the file in setup(), flush it after each write. Why are you closing it at all?

I'm sorry but i don't know how i do that. i am just getting started

There is no real harm in opening the file when you have data to write to it, writing that data, and then closing the file.

You want to organize loop() so that it checks for GPS data first. When encode() says that the stored data represents valid data, THEN you open the file, write to it, and close it.