TinyGPS Altitude question

Hello, What I am trying to do is use TinyGPS to read data from a GPS unit (garmin eTrex Legend) and two analog pins then write that information to an mircoSD card.

I have tried the example sketch that came with TinyGPS and it runs just fine, and so far my code seems to do everything I need it to do expecpt for one thing, when I run the example code I get an altitude in the form of Alt(cm):7250 or some other vaild number, with my code however I get a value of 99999999 indicating the altitude is invalid. I have been unable to locate the error in my code that is causing this, any help would be greatly appreciatied, but if you could the little C I know is self taught (for this project) so any jargon will just go right over my head, and if you can see a way of simplifying anything let me know.

#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <SdFat.h>
TinyGPS gps;
NewSoftSerial nss (2,3);

char name[]="test2.csv";
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
int temp=0;
int coo=0;
void setup(){
  pinMode(0, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(13, OUTPUT);
  card.init();
  volume.init(card);
  root.openRoot(volume);
  digitalWrite (13,HIGH);
  file.open(root, name, O_CREAT | O_APPEND | O_WRITE);
  file.print("Year,Month,Day,Hour,Minute,Second,DeciSecond,Latitude,Longitute,Altitute,Heading,Velocity,Temp,CO2 \n");
  file.close();
  digitalWrite(13,LOW);
  Serial.begin(115200);
  nss.begin(4800);
}

void loop(){
  if(feedgps()){
    memwrite();
  }
}

void memwrite(){
  long lat, lon;
  unsigned long age, date, time, chars;
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned short sentences, failed;
  gps.get_datetime(&date, &time, &age);
  Serial.print("\nDate :");
  Serial.print(date);
  Serial.print(" Time : ");
  Serial.print(time);
  feedgps();
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  digitalWrite(13, HIGH);
  file.open(root, name, O_CREAT | O_APPEND | O_WRITE);
  file.print(year);
  file.print(",");
  file.print(static_cast<int>(month));
  file.print(",");
  file.print(static_cast<int>(day));
  file.print(",");
  file.print(static_cast<int>(hour));
  file.print(",");
  file.print(static_cast<int>(minute));
  file.print(",");
  file.print(static_cast<int>(second));
  file.print(",");
  file.print(static_cast<int>(hundredths));
  file.print(",");
  digitalWrite(13, LOW);
  file.close();
  feedgps();
  Serial.print("\nYear :");
  Serial.print(year);
  Serial.print(" Month :");
  Serial.print(static_cast<int>(month));
  Serial.print(" Day :");
  Serial.print(static_cast<int>(day));
  feedgps();
  Serial.print("\nHour :");
  Serial.print(static_cast<int>(hour));
  Serial.print(" Minute :");
  Serial.print(static_cast<int>(minute));
  Serial.print(" Second :");
  Serial.print(static_cast<int>(second));
  Serial.print(".");
  Serial.print(static_cast<int>(hundredths));
  feedgps();
  gps.get_position(&lat, &lon, &age);
  digitalWrite(13, LOW);
  digitalWrite(13, HIGH);
  file.open(root, name, O_CREAT | O_APPEND | O_WRITE);
  file.print(lat);
  file.print(",");
  file.print(lon);
  file.print(",");
  file.print(gps.altitude());
  file.print(",");
  file.print(gps.course());
  file.print(",");
  file.print(gps.speed());
  file.print(",");
  file.print(analogRead(4));
  file.print(",");
  file.print(analogRead(5));
  file.print("\n");
  file.close();
  digitalWrite(13, LOW);
  feedgps();
  Serial.print("\nLat :");
  Serial.print(lat);
  Serial.print(" Long :");
  Serial.print(lon);
  Serial.print(" Alt :");
  Serial.print(gps.altitude());
  Serial.print("\nTemp :");
  Serial.print(analogRead(4));
  Serial.print(" CO2 :");
  Serial.print(analogRead(5));
  feedgps();
}
bool feedgps(){
  while (nss.available()){
    if (gps.encode(nss.read())){
      nss.flush();
      return true;
    }
    return false;
  }
}

I have tried the example sketch that came with TinyGPS and it runs just fine

There are two example sketches that come with the TinyGPS library. Which one did you run?

Is there some reason you close the file after writing the date, and then open it again to write the rest of the data?

Why are you flushing the NewSoftSerial buffer? You are losing GPS data that way.

The way I would debug this is to comment out all the code that writes to the SD card, and the Serial.print statements that print stuff other than the altitude. See if the altitude is correct. If so, start adding Serial.prints back in, and verify that the data continues to be good.

When the data you see in the serial monitor is good, add the code back in to write to the SD card. See if that is what is corrupting the GPS data.

There are two example sketches that come with the TinyGPS library. Which one did you run?

The one that uses the life GPS

Is there some reason you close the file after writing the date, and then open it again to write the rest of the data?

I was under the impression that A) I needed to feed the tinygps often and B) it was a good idea to close the file when not explicity interacting with it.

Why are you flushing the NewSoftSerial buffer? You are losing GPS data that way.

I was seeing if the problem was from failed sentences do to buffer overflow

I have tired this code with the alt still being “99999999” or “1000000.00” if I use the gps.f_altitude()

#include <NewSoftSerial.h>
#include <TinyGPS.h>
NewSoftSerial nss (2,3);
TinyGPS gps;

void setup(){
   nss.begin(4800);
   Serial.begin(115200);
}

void loop(){
   while (nss.available()){
      if (feedgps()){
         gpsprint();
      }
   }
}

void gpsprint(){
   Serial.println(gps.altitude());
}

bool feedgps(){
   while (nss.available()){
      if (gps.encode(nss.read())){
         return true;
      }
      return false;
   }
}

Capture the value returned by nss.read() in a variable, and Serial.print() that variable as well as passing it to encode.

I can't see any reason why this sketch is not returning altitude, provided that your GPS is outputting the correct sentences - it should output two or more different types. TinyGPS reads two kinds, and extracts altitude data from only one.

I'd wonder if your GPS is outputting both, except that you said it worked when you ran the example.