Need Code help to log gps data to sd

I have a pharos-gps500 sirf iii gps module (from microsoft streets) I have gotten it set up and working. It locks on to sats and I can output the data to the serial monitor. I just got a seeedstudio sd shield the one that has both sd and microsd slots. I have been able to make a sketch that will read from and write to etc… the sd card. But I am at a total loss as to how to log the gps data to the sd.

I used the tinygps example test with gps.

and the code i used for sd was the sd example for data logger.

Im using arduino 1.01 and my board is a duemilanove.

Any help or advice would be greatly appreciated. I have been looking online for some reference or code I could borrow but have had no luck as of yet.

Couldn’t you just take the raw GPS strings and print them out over SPI?

I dont know? Im very new to arduino and have no programming experience. I have’nt used spi before.

I usually don’t use arduino (actually I haven’t, but many concepts are very similar to the other uCs I use), but it should be a case that whenever a byte is read from the UART, then you simply copy the contents of the RX register and transfer it raw over to the SPI transfer buffer. There should be some spi.write(BYTE) or something similar.

This is the code that got me going in logging GPS to an SD card.

http://www.ladyada.net/make/gpsshield/GPSLogger_v3.pde

There is some extra stuff in there too in regards to turning off and on the GPS, changing the GPS settings etc, but the basic idea of the logging is:

Is there data waiting on the serial port?
   if so, read data character by character, saving it in a long string

      is this the end of the serial data?
         if so, terminate the string and write the string to the SD card.

         if not the end of the data, keep listening

   if there is no data waiting, do nothing.

I am having a problem with errors when compiling my code now. Im using arduino 0018 i have these libs installed

:NewSoftSerial,Streaming,Tinygps,sdfat

here is my code:

/*


Written by Jonnyboy323

Version 1.0

GPS Data Logger

10/17/11


*/

//Add the Mikal Hart Libraries

#include <NewSoftSerial.h>

#include <TinyGPS.h>

//Add the SdFat Libraries

#include <SdFat.h>

#include <SdFatUtil.h>

#include <ctype.h>

//Create the variables to be used by SdFat Library

Sd2Card card;

SdVolume volume;

SdFile root;

SdFile file;

#define BUFFSIZE 65

char buffer[BUFFSIZE]; //This will be a data buffer for writing contents to the file.

char in_char=0;

char filename=“trip.txt”;

//Intialize TinyGPS and NewSoftSerial

TinyGPS gps;

NewSoftSerial nss(3, 2);

//Intialize GPS variables

long lat, lon;

float flat, flon, fspeed, falt, fcourse;

unsigned long age, date, time, chars;

int year;

byte month, day, hour, minute, second, hundredths;

unsigned short sentences, failed;

bool newdata = false;

bool feedgps();

void setup()

{

nss.begin(4800); //GPS’s Buad Rate

pinMode(0, INPUT);

pinMode(1, OUTPUT);

pinMode(10, OUTPUT);

pinMode(13, OUTPUT); //LED Pin

digitalWrite(13, LOW); // Ensure LED starts as off

card.init(); //Initialize the SD card and configure the I/O pins.

volume.init(card); //Initialize a volume on the SD card.

root.openRoot(volume); //Open the root directory in the volume.

//Create file with defined filename

file.open(root, filename, O_CREAT | O_APPEND | O_WRITE); //Open or create the file ‘name’ in ‘root’ for writing to the end of the file

file.print(“year,month,day,hour,minute,seconds,latitude,longitude,altitude(ft),speed(mph),course\n”); //Write the header array to the end of the file.

file.close(); //Close the file.

// flash LED 5 times when setup is finished

for (int i=0; i<4; i++){

digitalWrite(13, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(13, LOW); // set the LED off

}

}

void loop()

{

bool newdata = false; // check if data is coming in

if (feedgps())

newdata = true;

if (newdata)

{

//Pull gps data

gps.f_get_position(&flat, &flon, &age);

feedgps(); //used to keep gps “awake”

gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);

feedgps();

falt = gps.f_altitude();

fspeed = gps.f_speed_mph();

fcourse = gps.f_course();

feedgps();

//Break up float values into whole numbers(var) and decimals(var1) to be added to data

int flat1 = (flat - (int)flat) * 10000;

int flon1 = (flon - (int)flon) * 100000;

int alt1 = 0;

if (falt >= 10000000.00){ //when gps cant get altitude

falt = 0; //set to 0

}else{

falt = falt * 0.032808399; //cm to feet

alt1 = (falt - (int)falt) * 10;

}

int fspeed1 = (fspeed - (int)fspeed) * 10;

int fcourse1 = (fcourse - (int)fcourse) * 10;

//Create then write the char array “buffer” to the end of the file

file.open(root, filename, O_WRITE | O_APPEND); //Open the file in write mode and append the data to the end of the file.

sprintf(buffer, “%d,%d,%d,%d,%d,%d,%0d.%d,%0d.%d,%0d.%d,%0d.%d,%0d.%d\n”, year,month,day,hour,minute,second,(int)flat,abs(flat1),(int)flon,abs(flon1),(int)falt,abs(alt1),(int)fspeed,abs(fspeed1),(int)fcourse,abs(fcourse1));

file.print(buffer); //Write the ‘contents’ array to the end of the file.

file.close(); //Close the file

//flash LED

digitalWrite(13, HIGH); // set the LED on

delay(500);

digitalWrite(13, LOW); // set the LED off

delay(10000); // wait 10 sec before writing next data point

}

}

