SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By jolijar
#106169
I am working on a Reverse Geocache box (already built one that works now I am modifying it)
When the player first presses the button I want it to store the time and start counting down (they will get 3 min for every mile they are from the target)

I am not sure how to code this. I think if I can get a code sample that gets the time from the gps and counts down to a fixed time I can figure out the rest of it. I have searched around but found nothing like this. Can anyone point me in the right direction?
By stevech
#106611
do you have the GPS serial data working, where you can input the NMEA sentences at 4800 baud?
This is step 1.
Once this works, the rest is easy.
By jolijar
#117868
yes I do.
Here is the code I have so far
It displays the date and time in the serial menu.
Now what I want is to have a date set like this
int Day = 21;
int Month = 1;
int Year = 2011;
int Hour = 14;
int Min = 29;
int Sec = 59;

and I want it to take the current date from the gps and figure out how far away it is in this format
Days:Hour:Min:Sec

I am completely clueless as to how to accomplish this.
Any ideas?

*note the code is modified from Mikal Hart's Tiny Gps which is what my reverse geocache box uses.
Code: Select all
#include <NewSoftSerial.h>
#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of NewSoftSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
*/

TinyGPS gps;
NewSoftSerial nss(3, 2);

void gpsdump(TinyGPS &gps);
bool feedgps();
void printFloat(double f, int digits = 2);
int Gpspwr   = 7;                                        // Gps power connected to pin 7

void setup()
{
 pinMode(Gpspwr, OUTPUT);                                 // Set a digital pin for the GPS Power  
 digitalWrite(Gpspwr, HIGH);                              // Turn the Gps on 
  Serial.begin(115200);
  nss.begin(4800);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();

  // Every 5 seconds we print an update
  while (millis() - start < 5000)
  {
    if (feedgps())
      newdata = true;
  }
  
  if (newdata)
  {
    Serial.println("Acquired Data");
    Serial.println("-------------");
    gpsdump(gps);
    Serial.println("-------------");
    Serial.println();
  }
}

void printFloat(double number, int digits)
{
  // Handle negative numbers
  if (number < 0.0)
  {
     Serial.print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  Serial.print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    Serial.print("."); 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    Serial.print(toPrint);
    remainder -= toPrint; 
  } 
}

void gpsdump(TinyGPS &gps)
{
  long lat, lon;
  float flat, flon;
  unsigned long age, date, time, chars;
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned short sentences, failed;

int Mo;
int Dy;
int Yr;
int Hr;
int Min;
int Sec;

  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  Mo = (static_cast<int>(month)); 
  Dy = (static_cast<int>(day));
  Yr = (year);
  Hr = (static_cast<int>(hour)); 
  Min = (static_cast<int>(minute)); 
  Sec = (static_cast<int>(second)); 
Serial.print(Mo);Serial.print("/");Serial.print(Dy);Serial.print("/");Serial.println(Yr);   
Serial.print(Hr-6); Serial.print(":"); Serial.print(Min);Serial.print(":"); Serial.println(Sec);   
  
}
  
bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}