atmega randomly drops into reset loop

Hi,

I’ve built this little switch to open my garage door, and most of the time it works alright, but sometimes it seems to get stuck and I have no idea where or why that happens. Schematic and sourcecode are attached.

All eeprom related commands are for debugging only. So far I found out that at some point the controller seems to start resetting itself until the battery dies.

These are my fuse settings:
avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as D1
avrdude: safemode: efuse reads as 7

I don’t really understand the startup time settings, so maybe there’s something wrong with that.

Any help is appreciated!

Cheers,

Alexander

main.cpp (6.1 KB)

I don't know what's wrong, but I'm struck by the number of EEPROM updates you're making. Do you realise that your EEPROM only has a finite number of times it can be written to? It's designed to be good for 100,000 updates, but at the rate you're making updates it wouldn't take you long to exceed that.

Do you have anything connected to the reset pin? When the problem happens, do you still have a healthy steady voltage on the 5V pin? What are the symptoms that make you conclude it is resetting itself?

Apart from the number of EEPROM writes, I can see a few possible issues:

  1. You are enabling the pin change interrupt before you configure the pins. Do it the other way round, i.e. set up the pins as inputs (and you could use INPUT_PULLUP mode instead of writing HIGH to the port), and only then enable the pin change interrupt.

  2. As you are using the watchdog, you should reset and disable it at the beginning of setup().

In my experience, reset loops are usually caused by using the watchdog incorrectly.

Regarding the EEEPROM writes, I realize that the EEPROM can be written only 100.000 times, but as I said, these are for debugging only. Once I know what’s going on I’ll throw them out again. Also the flash can be written only 10.000 times, and at the rate I’m reflashing the device right now, I might easily hit that limit first. :wink:

PeterH:
Do you have anything connected to the reset pin? When the problem happens, do you still have a healthy steady voltage on the 5V pin? What are the symptoms that make you conclude it is resetting itself?

I have a 10k pullup to VCC and a 100nF to ground, as seen in the schematic. I haven’t measured the voltage on the reset pin yet, I’ll do that next time the bug strikes. The conclusion that it’s resetting itself comes from the counters in the EEPROM: The counters for wakeup, sleep and button presses represent the actual number of times these events happened, while the reset counter is through the roof.

dc42:
Apart from the number of EEPROM writes, I can see a few possible issues:

  1. You are enabling the pin change interrupt before you configure the pins. Do it the other way round, i.e. set up the pins as inputs (and you could use INPUT_PULLUP mode instead of writing HIGH to the port), and only then enable the pin change interrupt.

  2. As you are using the watchdog, you should reset and disable it at the beginning of setup().

In my experience, reset loops are usually caused by using the watchdog incorrectly.

  1. I’ll try changing the order of enabling the interrupt and enabling the internal pullups. I tried using INPUT_PULLUP but my version of Arduino does not seem to support it.

  2. Ironically I used the watchdog because I suspected the process of setting up the RFM12 and sending my garage door opening sequence to hang (it takes quite long, about 300ms). Maybe I replaced one problem with another…

I’ll try and do what you told me, but I don’t really understand what that’ll change exactly. Could you go a bit more into the theory behind that or point me to some documentation?

Just wanted to let you know, I think i figured it out:

It actually was the watchdog, or rather the way I implemented it. In the end all I had to do was add a line at the beginning of my setup() that reads "MCUSB = 0;". Without that the MCU would keep resetting itself when the watchdog triggered a reset. With this line present everything seems to work as intended.

The updated code is in my github repository: https://github.com/alibenpeng/keyfob

Cheers,

Alexander