Storing arduino up time

Hello,

I'm trying to store the running time of an Arduino Mega 2560. I have in a loop a long var increasing every second. The idea was to write in the RTC memory (DS3232) and when I reboot the arduino the variable should be stored to continue this running time. The problem comes when the data is higher than 255 so it can't be stored and I should move to other memory position, but it does not seem the good way to store it.

if (millis() - last10sTime > 1000) {

      // increment up-time counter
      locUpTime += 1;
      RTC.writeRTC(31,locUpTime);
      last10sTime += 1000;

    }

Has someone any idea of how to face this? I'm getting a little bit mad... =(

Thank you very much.

(deleted)

spycatcher2k:
Just store the start time in the EEPROM, and compare it to the current time!

OP is trying to store on DS3232's SRAM...

Create a union:

union TimestampRTC{
  uint8_t tmArray[4];
  uint32_t tmStamp;
};

TimestampRTC rtcTime;

save your timestamp like this:

if (millis() - last10sTime > 1000) {

      // increment up-time counter
      rtcTime,tmStamp += 1;
      RTC.writeRTC(31, rtcTime.tmArray, 4);
      last10sTime += 1000;

    }

using the Jack Christenson library

Thank you for your quick response,

How should I read it?

rtcTime.tmStamp=RTC.readRTC(31); ?

For the moment when it's over 255 I have the same problem. :frowning:

The link provides all of the functions....

read it into your array:

it may be one of the best documented libraries on GitHub...

Before you get too excited, check the number of write cycles supported by the memory.
Writing every second may toast the RAM sooner than you think.
There are solutions, but you. we'd to understand the problem if it affects your project.

lastchancename:
Before you get too excited, check the number of write cycles supported by the memory.

The OP has not told us why he wants to record the UP time.

I suspect it would be sufficient to record the time once per minute, or maybe even once every 5 minutes.

...R

I thought I should have some problems about lifetime if i store in the arduino eeprom, but in RTC RAM should be stored every second with no problems, nevertheless it continues failling when it's over 255...

ok, while i haven't looked at the library you're using, i suspect it's only ever writing single bytes (unless there's a function to write larger types), so you need to break the unsigned long apart before storing and retrieving the 4-bytes of the time stamp value.

riki_val:
I thought I should have some problems about lifetime if i store in the arduino eeprom, but in RTC RAM should be stored every second with no problems, nevertheless it continues failling when it's over 255...

See reply 2

you need the library I referenced there, or you have to split up your timestamp byte by byte and use your library.

Just to clarify terminology, up time usually refers to the time since the last boot. It has on any system I've ever seen that kept track of it. I think what we're talking about here is running time, or cumulative up time, or something like that.

in some case , if you want to store the time , the craziest way is to use a capacitor why?
Let say you want to save time , but if you write to EEPROM , it will kill the EEPROM, if you write it to RAM , it will be lost,
so the hardware way is to use a cap and a diode, vcc(to adc also)->diode->cap-> avr
use 1 analog pin to measure the voltge of vcc, the cap must be big enough to power the avr when the vcc goes off
crazy idea not tested and need more contribution comment.

curlyz:
in some case , if you want to store the time , the craziest way is to use a capacitor why?
Let say you want to save time , but if you write to EEPROM , it will kill the EEPROM, if you write it to RAM , it will be lost,
so the hardware way is to use a cap and a diode, vcc(to adc also)->diode->cap-> avr
use 1 analog pin to measure the voltge of vcc, the cap must be big enough to power the avr when the vcc goes off
crazy idea not tested and need more contribution comment.

Wrong on so many levels that I can't even begin.

No need to store the time every second - you can make a UPS with a diode running into a biggish cap. Take a digital input from in front of the diode to sense when the power goes off. The cap will power the board for a few millis after that - long enough to write out the up time.

PaulMurrayCbr:
No need to store the time every second - you can make a UPS with a diode running into a biggish cap. Take a digital input from in front of the diode to sense when the power goes off. The cap will power the board for a few millis after that - long enough to write out the up time.

But the OP said, "when I reboot". Hitting the reset button will bypass that. We need more information about shutdown conditions.

aarg:
But the OP said, "when I reboot". Hitting the reset button will bypass that. We need more information about shutdown conditions.

True.

Also how it is powered. If it uses the onboard regulator, it might be easy to monitor the 9-12V that is powering it, and trigger if it falls below a threshold.

The OP has DS3232 battery backed RAM. What is difficult about just incrementing a counter in it while the system is running? This was in reply #2.

aarg:
Also how it is powered. If it uses the onboard regulator, it might be easy to monitor the 9-12V that is powering it, and trigger if it falls below a threshold.

The OP has DS3232 battery backed RAM. What is difficult about just incrementing a counter in it while the system is running? This was in reply #2.

Nothing really. EEPROM burns out if you write to it too frequently, but battery-backed RAM is a different thing. So realistically, it would be perfectly ok.

Hi guys,

First of all, thank you very much for all your help. Finally I make it work with reply #2, I did it with a
uint8_t tmArray[8]; //instead of length 4.

and then for read the same as write on RTC:

RTC.readRTC(31, rtcTime.tmArray, 8);

In fact, this is the acumulative up time so now neither reboot nor power-cycle the arduino this running time is lost.