Hi guys,
I'm currently working on a little temperature data logger and have compiled the following code below. However at present I am using the delay function to wait between writes, but from what I have read online the delay function is limited in terms of how big a delay can be utilised. For this project I would like the code to have the flexibility to accommodate a long delay (say 1hr). I would like your advice on how to best implement this.
#include <SPI.h>
#include <SD.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DS18B20 7
int time = 0;
const int chipSelect = 4;
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);
File myFile;
void setup() {
time =0;
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
// open the file.
File dataFile = SD.open("temp.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
time++;
String dataString = "";
dataString += String(time);
dataString += "\t";
dataString += sensors.getTempCByIndex(0);
dataFile.println(dataString);
dataFile.close();
}
// if the file isn’t open
else
{
Serial.println("error opening temp.txt");
delay(1000);
}
}
Please note at present I intend to move the project onto a bare bones arduino I've made (not sure if this has any bearing on coding approaches) but will also likely be powered by either a wall charger or battery.
Side note, I've been testing my project out on a breadboard at the moment and noted occasionally the first component of the datastring which counts the writing interval occasionally resets back to 1, any idea what could cause this?