Data Logger - How to set the date & time stamp of the log file

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Data Logger - How to set the date & time stamp of the log file

Postby mmathur » Thu Jul 14, 2011 8:43 am

I'm using the Data Logger kit and want to have the correct date and time stamp associated to each of the log files that get created by the Data Logger.

Can you provide any help/direction on how I can set the date and time stamp of the log file when a new file is created? Thank you.
mmathur
 
Posts: 7
Joined: Sun Jun 19, 2011 8:52 pm

Re: Data Logger - How to set the date & time stamp of the log file

Postby fat16lib » Thu Jul 14, 2011 9:51 am

Download the latest SdFat here http://code.google.com/p/sdfatlib/downloads/list.

Look at the AnalogLogger.pde example in SdFat/examples. It sets file timestamps.

You should be able to use the same method for your sketch.

SD.h is a wrapper for an old version of SdFat so it may take some mods to the wrapper if you want to use SD.h
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm

Re: Data Logger - How to set the date & time stamp of the log file

Postby mmathur » Thu Jul 14, 2011 1:09 pm

Thanks, I got it working - the log files have the correct date and time stamp.

Here is the code that I used to simply create a file to ensure files get created with the correct date & time stamp:

Code: Select all
#include <SdFat.h>
#include <SdFatUtil.h>  // define FreeRam()
#include <Wire.h>
#include "RTClib.h"

// Serial print stream
ArduinoOutStream cout(Serial);

RTC_DS1307 RTC; // define the Real Time Clock object
//------------------------------------------------------------------------------
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
    DateTime now = RTC.now();

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}
//------------------------------------------------------------------------------
// format date/time
ostream& operator << (ostream& os, DateTime& dt) {
  os << dt.year() << '/' << int(dt.month()) << '/' << int(dt.day()) << ',';
  os << int(dt.hour()) << ':' << setfill('0') << setw(2) << int(dt.minute());
  os << ':' << setw(2) << int(dt.second()) << setfill(' ');
  return os;
}

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
ofstream logfile;

//New variable for the SD card
SdFat sd;

// buffer to format data - makes it eaiser to echo to Serial
char buf[80];

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
 
  // red LED indicates error
  //digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void) {
  Serial.begin(9600);
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
    // pstr stores strings in flash to save RAM
  cout << endl << pstr("FreeRam: ") << FreeRam() << endl;
 
    // format header in buffer
  obufstream bout(buf, sizeof(buf));

  bout << pstr("millis");

  // connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    bout << pstr("RTC failed");
    Serial.println("RTC failed");
  }

  SdFile::dateTimeCallback(dateTime);
  DateTime now = RTC.now();
  cout  << now << endl;
 
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt(); 
 
  //Get the current time from the Real Time Clock (RTC);
  now = RTC.now();   
 
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (sd.exists(filename)) continue;
    logfile.open(filename);
    break;   
  }
 
  if (!logfile.is_open()) {
    Serial.println("Could not create the file!");
    error("couldnt create file");
  }
 
  Serial.print("Logging to: ");
  Serial.println(filename);

  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}

void loop(void) {

}
mmathur
 
Posts: 7
Joined: Sun Jun 19, 2011 8:52 pm

Re: Data Logger - How to set the date & time stamp of the log file

Postby fat16lib » Thu Jul 14, 2011 1:34 pm

Looks fine.

One comment about an incantation that is in many programs that use SdFat.

You don't ever need this statement with SdFat:
Code: Select all
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

SdFat will do the right thing. What ever the processor is, SS will be set to output mode. This is pin 10 on a 328, pin 53 on a Mega, pin 4 on a Sanguino...
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm

Re: Data Logger - How to set the date & time stamp of the log file

Postby adafruit » Fri Jul 15, 2011 11:52 am

fat16, agreed. we left that line in because we wanted to remind users about that pin being an output -even if it isnt used- because if its set to an input it breaks everything and people get really confused :(
User avatar
adafruit
 
Posts: 10489
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: Data Logger - How to set the date & time stamp of the log file

Postby fat16lib » Fri Jul 15, 2011 12:44 pm

Explaining which pins are used by a Shield is fine but adding code that is unneeded or wrong in the case of the Mega doesn't seem like a good idea.

Setting pin 10 to output mode causes more problems than it solves. On a Mega it is wrong and if a wire is attached from pin 10 to pin 53, it won't work.

Some people think they should worry about other pins when they see this and do more strange things.

Some think they need to handle chip select.

There is no problem on Adafruit shields, pin 10 is chip select. The Arduino Ethernet shield is fine since 10 is Enet chip select and SdFat lives well with it.

The SparkFun Shield could be a problem since pin 8 is chip select. But they warn users about pin 8, 10.

Since MOSI, MISO, and SCK can be messed up why not set their mode?
fat16lib
 
Posts: 586
Joined: Wed Dec 24, 2008 12:54 pm


Return to General Project help

Who is online

Users browsing this forum: No registered users and 9 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [102]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]