Data randomly stops printing to SD? help!

Hi,

I have an Arduino Pro 328 5V with an adafruit datalogging shield which I am using to log data from 9 ds18b20 temperature sensors.

I have them all connected to the same digital data pin 3, with ground and power connections.

Everything seems to be working fine, but data just stops printing to the SD card after somewhere between 5 and 30 minutes.

I need to have this log continuously at 5 min intervals from battery power (6 V sealed lead acid battery), and get it working soon for scientific data collection.

Not sure if I have a hardware or software problem. My code is below. Any help is appreciated!!

John

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>'
#include "RTClib.h"
RTC_DS1307 RTC; // define the Real Time Clock object
//RTC_Millis RTC;

File myFile;

#define ONE_WIRE_BUS_PIN 3
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);

//int numberOfDevices; // Number of temperature devices found

//DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

const int chipSelect = 10;

//float tempC, Temp01, Temp02, Temp03, Temp04, Temp05, Temp06, Temp07, Temp08, Temp09;
DeviceAddress Probe01 = { 0x28, 0xFF, 0x5C, 0x37, 0x2E, 0x04, 0x00, 0x69 };
DeviceAddress Probe02 = { 0x28, 0xFF, 0x5D, 0x78, 0x65, 0x14, 0x01, 0x9C };
DeviceAddress Probe03 = { 0x28, 0xFF, 0x20, 0x80, 0x65, 0x14, 0x01, 0x70 };
DeviceAddress Probe04 = { 0x28, 0xFF, 0x03, 0x2F, 0x65, 0x14, 0x01, 0xF5 };
DeviceAddress Probe05 = { 0x28, 0xFF, 0x39, 0x92, 0x65, 0x14, 0x01, 0xC0 };
DeviceAddress Probe06 = { 0x28, 0xFF, 0x9B, 0x38, 0x65, 0x14, 0x01, 0xC2 };
DeviceAddress Probe07 = { 0x28, 0xFF, 0x0F, 0x38, 0x65, 0x14, 0x01, 0x6A };
DeviceAddress Probe08 = { 0x28, 0xFF, 0xE6, 0x68, 0x64, 0x14, 0x02, 0xD2 };
DeviceAddress Probe09 = { 0x28, 0xFF, 0x6D, 0xD1, 0x64, 0x14, 0x02, 0x6B };

void measure(){
char filename[] = "Testfile.CSV";
SD.begin(chipSelect);
myFile = SD.open(filename, FILE_WRITE);

//pinMode(10, OUTPUT);

DateTime now = RTC.now();

//make date and time
char dt[16];
char tm[16];

sprintf(dt, "%02d/%02d/%02d", now.year(),now.month(),now.day());
sprintf(tm, "%02d:%02d:%02d", now.hour(),now.minute(),now.second());

myFile.print(dt);
myFile.print(" ");
myFile.print(tm);
myFile.print(",");

Serial.print(dt);
Serial.print(" ");
Serial.print(tm);
Serial.print(",");

// Initialize the Temperature measurement library
sensors.begin();
// set the resolution to 12 bit
sensors.setResolution(Probe01, 12);
sensors.setResolution(Probe02, 12);
sensors.setResolution(Probe03, 12);
sensors.setResolution(Probe04, 12);
sensors.setResolution(Probe05, 12);
sensors.setResolution(Probe06, 12);
sensors.setResolution(Probe07, 12);
sensors.setResolution(Probe08, 12);
sensors.setResolution(Probe09, 12);

//Get temperature from sensors
sensors.requestTemperatures();

float Temp01 = sensors.getTempC(Probe01);
float Temp02 = sensors.getTempC(Probe02);
float Temp03 = sensors.getTempC(Probe03);
float Temp04 = sensors.getTempC(Probe04);
float Temp05 = sensors.getTempC(Probe05);
float Temp06 = sensors.getTempC(Probe06);
float Temp07 = sensors.getTempC(Probe07);
float Temp08 = sensors.getTempC(Probe08);
float Temp09 = sensors.getTempC(Probe09);

//Print temp to SD
myFile.print(Temp01);
myFile.print(",");
myFile.print(Temp02);
myFile.print(",");
myFile.print(Temp03);
myFile.print(",");
myFile.print(Temp04);
myFile.print(",");
myFile.print(Temp05);
myFile.print(",");
myFile.print(Temp06);
myFile.print(",");
myFile.print(Temp07);
myFile.print(",");
myFile.print(Temp08);
myFile.print(",");
myFile.print(Temp09);
myFile.println();

Serial.print(Temp01);
Serial.print(",");
Serial.print(Temp02);
Serial.print(",");
Serial.print(Temp03);
Serial.print(",");
Serial.print(Temp04);
Serial.print(",");
Serial.print(Temp05);
Serial.print(",");
Serial.print(Temp06);
Serial.print(",");
Serial.print(Temp07);
Serial.print(",");
Serial.print(Temp08);
Serial.print(",");
Serial.print(Temp09);
Serial.println();

}

