# Arduino w/ Atmega328: can't clear the watchdog

I'm using the onboard software watchdog facility of an Atmega328 like documented here.
My purpose is to limit to 2 seconds the execution of a portion of code that could lead to endless computation.

This is the pseudo-code scheme:

enable watchdog (2 seconds)
run may-be-endless code
clear watchdog
run the rest


The rest of the code should be run if the may-be-endless code manage to run in the given amount of time, otherwise the whole program must be restarted.

This is the Arduino code I loaded into the board:

#include <avr/wdt.h>

void setup() {

Serial.begin(9600);

Serial.println("Started");

wdt_enable(WDTO_2S);
Serial.println("watchdog ON");

wdt_reset();
Serial.println("watchdog OFF");

}

void loop(){
Serial.println("loop...");
delay(1000);
}


At this point, the expected output should be an endless printing of loop... messages, after the ones in the setup (Started, watchdog ON/OFF).

Surprisingly, what I get instead is this:

Started
watchdog ON
watchdog OFF
loop...
loop...
loop...
Started
watchdog ON
watchdog OFF
loop...
loop...
loop...
Started
watchdog ON
watchdog OFF
loop...
loop...
[...]


Appartently the watchdog is being set but can't be cleared. How come?

wdt_reset()


does not disable the watchdog, it merely resets its timer, delaying the expiration.

You probably want

wdt_disable()


However, your overall approach may not be the most sound - letting the watchdog expire can be clever, but it is also fairly heavy-handed compared to approaches such as using a normal timer interrupt or having your code limit itself. Normally the watchdog is for unanticipated errors.

• Thank you... I feel ashamed that I didn't check wdt_disable before, it was that easy. I was distracted by several how-tos that I read which stated that wdt_reset was the way, and also the documentation was a little bit ambiguos to me. – etuardu Apr 15 '13 at 15:44

To disable the watchdog timer call wdt_disable(). All wdt_reset() does is reset the watchdog timeout, which in your case is redundant: you just turn it on two lines above. wdt_reset() does not turn the watchdog timer off (contrary your serial output).
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3876756429672241, "perplexity": 5231.423469865686}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 5, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610703519923.26/warc/CC-MAIN-20210120054203-20210120084203-00493.warc.gz"}