Points noted and thanks for the tip - have included the full code below.
I have downloaded the Time library from the Playground but I obviously need library examples for the DS1307. I shall look around and see what I can find.
/*
* SimpleMeterLogger.pde - Meter reading logic for logging
* This version logs pulses on digital pin 3
*
* Copyright Michael Margolis 2010
* In association with www.airsensor.co.uk
*/
#include <Fat16.h> // the SD Card library - http://code.google.com/p/fat16lib/
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h> // a basic DS1307 library - http://www.arduino.cc/playground/Code/Time
const long logInterval = 10; // the interval in seconds between each log
// LED pins
const int dataLedPin = 4; // LED indicating sensor data is received
const int logLedPin = 5; // LED flashes during a log attemp
const int fileLedPin = 6; // LED turns on if a file error occurs
const char *fileName = "logdata.csv"; // the name of the log file
const int logInterrupt = 1; // ATmega 168 and 328 - interrupt 0 = pin 2, 1 = pin 3
const int interruptPin = 3;
SdCard card;
Fat16 LogFile;
volatile unsigned long wattSensor = 0; // Counts power pulses in interrupt, 1 pulse = 1 watt
volatile byte dataState = 0; // used to blink the Led to indicate data updates
unsigned long totalWatts = 0; // Total power used since the sketch started ???
time_t nextTrigger;
void setup(void)
{
setSyncProvider(RTC.get); // the function to get the time from the RTC
pinMode(dataLedPin, OUTPUT); // LED interrupt indicator initialization
pinMode(logLedPin, OUTPUT);
pinMode(fileLedPin, OUTPUT);
pinMode(interruptPin, INPUT);
digitalWrite(interruptPin, HIGH);
attachInterrupt(logInterrupt, interruptHandler, FALLING);
nextTrigger = now() + logInterval; // schedule the logging time
// initialize the SD card
if (!card.init())
error(1);
// initialize a FAT16 volume
if (!Fat16::init(&card))
error(2);
// open file for append, create if it doesn't exist
if (!LogFile.open(fileName, O_CREAT | O_APPEND | O_WRITE))
error(3);
// clear write error
LogFile.writeError = false;
LogFile.println("Start");
}
void loop(void)
{
time_t timeStamp = now();
if( timeStamp >= nextTrigger)
{
digitalWrite(logLedPin, HIGH);
printDateTime(timeStamp);
MeterPulseLog();
LogFile.println();
// write the data to the card at the end of every line
if (!LogFile.sync())
error(4);
nextTrigger += logInterval;// schedule the next log time
digitalWrite(logLedPin, LOW);
}
// blink the LED to show meter pulses
if( digitalRead(interruptPin)== LOW ) // Light the LED when data pin is LOW
{
digitalWrite(dataLedPin, HIGH);
delay(100); // light for at least 100 ms
}
else
digitalWrite(dataLedPin, LOW);
}
void MeterPulseLog()
{
unsigned long wattSensorCount; //number of watts during this logging interval
uint8_t oldSREG = SREG; // save interrupt register
cli(); // prevent interrupts while accessing the count
wattSensorCount = wattSensor; //get the count from the interrupt handler
wattSensor = 0; //reset the watts count
SREG = oldSREG; // restore interrupts
totalWatts = totalWatts + wattSensorCount; //total watts counter
LogFile.print(','); // print comma to seprate from previous data
LogFile.print(wattSensorCount);
LogFile.print(',');
LogFile.print(totalWatts);
}
// routine handle file errors
void error(int err)
{
// blink forever
while(1)
{
digitalWrite(fileLedPin, HIGH);
delay(err * 200);
digitalWrite(fileLedPin, LOW);
delay(200);
}
}
void printDateTime(time_t t)
{
printDigits(hour(t),':' );
printDigits(minute(t),':' );
printDigits(second(t),',' ); // uncomment this to print seconds (and replace the comma above with colon)
// print a space here if you need it
printDigits(day(t),'//' );
printDigits(month(t),'//' );
// LogFile.print(year(t)); // prints year using 4 digits
LogFile.print(year(t)-2000); // prints year using 2 digits
LogFile.print(',');
}
void printDigits(int digits, char seperator)
{
// utility function time with leading 0
if(digits < 10)
LogFile.print('0');
LogFile.print(digits);
LogFile.print(seperator);
}
void interruptHandler() // routine called when external interrupt is triggered
{
wattSensor = wattSensor + 1; //Update number of pulses, 1 pulse = 1 watt
}