1

I have two successfully working individual programs, 1.GPS coordinate extract program. 2.EEPROM saving and reading program. now all I want to do is combine them both, whenever GPS location is updated ,I want to save that latitude data to EEPROM memory repalcing the previously saved data, but I guess my program is unable to save it,its just showing blank spaces when reading EEPROM,please help me guys..here is my code.

#include <SoftwareSerial.h> 
#include <TinyGPS++.h>
#define RXPin 8
#define TXPin 9
#define GPSBaud 9600
#define ConsoleBaud 9600

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

// The TinyGPS++ object
TinyGPSPlus gps;
#include <EEPROM.h>
int address = 0; 
int read_value = 0; 
char data;
void setup()
{
  Serial.begin(ConsoleBaud);
  ss.begin(GPSBaud);
  Serial.println("Previous value stored :");
  for(address = 0; address < 25; address ++)        // read the entire EEPROM memory
  {  
    read_value = EEPROM.read(address);
    Serial.write(read_value);
  }
  Serial.println("\n");

}

void loop()
{

  while (ss.available() > 0)
    gps.encode(ss.read());

  if (gps.location.isUpdated() || gps.altitude.isUpdated())
  {
    Serial.print("Location: "); 
    Serial.print(gps.location.lat(), 6);
    Serial.print(",");
    Serial.print(gps.location.lng(), 6);
    Serial.println("\n");

    data=(gps.location.lat(), 6);

    for(address = 0; address < 25;  )
    {
      EEPROM.write(address, data); 
      address ++;
    }
  }
}
  • data=(gps.location.lat(), 6); just always stores 6 in data, this is what will be sotored on all 25 first locations of EEPROM. It looks you don't understand what you're doing with this code. I'd recommend taking a look at some basic C programming tutorial. – jfpoilpret Oct 5 '14 at 12:27
2

Consider TinyGPS++ over TinyGPS only if you are running on Due or processor which can take advantage of the higher precision of 64-bit “double” floating-point.

char data; should be double data;. You're wasting resources otherwise.

data=(gps.location.lat(), 6); isn't how you do it. You only need to specify precision when writing to Serial, like Serial.print(gps.location.lat(), 6);. Make it data = gps.location.lat();.

address ++; should be address = address <=512-sizeof(address) ? (address + sizeof(data)): 0 ;

address <=512-sizeof(address) ensures that the last value written to the EEPROM is written in it's entirety.

Alternatively, I would recommend this to save you data structure directly to EEPROM, and what @jfpoilpret mentions.

1

You might try using sprintf. First define a buffer:

char data[25];

Then use sprintf to print to the buffer:

sprintf(data, "Location: %.3f,%.3f\n", gps.location.lat(), gps.location.lon());

%.3f prints a float with 3 decimal places. Check out the printf reference for details.

Then write the buffer to EEPROM:

for(address = 0; address < 25; address++ )
{
  EEPROM.write(address, data);
}
  • Careful with lat() and lon(): those return a double not an int! Hence sprintf("...%d...") will not work unless you explicitly cast lat() and lon() return to int. – jfpoilpret Oct 5 '14 at 18:03
  • well spotted @jfpoilpret :P – geometrikal Oct 6 '14 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.