I wonder if someone could explain this code snippet to me? I stumbled across it while searching for a way to do a soft reset of the arduino. It works very well, but I get the feeling it is something that is not done in polite company!!
void(* resetFunc) (void) = 0; // Soft reset function - Not sure how works!
void wakeUp()
{
resetFunc();
}
Call zero.
It isn't any kind of reset, but it is close.
A reset signal does stuff to the hardware and then goes via zero.
Most times it'll do almost the same things and work.
Right... I'm getting there...
So does this function create a 'pointer' to memory address 0 (reset vector) and then begin executing that code? Ie. bootloader etc?
Is this a bad thing to do?
I did not know you could jump to a memory address like that! Thats nifty!
The WakeUp() function is coded that way as I needed a way to get back to the main loop of my code and I wasn't sure how else to achieve that.
I needed a reset function as this stems from a project, receiving IR input from one remote and transmitting the appropriate IR signals to TV/XBMC etc.
The library I'm using for handling IR doesn't support receiving and sending of IR signals in the same sketch so would freeze, requiring a reset.
I should probably do the reset via a watchdog timer? I've stumbled over this, but the resetFunc seemed easier. I also tried connecting pin D2 to reset and pulling it high but that didn't work!
Thanks for your reply Nick Gammon, wasn't aware that you could jump to memory address' like that!
AWOL is right, it calls address 0. One has to hope that the stack pointer gets reset during reset processing, otherwise each time you "call" reset you will use up some of the available stack.
inboxjason:
The library I'm using for handling IR doesn't support receiving and sending of IR signals in the same sketch so would freeze, requiring a reset.
Libraries can be changed, you know. I don't like the idea of deliberately resetting a sketch because "it would freeze". You should really address why it freezes and fix that.
However the watchdog timer can be set up to generate a reset.