Simple watchdog description for Uno
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- spiney
- Posts: 214
- Joined: Mon Jul 09, 2012 6:35 am
Simple watchdog description for Uno
I have googled Arduino watchdog and found rafts of confusing and frightening posts. Can you point me to a simple description and a "How-to" please? My Uno hangs after about 80 readings of weather data. I have found a recipe for an external 555 timer watchdog circuit - maybe that is the safer, if power-hungry, solution?
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Simple watchdog description for Uno
The Atmel processor has a watchdog timer you ought to be able to use. I'd have to do a bit of research on enabling it - you might have to change a fuse setting. You should also be able to construct a simple watchdog from a regular timer interrupt.
If you're blowing up after 80 reads, you've probably got some sort of memory leak - or more likely, an issue with memory fragmentation.
Are you doing any dynamic memory allocation? Using the String library perhaps? Or using malloc? What other libraries are you using?
If you're blowing up after 80 reads, you've probably got some sort of memory leak - or more likely, an issue with memory fragmentation.
Are you doing any dynamic memory allocation? Using the String library perhaps? Or using malloc? What other libraries are you using?
- spiney
- Posts: 214
- Joined: Mon Jul 09, 2012 6:35 am
Re: Simple watchdog description for Uno
Thank you for your reply. But it was scary! What does changing a fuse mean?
I am using 2 Alarmtimer repeats and an interrupt function for a rain gauge (no rain - so that is not involved). I have about 373 free SRAM. The code is about 30k. I am using 2 SD files open at the same time. The Adafruit error check for the 2nd file fails and stops the programme. (So it is commented out)
I don't understand "Dynamic Memory Allocation" or malloc. Adafruit code for the SD file name change uses a string for the error call.
Here are the libraries:
I am using 2 Alarmtimer repeats and an interrupt function for a rain gauge (no rain - so that is not involved). I have about 373 free SRAM. The code is about 30k. I am using 2 SD files open at the same time. The Adafruit error check for the 2nd file fails and stops the programme. (So it is commented out)
I don't understand "Dynamic Memory Allocation" or malloc. Adafruit code for the SD file name change uses a string for the error call.
Here are the libraries:
Code: Select all
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>
#define PIN 3 // the pin for button/ rain gauge
#define STOP_SWITCH 2
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Simple watchdog description for Uno
It's easy. Most people just shove a penny in the socket. Just make sure you're not standing in a puddle at the time.Spiney wrote:Thank you for your reply. But it was scary! What does changing a fuse mean?
Fuses are just non-volatile configuration registers. They retain their settings even when power is removed. There are three fuse registers on the Uno. I just looked it up, and one of the fuse bits is used to prevent the watchdog timer from ever being turned off. That's not set by default on the Uno, but it doesn't matter. You can still turn the watchdog timer on.
The Arduino uses the GNU C runtime library, which implements some watchdog functions. The reference page is here:
http://www.nongnu.org/avr-libc/user-man ... chdog.html
It's pretty simple - why don't you play around with it a bit. All you have to do is include the header file in your sketch, and then call wdt_enable() with a timeout value. The library defines a bunch of constants for the timeout value. After that, you just have to call wdt_reset() regularly to prevent the watchdog from resetting your arduino.
I hacked up a little watchdog demo sketch. It's attached below...
- Attachments
-
- watchdog_demo.zip
- (842 Bytes) Downloaded 57 times
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Simple watchdog description for Uno
Does the amount of free ram change more than a little, or does it stay pretty much constant?Spiney wrote: I have about 373 free SRAM. The code is about 30k. I am using 2 SD files open at the same time. The Adafruit error check for the 2nd file fails and stops the programme. (So it is commented out)
I don't understand "Dynamic Memory Allocation" or malloc. Adafruit code for the SD file name change uses a string for the error call.
The sketch blows up when you try to open the second file? Are you calling close() on the first file before you try to open the second file?
We've got a tutorial on Arduino memories that talks about dynamic memory allocation:
http://learn.adafruit.com/memories-of-an-arduino
The SD file name change uses a string, but it doesn't use the String library (the word "string" is probably the single most overloaded and confusing word in programming).
Last edited by adafruit_support_rick on Wed Aug 28, 2013 4:44 pm, edited 1 time in total.
Reason: forgot link...
Reason: forgot link...
- spiney
- Posts: 214
- Joined: Mon Jul 09, 2012 6:35 am
Re: Simple watchdog description for Uno
Thank you very much for those replies.
The free RAM appears to stay constant while I was calling the readings at 60 second intervals. (Normally done at 30 minutes).
No I don't close the 1st file. And No it is not opening the 2nd file which stops the program - only asking the confirmation check that it has been opened caquses the error. I have been advised that I may keep both open and indeed it is necessary because I want to record Temp, pressure & RH on one file and the (rare) raingauge interrupts on the 2nd file. Both files are opened on the SD card and both have recorded data, though perhaps not precisely simultaneously - don't know! The Adafruit file name check code works fine for the 1st file but if I uncomment it for the 2nd file it prints the error and the program stops. I have been messing about with flushing but have now decided that the 30 minute readings can be flushed automatically when the buffer is full. For the raingauge data I have required that flushing is done only if there is an interval of more than a minute between interrupts. (Continuous heavy rain will mean interrupts at a repetition interval of 2 seconds.) If I uncomment the check I get "couldnt create rnfile". If I leave it commented out both files are opened and do recieve data.
I have asked if there is a buffer for each file but I did not get an answer to that question. I have been assuming that there probably are separate buffers - otherwise my "more than a minute" flush is probably unnecessary. (Since the rain file buffer may be half full when the rain stops - if it did not rain again for a month, that data might remain unrecorded? Here is the check code:-
The free RAM appears to stay constant while I was calling the readings at 60 second intervals. (Normally done at 30 minutes).
No I don't close the 1st file. And No it is not opening the 2nd file which stops the program - only asking the confirmation check that it has been opened caquses the error. I have been advised that I may keep both open and indeed it is necessary because I want to record Temp, pressure & RH on one file and the (rare) raingauge interrupts on the 2nd file. Both files are opened on the SD card and both have recorded data, though perhaps not precisely simultaneously - don't know! The Adafruit file name check code works fine for the 1st file but if I uncomment it for the 2nd file it prints the error and the program stops. I have been messing about with flushing but have now decided that the 30 minute readings can be flushed automatically when the buffer is full. For the raingauge data I have required that flushing is done only if there is an interval of more than a minute between interrupts. (Continuous heavy rain will mean interrupts at a repetition interval of 2 seconds.) If I uncomment the check I get "couldnt create rnfile". If I leave it commented out both files are opened and do recieve data.
I have asked if there is a buffer for each file but I did not get an answer to that question. I have been assuming that there probably are separate buffers - otherwise my "more than a minute" flush is probably unnecessary. (Since the rain file buffer may be half full when the rain stops - if it did not rain again for a month, that data might remain unrecorded? Here is the check code:-
Code: Select all
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++)
{
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create lgfile");
}
Serial.print("Logging to: ");
Serial.println(filename);
Serial.println();
// create a new file
char filename2[] = "RAINEY00.CSV";
for (uint8_t i = 0; i < 100; i++)
{
filename2[6] = i/10 + '0';
filename2[7] = i%10 + '0';
if (! SD.exists(filename2)) {
// only open a new file if it doesn't exist
rainfile = SD.open(filename2, FILE_WRITE);
break; // leave the loop!
}
}
//This should work but does not any more!?
/* if (! rainfile) {
error("couldnt create rnfile");
}
*/
Serial.print("Logging to: ");
Serial.println(filename2);
Serial.println();
- spiney
- Posts: 214
- Joined: Mon Jul 09, 2012 6:35 am
Re: Simple watchdog description for Uno
Thank you for the watchdog and memory links and for the watchdog demo. All very useful. I will follow these up tomorrow. Time for bed here now.
Regards,
Norman
Regards,
Norman
Please be positive and constructive with your questions and comments.