When adding new
retained variables to an existing set of
it's a good idea to add them after the existing variables. this ensures the
existing retained data is still valid even with the new code.
For example, if we wanted to add a new variable
char name we should add this after
retained float lastTemperature; retained int numberOfPresses; retained int numberOfTriesRemaining = 10; retained char name;
If instead we added
name to the beginning or middle of the block of variables,
the program would end up reading the stored values of the wrong variables. This is
because the new code would be expecting to find the variables in a different memory location.
Similarly, you should avoid changing the type of your variables as this will also alter the memory size and location of data in memory.
This caveat is particularly important when updating firmware without power-cycling
the device, which uses a software reset to reboot the device. This will allow previously
retained variables to persist.
During development, a good suggestion to avoid confusion is to design your application to work
correctly when power is being applied for the first time, and all
retained variables are
initialized. If you must rearrange variables, simply power down the device (VIN and VBAT)
after changes are made to allow reinitialization of
retained variables on the next power
up of the device.
It's perfectly fine to mix regular and
retained variables, but for clarity we recommend
retained variables in their own separate block. In this way it's easier to recognize
retained variables are added to the end of the list, or when they are rearranged.