How to Approach Long Delays

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?

Look for the standard example "blink without delay" to see how you can use the function millis() to achieve the same as a delay without blocking the rest of your code.
Interrupts are "immune" to delays in the main loop(), but not a substitute for them.
Try declaring time as unsigned long instead of integer.

from what I have read online the delay function is limited in terms of how big a delay can be utilised.

It certainly is. You can't delay more than 49 days. If that is a problem, you could delay() in a for loop, to delay for eternity.

The problem with delay() is that you can't do anything else while you have your thumb stuck up your ass. So, don't use delay(). If the interval between events is reasonable, use the blink without delay example's philosophy.

If the interval is longer, get an RTC, and have the Arduino do things EXACTLY like you would (look at your watch; it is time?).

Beware that the small low cost arduino does not have temperature compensation built into the MHz clock crystal, so you should expect your board to be +- 2 parts in 10000 off in the timing, and worse if it is in an unsteady environment such as an industrial shed, That might not matter, but don't expect to rely too much on only the arduino clock.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing. It may help with understanding the technique.

...R

You may wish to look into putting it to sleep for that period of time.

Thanks for the responses, Yes I have seen a watchdog example which puts it to sleep. I will check out the sleep function and the millis and report back