There are several ways to connect a GPS device. Like PaulS said, the pins you are using for the GPS are... wrong. Read this for all the alternatives. SoftwareSerial is the worst choice, even if you had used other pins.
And you might be interested in my NeoGPS library. It's smaller, faster, more reliable and more accurate than all other GPS libraries. There are other advantages, related to the GPS quiet time and printing too much data. Even if you don't use it, there are lots of tips on the Troubleshooting page and the Installation page.
Here is a NeoGPS/AltSoftSerial version of your sketch:
#include <NMEAGPS.h>
#include <SPI.h>
#include <SD.h>
File myFile;
// The serial connection to the GPS device. PICK ONE:
// BEST: connect GPS to pins 0 & 1.
// Requires disconnecting pin 0 to upload new sketch over USB.
//#define gpsPort Serial // just an alias for Serial
// 2nd BEST: connect GPS to pins 8 & 9 (on an UNO)
#include <AltSoftSerial.h>
AltSoftSerial gpsPort;
// 3rd BEST: NeoSWSerial on any two pins (except 0 & 1)
//static const int RXPin = 10, TXPin = 11;
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort(RXPin, TXPin);
// NOT RECOMMENDED: SoftwareSerial
static const uint32_t GPSBaud = 9600;
NMEAGPS gps; // The parser
gps_fix fix; // A structure with all the parsed values
unsigned long lastUpdateTime = 0;
unsigned char fixCount = 0; // The number of fixes we have received.
void setup()
{
Serial.begin(9600);
gpsPort.begin(GPSBaud);
Serial.print( F("Initializing SD card...") ); // F macro saves RAM!
if (!SD.begin(8)) {
Serial.println( F("initialization failed!") );
return;
}
Serial.println( F("card initialized.") );
// Open the file *once*
myFile = SD.open("test12.txt", FILE_WRITE);
// No GPS data has been processed yet!
//Serial.println( fix.latitude(), 6 );
}
void loop()
{
if (gps.available( gpsPort )) { // read and parse GPS characters
// A complete GPS fix is ready, once per second.
fix = gps.read();
fixCount++;
// Have we received 5 fixes? This is also 5 seconds.
if (fixCount >= 5) {
fixCount = 0; // reset
Serial.print( F("Writing to test.txt... ") );
// NOTE: You may want to make sure the GPS really has a location:
if (fix.valid.location) {
Serial.print( fix.latitude(), 6 );
myFile.println( fix.latitude(), 6 );
} else {
Serial.print( '?' );
myFile.println( '?' );
}
// Make sure this location gets saved on the card.
// This slows things down a little, but it guarantees that
// the card will have the latest data when you power off
// the Arduino to remove the card. Some people use a
// button press to close the file and stop logging.
myFile.flush();
Serial.println( F(" done.") );
}
}
}
Your original version uses 18850 bytes of program space and 1310 bytes of RAM.
The NeoGPS version uses 18286 bytes of program space and 1187 bytes of RAM.
If you'd like to try it, NeoGPS and AltSoftSerial are available from the Arduino IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries.
Cheers,
/dev