Go Down

Topic: Running Arduino Micro 24/7 (Read 2 times) previous topic - next topic


Please excuse my questions but i'm new to the Arduino world and this is my first project.

For the Watchdog timer I found some example code and added it in my loop as below - so far it seems to be working so thanks for the idea!

#include <avr/wdt.h>
wdt_enable(WDTO_8S); // have the wdt reset the chip if 8s passes without a reset (in the setup)
wdt_reset(); // reset the wdt (in the void loop)

Can you also please give me some tips on how to check if i'm using dynamic memory allocation.  There are no string variables but apart from that my C knowledge is very rusty.
Most of the variables I use are defined at the top of the script as ints, there are also a few arrays without explicitly choosing a size, e.g.

int sensor_pin[]={0,1};
int sensor_output[] = {0,0};

and functions look like this:

int read_sens(int pin_no)

The last question I had was regarding the 'void loop()', in old school programming language we used to always make sure that there was a delay() in the script to avoid saturating the CPU, is this important with the Arduino?

Thanks in advance for your help,

Nick Gammon

If you don't use malloc, new, or the String class, you probably are not using dynamic memory (unless one of the libraries you include, does).

There is no need for delay. The processor is running full-speed anyway, inside delay it is just running full-speed checking if time has elapsed.

Nick Gammon

Regarding delay, you are probably thinking of a multi-programming operating system, like Apple or Windows. In those cases, doing a delay let other programs run. That doesn't apply here.


Jan 28, 2013, 12:32 am Last Edit: Jan 28, 2013, 12:34 am by phthomps Reason: 1
Great! That has answered a lot of questions for me and put my mind at rest so thanks!

On another note, today I had some other serious issues with my Arduino micro board so I was hoping that you might have some idea.
After putting in the Watchdog timer reset, it pretty much appeared to kill the board.
I could no longer upload any program (not even the BareMinimum), the upload just got stuck for about a minute before timing out.
Both reset and powering off/on didn't fix it, rebooting PC etc..
I did a bit more digging and eventually found 1 fix which got it going again:
  - Hold the reset button.  Start uploading.  Release the reset button.
Noting else worked!
I tried to reproduce the WDT issue again and the same thing happened, exactly as before
When testing this script on my Arduino Uno it worked without problem.
Do you know if this is some nasty bug on the Arduino micro only since it works on the UNO? or could my script be wrong?
I have put the script below, any ideas would be very helpful.
Thanks again,

#include <avr/wdt.h>
void setup() {  
 wdt_enable(WDTO_8S); // have the wdt reset the chip if 8s passes without a reset
void loop() {  
 wdt_reset(); // reset the wdt

Nick Gammon

It's a well-known problem with some versions of the bootloader on the Arduino Mega.

Installing a corrected bootloader will fix it.

Your technique (of holding down reset) stops the sketch from loading after a power-off, so the problem WDT is not activated.

Go Up