Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



How to reset your A...
 
Share:
Notifications
Clear all

[Solved] How to reset your Arduino in code ...

4 Posts
2 Users
0 Reactions
1,990 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

While working on a project, I needed the ability to do a reset of my Arduino, and found this code - often referred to the WatchDog Timer.

It didn't fix my problem, but since I have looked for this before (all for the wrong reasons), I thought it to be a good idea to post it here;
#include <avr/io.h> 
#include <avr/wdt.h>
... void setup() 
{ ... // do something }
void loop() 

... // do something     if (condition) 
    { 
      wdt_enable(WDTO_1S); // resets your Arduino
    } 
  } ... // do something
}
Side notes:
- this is a hard reset so any variables will be blanked and their values lost.
- consider finding a better way to work around this, and reconsider the design of your sketch

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

an alternative is this call, which seems to work as well - but you'll loose your variables as well:

asm volatile ("  jmp 0");

   
ReplyQuote
(@papygeek)
New Member
Joined: 7 years ago
Posts: 1
 

Hi Hans,

First of all, I want to thanks you for all the shared informations on your website,

I am please to use your code done "AllEffects LEDStrip".

Please I apologize for my rosty english.

I have look your cleaver way to use reset by jumping to reset vector and saving data to EEPROM.

On my side I  try another way, without the EEPROM, it is more a kind of software reset.

1/ I save the stack pointer after setup

2/ I save the return adress of where in the code I  want to be back

3/ I use interrupt for 2 push button one for inc the other for decrement  a variable volatile byte selectedEffect;

4/ at the end of interrupt I get back the stack pointer saved at 1/ and get code back to  adress saved in 2/

That the idea and as you will see in the code hereunder that's works.

But when I link that code with Fasteled AllEffects LEDStrip or NeoPixel AllEffects LEDStrip I get strange behavior that the stack pointer and return adress whas undefined.

If I comment the use of ledstrip then my undefined variables are back, i feel that should be a problem of linker xor compiler pragma but I am not so familiar with such matter, may be you will see and suggest other way, if you have time to have look on it, if you need more comments of my code please to not hesitate to ask for.

If you intend to run this code please notice that I use Arduino ATMega2560 and in the IRQ handler I push and pop three byte for return adress, where standart Arduino need only two!

Of course on hardware side if your design have only one push button that would work, you will just go on the related IRQ.

There is no matter on usage, your code run very well on my hardware, as you I just remain curious of whats going on and want to share my experience.

If you find usage to share this code please do it, I have no blog or website.

Thanks again for all of your sharing and stay curious.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Hi Papygeek!

Thanks for the compliment and welcome at Tweaking4All! 

Your approach sounds interesting and advanced (for most users) - thanks for sharing. I'd love to find an alternative method, always open for improvements.
It sounds like NeoPixel and FastLED trashes your stack pointer though. Both libraries, especially FastLED, are quite advanced when it comes to using the Arduino hardware.


   
ReplyQuote
Share: