Writing coordinates to eeprom over 10 seconds

Hi. I have such a question, I use eeprom libraries. And I have a problem with saving coordinates downloaded from TinyGPSPlus.
I do it like this gives a pwm signal of 1750 and wants to save the coordinates. Writing to the eprom takes place, but it usually takes about 10 seconds. The question is whether such a record of coordinates can be accelerated or must it remain so?

I'm testing this on a stm32f103.

...
#include <EEPROM.h>
...

int threshold = 1750;
bool executed = false;

struct gpsData {
  float lat;
  float lon;
};

gpsData locationData;

void setup() {
  Serial.begin(9600);
  Serial2.begin(115200);
}

void loop() {
  int pwmValue = pulseIn(pwmPin, HIGH);
  while (Serial2.available() > 0) {
    gps.encode(Serial2.read());
  }

  if (pwmValue >= threshold && !executed) {
    if (gps.location.isValid()) {
      locationData.lat = gps.location.lat(); 
      locationData.lon = gps.location.lng(); 
      
      EEPROM.put(0, locationData); 
      executed = true;
    }
  }


}

Thank you in advance for your help

I think It's not that the writing to EEPROM takes 10 seconds. The reason is different - your GPS receiver only outputs correct data once every 10 seconds

You can easily test it by replacing the line

to line

Serial.println(millis());
2 Likes

Unfortunately, that's not it. I set the value fetch with nothing gps.location.lat() and gps.location.lon(). And then the same thing takes about 10 seconds to write to eeprom.
I connected an lcd display and the values on it change 10 times per second as I move the GPS lon, lat. And this write to eeprom is a massacre for me.

  if (pwmValue >= threshold && !executed) {

      locationData.lat = gps.location.lat(); 
      locationData.lon = gps.location.lng(); 
      
      EEPROM.put(0, locationData); 
      executed = true;
    }

Try something simple

Save the value of millis()
put() a value in the EEPROM
Save the value of millis() again
Print the difference between the 2 save values

DO NOT DO THIS IN loop() The EEPROM has a limited guaranteed number of write operations and you may have already exceeded that due to the operation of your current sketch

What does this produce?

#include <EEPROM.h>

struct gpsData
{
  float lat;
  float lon;
};

gpsData locationData;

void setup()
{
  Serial.begin(115200);
  
  unsigned long start = millis();

  locationData.lat = 78.9012;
  locationData.lon = 12.3456;

  EEPROM.put(0, locationData);

  Serial.print(millis() - start);
  Serial.println(" ms");

}

void loop()
{}
1 Like

performs the write only once... in the loop, !executed is responsible for that.
As for millis(), it saves immediately, identically, it saves a structure of 10 different byte variables quickly. And if it only gives float() or double() write it stretches to 10 seconds. I changed to other stm32f401, stm32f411 processors, the problem is still the same.

...and what did the code in post #5 produce?

So the problem is not in EEPROM, as you were said.
If you looking for help, please follow the advices rather than ignore it!

In my UNO setup, the sketch of post #5 reports "writing time" as 22 ms which means that writing delay for each location is about 2.75 ms (22/8) and it makes sense when the maximum writing delay for a location is 5 ms.

11ms it means that something else is delaying my save...I will look for the reason in the code. Maybe the ibusbm library delays writing, I'll test and check.
Thank you in advance for your help, at least now I know that the record is practically immediately stored in memory, even if it has 8 decimal places.

number of decimal places has nothing to do with size of float being stored, just so you know.

1 Like

The reason why writing to EEPROM takes around 10 seconds is because EEPROM write operations are relatively slow, and your current code is writing a floating-point value to EEPROM. Floating-point values typically require more bytes to store than integers or booleans, which further slows down the write operation.

To speed up the EEPROM write operation, you can consider the following approaches:

  1. Store the GPS coordinates as integers instead of floating-point values. This can significantly reduce the number of bytes required to store each coordinate, which can speed up the write operation.
  2. Instead of writing to EEPROM every time the threshold is met, you can buffer the GPS coordinates in an array or a circular buffer, and write the entire buffer to EEPROM periodically. This can reduce the number of EEPROM write operations required, which can improve performance.
  3. Use an external EEPROM chip with faster write speeds. This can provide faster write operations compared to the built-in EEPROM in the STM32F103.

In addition to the above approaches, you can also consider optimizing your code to reduce the time spent in other parts of your program. For example, you can try to reduce the amount of data transmitted over the serial port, or optimize your GPS parsing code to reduce the time spent parsing GPS data.

I hope these suggestions help you improve the performance of your code.

Nope
Are you reading the whole topic before posting your message?

A float type variable (no matter how many digits are there after the decimal point) is always encoded into 4-byte (32-bit) as per IEEE-754 standard (Fig-1).
IEEE754Float
Figure-1:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.