hi all,
I find myself pressing the reset-button on my arduino Mega when I'm heading off on a wrong limb. The proper working of my current sketch depend on values that are written to memory prior to full stop/reset. Does the reset()-button spawn an event that I can hook up to, to have my values written .. or shall I face the fact that I shall learn to end my sketch in a proper manner (pressing my own button) and insert the writing code in this event?
When you press the reset button, all RAM is cleared (correction from westf below, it is cleared because the compiler adds startup code before "main()", not because of hardware), all registers are default values, and your code jumps to the bootloader.
You can try placing code in the bootloader, but remember that all memory is reset.
You can get rid of the bootloader, but the memory is still reset.
You can store some stuff into EEPROM, that will not be reset.
The best way is to just add another button yourself.
Reset does NOT clear RAM. The sketch startup code will clear ram, however.
westfw:
Reset does NOT clear RAM. The sketch startup code will clear ram, however.
If this is the case, then it is possible to implement a reset handler, but you're going to have to compile it into the very beginning of the bootloader, or stop using the bootloader.
It will be very difficult and most likely not worth the time in comparison to just implementing another button.
The bootloader does not clear RAM, either, it doesn't even use any RAM (beyond a few locations of stack) unless you upload something. So all you really need to do is modify your sketch, although that's probably a bit tricky to do from within the Arduino environment. (I think you could have some code execute BEFORE the startup code but putting it in one of the magic ".initN" sections. But the big difficulty would be in doing something that doesn't get immediately undone by the startup code.)