GPS and eeprom problem.

Hi guys, i'm new to the forum and beginner with arduino as well. My question is why arduino is writing only the first two digits from latitude, the first two before the dot. For example latitude 48.356789. Is working on Serial.print(flat, 10) but with eeprom.write(addr, flat) won't work, it's writing only 48 to eeprom. I'm trying to save the location to eeprom and then send it as sms with FONA from Adafruit. For now this is the only problem i'm struggling with. If its possible can i combine "flat" and "flong" in one address on eeprom?
Thanks in advance and sorry for my bad english.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <EEPROM.h>

TinyGPS gps;
SoftwareSerial ss(3, 4);

int addr = 0;

void setup()
{
  Serial.begin(115200);
  ss.begin(4800);
  
  Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop() {
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    Serial.println(flat, 10);
    Serial.println(flon, 10);
    //lat = flat;

    EEPROM.write(addr, flat);
        
    addr = addr + 1;
    if (addr == 512)
    addr = 0;
    
    delay(3000);
    
  }
}

Is working on Serial.print(flat, 10) but with eeprom.write(addr, flat) won't work, it's writing only 48 to eeprom.

If flat is a float, then you are only writing the first of 4 bytes to the EEPROM.

You need to use a union and store the flat value in the float portion and then write all 4 bytes on the byte array portion to the EEPROM.

512/4 = 128, so you'll only get about 2 minutes worth of data. Why are you storing data in EEPROM, anyway?

EEPROM.write only writes one byte to the address but it is not writing the first of the 4 bytes. The floating point number 48.356789 will be converted to an unsigned character, which will result in 48, and that will be written to the address.

Pete

PaulS:

Is working on Serial.print(flat, 10) but with eeprom.write(addr, flat) won't work, it's writing only 48 to eeprom.

If flat is a float, then you are only writing the first of 4 bytes to the EEPROM.

You need to use a union and store the flat value in the float portion and then write all 4 bytes on the byte array portion to the EEPROM.

512/4 = 128, so you'll only get about 2 minutes worth of data. Why are you storing data in EEPROM, anyway?

Thanks for your response. What i'm trying to do is a "SOS BOX" ( press a button, log gps coordinates to eeprom, small delay, read that address from eeprom and use the "fona.sendSMS" command to send the location, or maybe convert the lat and long in a google maps link and then send it). I can allready use a button to send a sms, but again at fona command i can only use "fona.sendSMS("number", "message"); if i'm trying " fona.sendSMS("number", latitude, 16); won't work. Maybe there is another way to do it, for example "message = latitude + longitude" or something but i have no ideea. Hope you guys understand what i mean. Thanks for your time.

I figured out another method to add the two coordinates together using string, with my limited knowledge doesn't work either =(

here is the part of code

void getgps(TinyGPS &gps) {
  
  float latitude, longitude;
  //int latitude, longitude;
  
  gps.f_get_position(&latitude, &longitude);
  
  
  stringOne = String(latitude);
  stringTwo = String(longitude);
  stringThree = StringOne + StringTwo;
  Serial.print(stringthree);
  
}

end the error i get:

sketch_sep20a.ino: In function 'void getgps(TinyGPS&)':
sketch_sep20a:37: error:[b] call of overloaded 'String(float&)' is ambiguous[/b]
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:70: note: candidates are: String::String(long unsigned int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:69: note:                 String::String(long int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:68: note:                 String::String(unsigned int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:67: note:                 String::String(int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:66: note:                 String::String(unsigned char, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:65: note:                 String::String(char)
sketch_sep20a:38: error: call of overloaded 'String(float&)' is ambiguous
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:70: note: candidates are: String::String(long unsigned int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:69: note:                 String::String(long int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:68: note:                 String::String(unsigned int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:67: note:                 String::String(int, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:66: note:                 String::String(unsigned char, unsigned char)
C:\Program Files (x86)\arduino-1.0.4-windows\arduino-1.0.4\hardware\arduino\cores\arduino/WString.h:65: note:                 String::String(char)
sketch_sep20a:39: error: 'StringOne' was not declared in this scope
sketch_sep20a:39: error: 'StringTwo' was not declared in this scope
sketch_sep20a:40: error: 'stringthree' was not declared in this scope

Can enyone please help me?

The String class is useless. Forget it.

You could use dtostrf() to convert the float to a string. (Note that a string is not the same thing as a String).

You can concatenate strings using strcpy() and strcat().