Saving, then reading from eeprom after power cycle

Hi,

I am almost there creating a datalogger with 32u4 and a Nextion 4832T035_11.

I am having real trouble reading unixtime from eeprom, following a power cycle and variables are cleared.

Saving unixtime to eeprom, address 30 (caldateadd).

void caldatestring() {//-----------------------Compile date and time "now" into a single string
  caldate = "";
  DateTime now = rtc.now();                           //grab date & time from the RTC
  long caldatetime1 = (now.unixtime());
  String caldatetimehex = String(caldatetime1);
  caldate += (caldatetimehex);
  EEPROM.put(caldateadd, caldate);                    //write calibration date to eeprom
}

Retrieve caldate from eeprom, caldatesize set to 10.

void read_caldate_from_eeprom(char *bufferIn) {
  for (int i = 30; i < caldate_size; i++) {
    bufferIn[i] = EEPROM.read(i);
    caldate = bufferIn;
  }
}

If you can offer any assistance, it would be appreciated.
Thank you.

You use EEPROM.put() to save the data so why not use EEPROM.get() to read it back ?

Hi,
I have that working with two other saved numbers, namely zero and gain readings of a sensor (adc), but when it comes to the 10-digit unixtime, eeprom.get just doesn't return a logical number after a power cycle.

Hello

Your loop is incorrect

for (int i = 30; i < caldate_size; i++)

if as you said, caldate_size is 10, then the loop will not run because i is always greater than 10

Show a complete example

Why are you using Strings ?

You seem to have unixtime in a long so why not just put() it into the EEPROM and get() it back later ?

for (int i = 30; i < caldate_size; i++)

Another observation, if caldate_size is set to 10 as you say, then how many times will this for loop iterate ?

I'm using strings as Nextion writes need them.

I see the problem with 10 being less than 30.

How do I reconstruct the long after the eeprom.get?

You do not need to reconstruct the long after the get(). The value will already be in a long

#include <EEPROM.h>

unsigned long savedValue = 1234567891;
unsigned long loadedValue;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  EEPROM.put(0, savedValue);
  EEPROM.get(0, loadedValue);
  Serial.println(savedValue);
  Serial.println(loadedValue);
}

void loop()
{
}
1 Like

Changing from long to unsigned long brought this 2 day expedition trying to find the answer to a close.

Thanks very much UKHeliBob.

You are welcome

Despite what some people say, size does matter :grinning:

My overly large and bloated code could use some optimization and while I usually relish a challenge, time is against me.
Does anyone know of any decent paid resources (sites) for experienced coders to trim fat using their boxes of tricks? I'm at 97% of prog space with a few more calculations and Nextion communications to do. Pretty sure a lot of it will be ditching strings in favor of chars.

IF you don't have time to do it yourself, how will you ever have time to do all the testing after someone makes changes?
Paul

Time spent learning how to code could be spent by me testing the code by a.n.other?

Jugglers need to juggle more than one ball.

@negativ3 You could always ask in the Gigs and Collaborations section of this forum and this topic could be moved there

I never even considered this site had such a thing UKHeliBob. Will get my binoculars on.
No need to move, will begin anew.

How will you spend your time in the weeks between tests?
Paul

Repairing electro/hydraulic monsters and reading Roark's formulas for stress and strain.

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