Go Down

Topic: How do I reset the Arduino programmatically? (Read 3 times) previous topic - next topic

noobtoarduino

Hello,

Is it possible to reset the Arduino via software in such a way that the Arduino thinks it's being powered on for the first time?

I Googled before coming to the forums and found this:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235780325/all

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?

Code: [Select]

void(* resetFunc) (void) = 0; //declare reset function @ address 0
...
resetFunc();  //call reset



CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Nick Gammon


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.
http://www.gammon.com.au/electronics

Si

I've got to ask, but is this to solve a software problem you are having?

If its to effectively run 'setup' again, you can just call 'setup' from anywhere you like.

--
My New Arduino Book: http://www.arduinobook.com

noobtoarduino

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.

Si

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.
--
My New Arduino Book: http://www.arduinobook.com

PaulS

Quote
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.

Resetting is rarely a good idea.

Si

Quote
Resetting is rarely a good idea.


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?

I'm curious.
--
My New Arduino Book: http://www.arduinobook.com

MarkT

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).
[ I won't respond to messages, use the forum please ]

Si

I'm sure I remember reading something about engine management units that reset themselves frequently with a watchdog timer.

Any of you proper embedded guys out there know why this is done?
--
My New Arduino Book: http://www.arduinobook.com

wildbill

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

flico

Trabajando en ...

    * Control Domotico (En montaje ...)
    http://casitadomotica.blogspot.com/
 

[url=https://bitbucket.org/fmalpartida

bperrybap

#12
Dec 08, 2011, 08:05 pm Last Edit: Dec 08, 2011, 08:08 pm by bperrybap Reason: 1
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.

--- bill


Nick Gammon


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.
http://www.gammon.com.au/electronics

Go Up