I am calling pressure readings from a BMP085 sensor within an Alarm.timer function. I have the watchdog set for 8 seconds and reset in the loop, which normally has little other work. Ocasionally the program freezes. Sometimes the watchdog does not cause a restart, but on one ocasion that it did I was able to see that it was the call to the BMP085 which had hung. I am using Adfruit's BMP085 library. How is it that the watchdog does not always sniff out the siezure? Is there a work-around?
Thank you for your reply; my apologies.
Yes I had read the advice and I thought that my lengthy code might be of no assistance for answering a known occurence. Here is the whole code.
#include <avr/wdt.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_BMP085.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Adafruit_RGBLCDShield.h>
#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>
#define PIN 3 // the pin for button/ rain gauge
#define STOP_SWITCH 2
bool stopped = false;
RTC_DS1307 RTC;
time_t syncProvider() //this does the same thing as RTC_DS1307::get()
{
return RTC.now().unixtime();
}
uint32_t syncTime = 0; // time of last sync()
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging files
File logfile;
File rainfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
while(1);
}
volatile int flag=0;
int nflag;
int Run = 0;
int test = 1;
int nutim = 0;
int oldtim = 0;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
byte fetch_humidity_temperature(unsigned int *p_Humidity, unsigned int *p_Temperature);
float maxT;
float minT = 90;
float maxTy;
float minTy;
byte _status;
unsigned int H_dat, T_dat;
float RH, T_C;
Adafruit_BMP085 bmp;
void setup()
{
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
RTC.adjust(DateTime(__DATE__, __TIME__));
}
RTC.adjust(DateTime(__DATE__, __TIME__));
setSyncProvider(syncProvider); //reference our syncProvider function instead of RTC_DS1307::get()
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(STOP_SWITCH, INPUT_PULLUP);
// initialize the SD card
Serial.print("InitSD");
// 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("Cd fail");
}
Serial.println("cdinit.");
// 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("No lgfile");
}
Serial.print("Log 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 - why?
/* if (! rainfile) {
error("couldnt create rnfile");
}
*/
Serial.print("Log to ");
Serial.println(filename2);
Serial.println();
pinMode(PIN, INPUT); //set the pin to input
digitalWrite(PIN, LOW); //use the internal pullup resistor
PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
// (RISING, FALLING and CHANGE all work with this library)
// and execute the function burpcount when that pin changes
Repeats();
/* while (minute() != 0) //Prog holds here until the hour.
{
delay(1000);
}
*/
Alarm.timerRepeat(1800, Repeats);
Alarm.timerRepeat(14, Repeat2);
Alarm.alarmRepeat(0,1,0, Repeat3);
wdt_enable(WDTO_8S);
}
void loop(){
if (LOW == digitalRead(STOP_SWITCH)){
logfile.flush();
rainfile.flush();
Serial.println("flush");
delay(10); //switch debounce interval
while (LOW == digitalRead(STOP_SWITCH)) { //stick here until stop switch is turned off.
stopped = true;
wdt_reset();
}
}
stopped = false; //stop switch is turned off
Alarm.delay(0); // wait 0 seconds - something needed here or
if (flag != nflag){
nutim = minute();
if((nutim - oldtim) >= 2){
test = 1;}
oldtim = minute();
Serial.print(flag);
Serial.print(",");
Serial.print(month());
Serial.print('/');
Serial.print(day());
Serial.print(' ');
Serial.print(hour());
Serial.print(':');
Serial.print(minute());
Serial.print(':');
Serial.print(second());
Serial.println();
rainfile.print(flag);
rainfile.print(",");
rainfile.print(month());
rainfile.print('/');
rainfile.print(day());
rainfile.print(' ');
rainfile.print(hour());
rainfile.print(':');
rainfile.print(minute());
rainfile.print(':');
rainfile.print(second());
rainfile.println();
if (test == 1){
rainfile.flush();
Serial.println(F("flushed"));
}
test = 0;
}
nflag = flag;
wdt_reset();
}
void Repeats(){
_status = fetch_humidity_temperature(&H_dat, &T_dat);
RH = (float) H_dat * 6.10e-3;
T_C = (float) T_dat * 1.007e-2 - 40.0;
maxT = max(T_C, maxT);
minT = min(T_C, minT);
logfile.print(day(), DEC);
logfile.print("/");
logfile.print(month(), DEC);
logfile.print("/");
logfile.print(year(), DEC);
logfile.print(" ");
logfile.print(hour(), DEC);
logfile.print(":");
logfile.print(minute(), DEC);
logfile.print(":");
logfile.print(second(), DEC);
logfile.print(",");
Serial.print(hour(), DEC);
Serial.print(":");
Serial.print(minute(), DEC);
Serial.print(":");
Serial.println(second(), DEC);
logfile.print(T_C, 2);
logfile.print(",");
logfile.print(RH, 1);
logfile.print(",");
logfile.print(bmp.readPressure());
logfile.println();
logfile.flush();
Serial.print("Pres ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print("RH ");
Serial.print(RH, 1);
Serial.print(" T ");
Serial.println(T_C, 2);
Serial.print(F("Tmax "));
Serial.println(maxT, 2);
Serial.print(F("Tmin "));
Serial.println(minT, 2);
Serial.println();
}
void Repeat2(){
Run ++ ;
if (Run > 1) {
lcd.setCursor(0, 0);
lcd.print(F(" "));
lcd.setCursor(0, 0);
lcd.print("P ");
lcd.print(bmp.readPressure());
lcd.setCursor(8, 0);
lcd.print(" Rn");
lcd.print(flag);
lcd.setCursor(0, 1);
lcd.print("RH ");
lcd.print(RH, 1);
lcd.print(" ");
lcd.print(" T ");
lcd.print(T_C, 2);
Run=0 ;
}
else {
lcd.setCursor(0, 0);
lcd.print(F(" "));
lcd.setCursor(0, 0);
lcd.print(F("MxT"));
lcd.print(maxT, 2);
lcd.print(F("MnT"));
lcd.print(minT, 2);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(F("Yx "));
lcd.print(maxTy, 2);
lcd.print(F("Yn "));
lcd.print(minTy, 2);
}
}
void Repeat3(){
logfile.print(",");
logfile.print(",");
logfile.print(",");
logfile.print(",");
logfile.print(",");
logfile.print(day(), DEC);
logfile.print("/");
logfile.print(month(), DEC);
logfile.print("/");
logfile.print(year(), DEC);
logfile.print(",");
logfile.print(maxT, 2);
logfile.print(",");
logfile.print(minT, 2);
logfile.println();
logfile.flush();
maxTy = maxT;
minTy = minT;
maxT = 0;
minT = 99;
}
void burpcount()
{
flag++;
}
byte fetch_humidity_temperature(unsigned int *p_H_dat, unsigned int *p_T_dat)
{
byte address, Hum_H, Hum_L, Temp_H, Temp_L, _status;
unsigned int H_dat, T_dat;
address = 0x27;
;
Wire.beginTransmission(address);
Wire.endTransmission();
delay(100);
Wire.requestFrom((int)address, (int) 4);
Hum_H = Wire.read();
Hum_L = Wire.read();
Temp_H = Wire.read();
Temp_L = Wire.read();
Wire.endTransmission();
//_status = (Hum_H >> 6) & 0x03;
Hum_H = Hum_H & 0x3f;
H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;
T_dat = (((unsigned int)Temp_H) << 8) | Temp_L;
T_dat = T_dat / 4;
*p_H_dat = H_dat;
*p_T_dat = T_dat;
return(_status);
}