bool feedgps()

{

while (nss.available())

{

if (gps.encode(nss.read()))

return true;

}

return false;

}

I get these errors when compiling:

arduino-0018\libraries\NewSoftSerial/NewSoftSerial.h:4:20: error: Stream.h: No such file or directory

and

arduino-0018\libraries\NewSoftSerial/NewSoftSerial.h:53: error: expected class-name before ‘{’ token

any idea whats causing these erroe

ok so i figured out that some libraries were not where they should be got that sorted out, then it compiled and uploaded.The program runs, writes data to the card. Now the problem is that while its writing the correct latitude in relation to my location, the longitude its reporting is off the actual location by 11 miles. However if I load and run the tinygps example sketch it then reports bot lat and lon correctly so I figure the problem must be in my code but Im stumped as to what it is. Any ideas?

heres the code Im running:

/*


Written by Jonnyboy323

Version 1.0

GPS Data Logger

10/17/11


*/

//Add the Mikal Hart Libraries

#include <SoftwareSerial.h>

#include <TinyGPS.h>

//Add the SdFat Libraries

#include <SdFat.h>

#include <SdFatUtil.h>

#include <ctype.h>

//Create the variables to be used by SdFat Library

Sd2Card card;

SdVolume volume;

SdFile root;

SdFile file;

#define BUFFSIZE 65

char buffer[BUFFSIZE]; //This will be a data buffer for writing contents to the file.

char in_char=0;

char filename=“trip.txt”;

//Intialize TinyGPS and SoftwareSerial

TinyGPS gps;

SoftwareSerial nss(3, 2);

//Intialize GPS variables

long lat, lon;

float flat, flon, fspeed, falt, fcourse;

unsigned long age, date, time, chars;

int year;

byte month, day, hour, minute, second, hundredths;

unsigned short sentences, failed;

bool newdata = false;

bool feedgps();

void setup()

{

nss.begin(4800); //GPS’s Buad Rate

pinMode(0, INPUT);

pinMode(1, OUTPUT);

pinMode(10, OUTPUT);

pinMode(13, OUTPUT); //LED Pin

digitalWrite(13, LOW); // Ensure LED starts as off

card.init(); //Initialize the SD card and configure the I/O pins.

volume.init(card); //Initialize a volume on the SD card.

root.openRoot(volume); //Open the root directory in the volume.

//Create file with defined filename

file.open(root, filename, O_CREAT | O_APPEND | O_WRITE); //Open or create the file ‘name’ in ‘root’ for writing to the end of the file

file.print(“year,month,day,hour,minute,seconds,latitude,longitude,altitude(ft),speed(mph),course\n”); //Write the header array to the end of the file.

file.close(); //Close the file.

// flash LED 5 times when setup is finished

for (int i=0; i<4; i++){

digitalWrite(13, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(13, LOW); // set the LED off

}

}

void loop()

{

bool newdata = false; // check if data is coming in

if (feedgps())

newdata = true;

if (newdata)

{

//Pull gps data

gps.f_get_position(&flat, &flon, &age);

feedgps(); //used to keep gps “awake”

gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);

feedgps();

falt = gps.f_altitude();

fspeed = gps.f_speed_mph();

fcourse = gps.f_course();

feedgps();

//Break up float values into whole numbers(var) and decimals(var1) to be added to data

int flat1 = (flat - (int)flat) * 10000;

int flon1 = (flon - (int)flon) * 100000;

int alt1 = 0;

if (falt >= 10000000.00){ //when gps cant get altitude

falt = 0; //set to 0

}else{

falt = falt * 0.032808399; //cm to feet

alt1 = (falt - (int)falt) * 10;

}

int fspeed1 = (fspeed - (int)fspeed) * 10;

int fcourse1 = (fcourse - (int)fcourse) * 10;

//Create then write the char array “buffer” to the end of the file

file.open(root, filename, O_WRITE | O_APPEND); //Open the file in write mode and append the data to the end of the file.

sprintf(buffer, “%d,%d,%d,%d,%d,%d,%0d.%d,%0d.%d,%0d.%d,%0d.%d,%0d.%d\n”, year,month,day,hour,minute,second,(int)flat,abs(flat1),(int)flon,abs(flon1),(int)falt,abs(alt1),(int)fspeed,abs(fspeed1),(int)fcourse,abs(fcourse1));

file.print(buffer); //Write the ‘contents’ array to the end of the file.

file.close(); //Close the file

//flash LED

digitalWrite(13, HIGH); // set the LED on

delay(500);

digitalWrite(13, LOW); // set the LED off

delay(5000); // wait 5 sec before writing next data point

}

}

bool feedgps()

{

while (nss.available())

{

if (gps.encode(nss.read()))

return true;

}

return false;

}

Drop the arduino and use [Openlog + GPS. No need to code anything. Data is logged to sd card when the power is on. 8)

See my reply to [this topic for an application of this.](https://forum.sparkfun.com/viewtopic.php?f=14&t=32695&p=145714&hilit=cat+collar#p145547)](https://www.sparkfun.com/products/9530)