Hello,
I'm using the Adafruit Data Logging Shield (Adafruit Data logging shield for Arduino [v1.0] : ID 243 : $24.80 : Adafruit Industries, Unique & fun DIY electronics and kits) to log the output of three temperature sensors and two LDRs to a 2GB SD card. However, I can only write a limited number of lines to the card. How many I can't predict, sometimes it's a few hundred, last night it was ~4700, sometimes it's only 20 or so. Normally the circuitry is not connected to a computer, but (from memory) I think that when it's hooked up to a computer the output to the serial display (temperatures, light info, time) continues but nothing is written any more, and I don't get any errors.
The sketch, as it si now, opens the file and then goes into an infinite while() loop in which it continuously writes to the file and flushes it, to avoid repeated open() and close() operations. But I have also used a variant of the code below in which for each writing operation the file is opened, a line is written, and then the file is closed again (this seemed to be the proper way of doing it). Unfortunately, this also didn't help.
What's going on? I don't even know how to debug this.
Any hints are much appreciated.
Enno
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <stdlib.h> // provides flat to string conversion function dtostrf
#include <OneWire.h>
#include <DallasTemperature.h>
// needed for the RTC
RTC_DS1307 RTC;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
int LDRwater=0;
int LDRheat=1;
double T;
int watersensor;
int heatsensor;
float temp1;
float temp2;
float temp3;
long unixtime;
String dataString;
char s[32]; // needed for float to string conversion using dtostrf
const int chipSelect = 10;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress Thermometer1 = {
0x10, 0x18, 0x58, 0x7F, 0x02, 0x08, 0x00, 0xA1 };
DeviceAddress Thermometer2 = {
0x28, 0xD6, 0xAC, 0x18, 0x04, 0x00, 0x00, 0xC0 };
DeviceAddress Thermometer3 = {
0x10, 0xA6, 0x54, 0x7F, 0x02, 0x08, 0x00, 0x94 };
void setup(){
Serial.begin(9600);
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// set the resolution to 12 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(Thermometer1, 12);
sensors.setResolution(Thermometer2, 12);
sensors.setResolution(Thermometer3, 12);
Wire.begin();
RTC.begin();
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)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// open file for writing and write a header line
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("#hh mm ss unixtime LDR0 LDR1 T1 T2 T3");
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
}
void loop(){
// open the file for writing
File dataFile = SD.open("datalog.txt", FILE_WRITE);
while(1){
watersensor=analogRead(LDRwater);
heatsensor=analogRead(LDRheat);
sensors.requestTemperatures(); // Send the command to get temperatures
temp1 = sensors.getTempC(Thermometer1);
temp2 = sensors.getTempC(Thermometer2);
temp3 = sensors.getTempC(Thermometer3);
Serial.println("--------------------------------------------------------");
Serial.print("Warmwasser: ");
Serial.println(watersensor);
Serial.print("Heizung: ");
Serial.println(heatsensor);
Serial.print("T1, T2, T3: ");
Serial.print(temp1);
Serial.print(", ");
Serial.print(temp2);
Serial.print(", ");
Serial.println(temp3);
DateTime now = RTC.now();
unixtime=now.unixtime();
dataString=String(now.hour())+" "+String(now.minute())+" "+String(now.second())+" "+String(unixtime)+" "+\
String(watersensor)+" "+String(heatsensor)+" "+\
dtostrf(temp1, 7, 4, s)+" "+dtostrf(temp2, 7, 4, s)+" "+dtostrf(temp3, 7, 4, s);
//File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.flush();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
while(1);
}
delay(10000);
}
}