Can you save millis Using EEPROM

I am setting up some hardware that is designed to never turn off. But as we know sometimes you loose power or a circuit breaker trips etc. I have set up a capacitor bank to allow sufficient time to write to EEPROM before Arduino looses all power, therefore i am only writing to EEPROM when power loss is detected, which reduces significantly the amount of writes to the Arduino. However i have learned that i can only send 8 bit numbers to EEPROM and millis is much larger. Therfore my Question:

Can you use EEPROM to save millis() and then upload millis back on to continue timer at exactly the point it left off?

No. you can't "set" millis- it's a count of milliseconds from start.

1 Like

You can store a 64 bit number in eeprom by storing 8 bits at a time, using a total of 8 memory address. then when the number is used, 8 reads, and put the number back together.

if (!PassTwo)
        {
          rx_frame.FIR.B.FF = CAN_frame_std;
          rx_frame.MsgID = 1;
          rx_frame.FIR.B.DLC = 8;
          rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;
          PassTwo = true;
        } else {
          rx_frame.data.u8[4] = *item & 0xFF;;
          rx_frame.data.u8[5] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[6] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[7] = (*item >> 24) & 0xFF;
          ESP32Can.CANWriteFrame(&rx_frame); // send items over CAN buss
          PassTwo = false;
        }

Here I break down a 32 bit number into 8 bit pieces for sending.

item contains the 32 bit int.

1 Like

It doesn’t seem reasonable to save millis as you won’t know how long the power was off.

You can use put/get to save/read different data types.

Why not use an RTC like the DS3231 ?

1 Like

Space is an issue and so are my inputs and outputs, trying to save on room. Though i believe i can do some math and instead of just saving milli's I just save the time elapsed and include that in the calculation of my timer so it will automatically adjust.

If you want to keep track of how long the device has been active you can implement a software timer. Keeping track of milliseconds for life time monitoring is probably not necessary. Counting full seconds should be good enough.

Thanks i think this will help as i will be storing more than 8 bits, but in essence it seems unreasonable to read all of millis, so i'll settle with just reading the time difference and storing that.

Cheers.

@matt181 What needs to continue ?

Can you show your sketch ? and tell how you want to continue ?

When the Arduino starts, all the libraries are initialized and the hardware is initialized.
Suppose your sketch does something at certain times of the day (open the curtains, turn on lights, and so on) then the TimeLib + TimeAlarms library can be used with a RTC and in setup() you need a lot of code to check everything and take action if needed.

@Klaus_K, I did that in a project and it seems to work. After a few years I found that it did not work so I don't know how many seconds the device was on.

Please ignore what Idahowalker wrote.

Read once more what @LarryD wrote in reply #4 and click on those links to see what those functions do.

Sure you can. It’s a microprocessor, the programmer rules.

Whether it’s a good idea is an entirely different matter.

a7

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