void setup()
{

Serial.begin(9600);
delay(300);
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);

Serial.print("Initializing");
delay(2000);
pinMode(10, OUTPUT);

//digitalWrite(10, HIGH);
delay(1);

if (!SD.begin(chipSelect))
{
Serial.print("failed!");
delay (2000);
return;
}
Serial.println(" init. OK!");

sensors.begin();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());

char filename[] = "Testfile.CSV";
myFile = SD.open(filename, FILE_WRITE);

//Set addresses for digital sensors
sensors.setResolution(Probe01, 12);
sensors.setResolution(Probe02, 12);
sensors.setResolution(Probe03, 12);
sensors.setResolution(Probe04, 12);
sensors.setResolution(Probe05, 12);
sensors.setResolution(Probe06, 12);
sensors.setResolution(Probe07, 12);
sensors.setResolution(Probe08, 12);
sensors.setResolution(Probe09, 12);

Wire.begin();
if (!RTC.begin()) {
myFile.println("RTC failed");
Serial.println("RTC failed");
}

DateTime now = RTC.now();
DateTime compiled = DateTime(DATE, TIME);
if (now.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(DATE, TIME));
}

}

void loop()
{
//DateTime now = RTC.now();
//log every minute
//if (now.second() > 00 || now.second() < 10) {

measure();
myFile.flush();
delay(60000);

}

The problem could be the power supply. How are you connecting the battery? Minimum through the barrel jack is 7v.

You don't need to set the resolution, but you seem to be doing it twice.

Check the please read about posting code.

Thank for the reply and sorry about the incorrect posting of code. I reposted correctly in this reply.

I have a 6V lead acid battery connected to a barrel jack soldered onto the pro. But even when connected to the computer through USB, the data stops printing to SD after about 10 minutes, but continues to print to the serial monitor indefinitely.

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>                  
#include <SD.h>'
#include "RTClib.h"     
RTC_DS1307 RTC; // define the Real Time Clock object
//RTC_Millis RTC;

File myFile;

#define ONE_WIRE_BUS_PIN 3
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);

//int numberOfDevices; // Number of temperature devices found

//DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

const int chipSelect = 10;

//float tempC, Temp01, Temp02, Temp03, Temp04, Temp05, Temp06, Temp07, Temp08, Temp09;
DeviceAddress Probe01 = { 0x28, 0xFF, 0x5C, 0x37, 0x2E, 0x04, 0x00, 0x69 }; 
DeviceAddress Probe02 = { 0x28, 0xFF, 0x5D, 0x78, 0x65, 0x14, 0x01, 0x9C };
DeviceAddress Probe03 = { 0x28, 0xFF, 0x20, 0x80, 0x65, 0x14, 0x01, 0x70 };
DeviceAddress Probe04 = { 0x28, 0xFF, 0x03, 0x2F, 0x65, 0x14, 0x01, 0xF5 };
DeviceAddress Probe05 = { 0x28, 0xFF, 0x39, 0x92, 0x65, 0x14, 0x01, 0xC0 };
DeviceAddress Probe06 = { 0x28, 0xFF, 0x9B, 0x38, 0x65, 0x14, 0x01, 0xC2 }; 
DeviceAddress Probe07 = { 0x28, 0xFF, 0x0F, 0x38, 0x65, 0x14, 0x01, 0x6A };
DeviceAddress Probe08 = { 0x28, 0xFF, 0xE6, 0x68, 0x64, 0x14, 0x02, 0xD2 };
DeviceAddress Probe09 = { 0x28, 0xFF, 0x6D, 0xD1, 0x64, 0x14, 0x02, 0x6B };

