Search within the forum instead. This was discussed here within the last few weeks.
I think PaulS, Awol, or Nick Gammon had the solution.
Read the data sheet too - power on reset, hardware reset, software reset, not all do quite the same thing; but may end up doing what you need.
noobtoarduino:
But, the thread is a couple years old, and I don't have enough technical knowledge to understand whether doing it this way is safe to use. Any advice?
What you posted re-enters the program at the point where a hardware reset would. However it doesn't reset the hardware.
I think there was a recent post about using the Watchdog timer to do a (hardware) reset. Try searching for that.
I don't know if it's a software problem per se. I would just like the ability to reset the Arduino every once in a while, because it seems that after several days of 24/7 operation the program freezes or gets stuck.
My program gets weather & stock market data from the internet via a Wifi shield, and changes the color of an RGB LED setup accordingly. It works great for days at a time, but sometimes it would just get "stuck" by not changing colors as it should.
I would like to mess around and see if resetting the Arduino programmatically, while not solving the underlying problem of the freezes, gets rid of the freeze ups to a degree that I can live with.
Freezing like that is probably a sign of memory leaks or buffer overflows, and so, a periodic reset may well cure it, so follow some of the links on the Watchdog timer idea. That is for this kind of situation.
I always try and allocate all memory statically and stay away from things like the String class and be very careful with my array indices when writing into buffers. But it could well be something buried deep in a library somewhere, in which case it would take a lot of patience to winkle it out.
in which case it would take a lot of patience to winkle it out.
On the other hand, if you were to find (and fix) a problem in a library, that would benefit everyone that uses that library, and you would learn a lot about debugging in the process.
Thats my instinct too, but why do microcontrollers generally have the facility of a watchdog timer to reset them?
I have an Arduino heating thermostat that has been running for months without any problems.
Is it solely to cope with sloppy programming? Or are there other reasons the microcontroller can freeze? Voltage transients that don't kill it, but do affect the RAM or register state for instance?
Watchdog timers can be used to interrupt code that's got stuck, or to wake up the processor regularly to save battery life, or any other use you care to make. Typically watchdog timer circuit is micropower (but not very accurate).
If you're using the Async labs library for your wifi, take a look at this thread: http://arduino.cc/forum/index.php/topic,75915.0.html. There's a fix there for buffer overruns that were causing me similar lockup issues
While having to reset a system is often due to misbehaving software, it does have its uses.
Attached is a watchdog timer sketch that I wrote to demonstrate how to reset the AVR using
a software function.
I will caution you that depending on which bootloader you have, you may not be able to use
the watchdog reset functionality because the bootloader will get stuck in a continuous watchdog reset.
Also, reseting the AVR is not the same as a full hardware reset in that other components
that are attached to the the AVR will not see any sort of RESET nor will they experience a powerup
so they will not be restored to their initial power up state.
If you are using a an older Arduino board, I'd recommend updating the bootloader to the
latest optiboot bootloader as it is smaller, faster, and also supports using the watchdog reset.
Si:
Thats my instinct too, but why do microcontrollers generally have the facility of a watchdog timer to reset them?
I helped with a project a little while back where the poster wanted to take temperature (or something) readings every few minutes, but be powered by a battery. Taking a reading, and then going to power-down sleep, let it consume only a handful of microamps until the watchdog woke it up again. Although the maximum watchdog interval is 8 seconds, you can string those together to make a longer time.
Another approach is to "power up" an onboard clock, check the real time, and if not the wanted time (eg. time to feed the fish) go back to sleep.
Finally for things like a burglar alarm, where - who knows why it might fail? - you want to make sure it keeps working, you might reset it if the "dog" isn't "patted" every second or so.
For example, I suspect that satellites sent to Jupiter or wherever have such a timer, so that some coding error that goes undetected for years doesn't mean the loss of the satellite.
I have encountered one situation where Arduino stalled, watchdog didn't do anything about it, and pushing the physical reset button fixed it. I'd love to explain how exactly I stalled it in this way, but I really don't understand it at all. All I can say is that it was operating unattended for some weeks, ran a pretty complicated program, and had an ethernet shield.
The way I do resetting now is I mount a featherweight servo on top, which physically pushes the reset button. This seems to be the only method that's exactly equivalent to... physically pushing the button It's possible to have one Arduino reset itself in this way, or two Arduinos that can reset each other when one stalls out.