Automatic periodic reset

I wanted a way to do a hardware reset on a periodic schedule. So far, this is the best I came up with:

int resetpin=11;

void setup(){
  pinMode(resetpin,INPUT);
  digitalWrite(resetpin,HIGH);
}

void loop(){

  if (millis() >= 3600000) {
    pinMode(resetpin,OUTPUT);
    digitalWrite(resetpin,LOW);
    delayMicroseconds(100);
  }
}

Jumper digital pin 11 to the RESET pin, which is near the analog input ports (at least, it is on the Diecimila.

I played games with the direction of the pin, otherwise it was was constantly resetting. The final delay is probably overkill, the datasheet says that RESET needs to be pulled low for 2.5uS.

Cant you just fiddle with the watchdog timer to automatically reset without needing a IO pin?

Yes, you can do it.

In Atmega8, put this line at the moment you want to reset:
WDTCR = (1<<WDE);

In Atmega168 (I’m not sure) you can try this:
WDTCSR = (1<<WDE);

I did twiddle with the WDT, on a 168, using:

WDTCSR = (1<<WDE);

But for some reason, things went from bad to worse. I’m not entirely sure what happened, but basically it wedged the bootloader, to the point where I actually had to re-burn the bootloader on my STK500. I couldn’t load new sketches, which is why I reloaded the bootloader. I haven’t tried to look too deep: either the default prescaler was too low, and the code was never executing my WDR before the timeout, or the bootloader itself uses the WDT.

have you seen this note on software reset : http://support.atmel.no/bin/customer?custSessionKey=&customerLang=en&noCookies=true&action=viewKbEntry&id=21

the example code compiles in the arduino environement but I have not tested it to see if it works.

Heh, I’m reading that, and one of the warnings says:

You should not try to:
Use another pin of the AVR to pull the external RESET line

So there goes my idea above! I’ll give the code a shot when I get home.

I just tried a test with the appnote that mem posted.

I put an LED on pin 11, because the Diecimila’s LED on pin 13 starts acting funny. Pin 11 pulses the expected number of times, and then the board resets. When the WDT resets the device, the built-in LED on pin 13 blinks in very rapid sequence, and just keeps blinking. But it never appears to start re-executing the sketch.

Pressing the hardware reset button has no affect, I need to disconnect the USB cable and reconnect it, then the device will execute the sketch as above.

Good thing I gave it a 10 second delay, because during this event, I can’t upload a new sketch. I’ve disconnected anything I’ve attached, other than the LED.

I hope someone else will read this and test it, and let me know the results. Maybe I can’t use millis in this manner?

#include <avr/io.h>
#include <avr/wdt.h>

int ledpin=11;

void setup()
{
  pinMode (ledpin,OUTPUT);
}

void loop()
{
  digitalWrite(ledpin,HIGH);
  delay(500);
  digitalWrite(ledpin,LOW);
  delay(500);
  
  if (millis() >= 10000) {
    wdt_enable(WDTO_30MS); 
    while(1) {}
  }

}

Sorry if my suggestion was not good.
I tryed with Atmega8 and worked.

But looking the datasheet, perhaps this could work:
WDTCSR = (1<<WDTON) | (1<<WDE) | (0<<WDIE);

WDTON is a fuse, I can’t set it this way. I programmed WDTON=1 with my STK500, but that doesn’t allow me to upload sketches anymore. I even erased the device, reflashed the bootloader, and programmed WDTON=1. Then when I connected the USB and tried to upload a sketch, it times out. When I unprogram the WDTON fuse, the bootloader is fine.

Well, I give up, since if it works for you, there’s obviously something I’m missing or doing wrong.

I thought WDTON was a register like all others.

I said I did it in Atmega8 (WDTCR = (1<<WDE);), not as bootloader, but inside the blink sketch, at the end of code.

So Arduino blinks once, then blinks again for reset. 8seconds before it blinks once, and blink for reset again…

So I thought that if you change the same register for Atmega168:

WDTCSR = (1<<WDTON) | (1<<WDE) | (0<<WDIE);

it would work like my ATmega8.