Go Down

Topic: Writes to one record in EEPROM affecting other records in database (Read 228 times) previous topic - next topic

birdnird

I'm using the Extended Database Library (https://github.com/jwhiddon/EDB) to access the internal EEPROM on a Pro Mini.

I've simplified my code a bit to show what I'm doing. I'm reading values from a sensor (MPU6050), doing some calculations, and saving some values to the database. The structure I'm using for the database record is:

Code: [Select]
struct LogData {
  int id;
  long date;
  // int distance; // This works
  long distance;  // This causes EEPROM writes to act strangely
  int minutes;
};


For debugging purposes, I'm writing a new record every minute. I'm just appending a new record using the EDB library's appendRec function.

When the structure uses an int for the distance field shown above, it works fine. But when I change the distance field to a long, whenever I append a record, the latest value for the minutes field gets written to every single record in the EEPROM. So, for example, if I had the following values in the EEPROM after the first append:



id  date         distance   minutes
1   1538380800   320         1



When I append the 2nd record, if the value of minutes is zero, it will change the minutes value for all previous records:


id  date         distance   minutes
1   1538380800   320         0
1   1538381600    24         0


I don't understand why the code works with an int for distance, but the date is a long. If it's a problem of not handling 4-byte values, I should have problems with the date field too. The EDB code is accessing them as bytes, so it shouldn't matter what data type I use, right? And why does it affect the field following the distance field (minutes) and not the distance field itself?

The full simplified code is attached because it was too long to include inline.

Thanks!


Go Up