void measure(){
  char filename[] = "Testfile.CSV";
  SD.begin(chipSelect);
  myFile = SD.open(filename, FILE_WRITE); 
  
  //pinMode(10, OUTPUT);
  
  DateTime now = RTC.now();
  
//make date and time
 char dt[16];
 char tm[16];

 sprintf(dt, "%02d/%02d/%02d", now.year(),now.month(),now.day());
 sprintf(tm, "%02d:%02d:%02d", now.hour(),now.minute(),now.second());

 myFile.print(dt);
 myFile.print(" ");
 myFile.print(tm);
 myFile.print(",");
  
 Serial.print(dt);
 Serial.print(" ");
 Serial.print(tm);
 Serial.print(",");
 
// Initialize the Temperature measurement library
  sensors.begin();    
 // set the resolution to 12 bit
  sensors.setResolution(Probe01, 12);
  sensors.setResolution(Probe02, 12);
  sensors.setResolution(Probe03, 12);
  sensors.setResolution(Probe04, 12);
  sensors.setResolution(Probe05, 12);
  sensors.setResolution(Probe06, 12);
  sensors.setResolution(Probe07, 12);
  sensors.setResolution(Probe08, 12);
  sensors.setResolution(Probe09, 12);

//Get temperature from sensors
sensors.requestTemperatures();

float Temp01  = sensors.getTempC(Probe01);
float Temp02  = sensors.getTempC(Probe02);
float Temp03  = sensors.getTempC(Probe03);
float Temp04  = sensors.getTempC(Probe04);
float Temp05  = sensors.getTempC(Probe05);
float Temp06  = sensors.getTempC(Probe06);
float Temp07  = sensors.getTempC(Probe07);
float Temp08  = sensors.getTempC(Probe08);
float Temp09  = sensors.getTempC(Probe09);

//Print temp to SD
  myFile.print(Temp01);
  myFile.print(",");
  myFile.print(Temp02);
  myFile.print(",");
  myFile.print(Temp03);
  myFile.print(",");
  myFile.print(Temp04);
  myFile.print(",");
  myFile.print(Temp05);
  myFile.print(",");
  myFile.print(Temp06);
  myFile.print(",");
  myFile.print(Temp07);
  myFile.print(",");
  myFile.print(Temp08);
  myFile.print(",");
  myFile.print(Temp09);
  myFile.println();


 
Serial.print(Temp01);
Serial.print(",");
Serial.print(Temp02);
Serial.print(",");
Serial.print(Temp03);
Serial.print(",");
Serial.print(Temp04);
Serial.print(",");
Serial.print(Temp05);
Serial.print(",");
Serial.print(Temp06);
Serial.print(",");
Serial.print(Temp07);
Serial.print(",");
Serial.print(Temp08);
Serial.print(",");
Serial.print(Temp09);
Serial.println();

}
 
void setup()   
{
  
  Serial.begin(9600);
    delay(300);
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  Serial.print("Initializing");
  delay(2000);
  pinMode(10, OUTPUT);

  //digitalWrite(10, HIGH);
  delay(1);
  
 if (!SD.begin(chipSelect)) 
  {
  Serial.print("failed!");
    delay (2000);
    return;
  }
  Serial.println("  init. OK!");
  
  sensors.begin();
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(sensors.getDeviceCount());  

 char filename[] = "Testfile.CSV";
 myFile = SD.open(filename, FILE_WRITE); 

  Wire.begin();  
  if (!RTC.begin()) {
    myFile.println("RTC failed");
    Serial.println("RTC failed");
  }


  DateTime now = RTC.now();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (now.unixtime() < compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  
}

void loop()   
{
//DateTime now = RTC.now();
//log every minute
     //if (now.second() > 00 || now.second() < 10) { 
      
   measure();
   myFile.flush();
   delay(60000);    
 
  }

You could try put a myFile.close() after the file is written in the measure function and not have the myFile.flush() in the loop.

It will have the open and close together which will definitely write to the file, and for some reason, there may be some sort of buffer overflow with myFile.flush().

OK, your power is probably still suss, but absolved from this problem.

I have never heard of myFile.flush() and I bet it is either redundant or the problem.

Here is a basic temp to SD. The SD is open only when you write to it.

/* Basic 2xDS18B20 code for serial monitor, bluetooth, Excel or w.h.y.
Derived from Hacktronics. USE THEIR ADDRESS SNIFFER and substitute your 
numbers. Use Hacktronics connections diagram. 
http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
Stay away from using parasite power
-127C means bad connection
85 means you haven't gotten a read yet, probably just the 
wrong order of commands
*/

#include <OneWire.h>
#include <DallasTemperature.h>
#include "Wire.h"                
#include <SD.h>
#include <SPI.h>                

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
  
byte Thermo1[8] = {0x28, 0x39, 0xFD, 0x50, 0x04, 0x00, 0x00, 0X69};
byte Thermo2[8] = {0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};
float tempC,Temp1,Temp2;  

char filename[] = "Test.CSV";
File myFile;

void setup(){
  Serial.begin(9600);
  sensors.begin();
  delay(500);//Wait for newly restarted system to stabilize

  //pinMode(10, OUTPUT);// Uno
  pinMode(53, OUTPUT);//MEGA
    if (!SD.begin(chipSelect)) 
  {
    Serial.println("Card failed");
    // don't do anything more:
    return;
  }
    Serial.println("CARD OK");
}

void loop() {
 sensors.requestTemperatures();  // call readings from the addresses
  Temp1 = sensorValue(Thermo1);
  Temp2 = sensorValue(Thermo2);  

Serial.print("      Temp1 = ");
Serial.print(Temp1);
Serial.print("      Temp2 = "); 
Serial.println(Temp2);

  myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(Temp1);
  myFile.print(",");
  myFile.println(Temp2);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
       
delay(1000);
}

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

If you don't close the file it ties up a file descriptor in RAM, you must always clean up
your resources in a long-running program or it will jam up sooner or later.