Launch function before resetting

Hello,

I have a sketch that performs some tasks and evaluates whether it succeeds or fails and keeps tracking of the evolution of the task (how many succeeds and how many failures).

It is al kept in volatile memory.

I've successfully programed the arduino to detect power failure and at that point store all variables into the EEPROM memory to be able to set up all variables to previous status in the next boot (power comes back). This works pretty well and I can resume the process after power failures.

The issue comes when instead of a power drop, the system resets. I would like to be able to call the function to store data into the EEPROM BEFORE the arduino resets.

Is there any way of setting a function to run BEFORE reseeting the arduino board?

What is causing the reset ?

There are two options:

  1. I don't know (maybe som short fut or malfunction) it's unexpected reset.

  2. reset button press

Is there any difference?

I don't think it matters if there is any difference but if I were you I would be looking to prevent the reset occuring in the first place

that could be a possible solution.

I think I set up a capacitor in the reset pin to prevent it to reset when UART has any malfunction or stablishes new connection.

But somehow I've managed to reset the board pressing reset button.

@thememore

Which Arduino are you using?

Are you familiar with watchdog timer interrupt and system rese

Once it resets it's all gone. If the reset is not planned, how will the Arduino know that it's coming ?

Best practice if dealing with an MCU that has a true EEPROM is to store everything into it the moment that it's available. Keep in mind that a true EEPROM also has a life cycle but it is significantly longer than a flash-EEPROM (100.000 writes vs 1.000 writes)

The reset button grounds the RST pin. The DTR signal can be blocked by adding a capacitor, but this will not prevent things like power loss or pressing the reset pin. (you could remove the reset pin, but still power loss can happen)

I'm using either: Arduino nano, nano every or micro.

I've read and tried watchdog in other projects. In this one I think I don't use it.

But watchdog would be used to force a reset if any malfunction by software. amb I right?

The reset system, I'm not familiar with it. That's somehow my question, can a function be called when reset process is launched?

yes indeed I don't store data in EEPROM unless power failure because the data is generated every 3-4 seconds and the process should run for 5-6 days at least. That would be a lot of EEPROM writes and life time of EEPROM would be quite short.

Maybe you should store your critical variables in FRAM (e.g. MB85RC256V 32 kByte)
FRAM is non-volatile and allows millions of writes.
You can use it in a write through fashion, so if a variable changes, you update the FRAM too.
Only in setup() you will read the variables to restart.

By using a high clock rate for I2C the FRAM is quite performant.

Don't know your application but you could use a part of the FRAM e.g. for logging, or display strings, etc.

In Arduino NANO, the watchdog timer, by default is at "Stopped Mode".

However, if you have programmed it for "Interrupt Mode and then System Reset Mode", then (after the expiry of timeout period), the MCU executes ISR(WDT_vect){} and then re-starts from RESET/ (the boot location). In the ISR() routine, you can execute your diagnostic or house-keeping code before the MCU re-starts.

seems an interesting solution, not for my question here, but for tha practical root problem, which is storing data and be able to recover it on next boot to resume.

Thanks for the tip, I'll explore it for nexst projects.

I'm not sure if I understood you well. What you mention would allow me to reset after a certain condition. But not to detect an unexpected reset and prepare for the reset before it happens. Did I understood you well?

@thememore
My post #12 is valid if you have used watchdog timer in your application; otherwise, it is just for general information.

Then whatever you programmed in that case should also work for brownouts (power droop) as the board will be reset in both cases.

no, because for power breakdowns I use a pin interruption with a capacitor and I compare voltage in two pins. When there is a difference I launch an interruption with the function to store data in EEPROM.

But in an unexpected reset, keeping power working, I cannot detect the drop and thus not launch the EEPROM saving function.

I've checked and I'm not using watchdog timers in this project.

Then the simplest solution is what @robtillaart suggested. There is no way you can know in advance when a reset is about to occur.

@thememore
Your board is normally working and suddenly it restarts -- correct?

Note that RESET and START/RESTART are not the same things. When you press the RESET button and hold it down, the board is in RESET state meaning it is not running. The borad starts running when you release the RESET button.

Likewise brown-out detection logic keeps the board is at RESET state if the BOD is enabled and the detection level goes below set level. The board automatically restarts when the detection level goes above set level.

Also, you are not using watchdog timer

In your case, you have no idea why is the board resetting?

Do you have any external circuits that are connected with your Arduino board? If yes, show us the connection diagram.

The board also restarts when click is made on the serial monitor icon of the IDE? It is also restarts at the end of sketch Uploading process.