CSV Logging File Column Headers

Hi There,

I am currently using this sketch to record and log the temperature of 1 sensor attached to my uno with the Adafruit Logger shield.

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

#define ECHO_TO_SERIAL 1 

RTC_DS1307 RTC;

void setup() {
	Serial.begin(57600);
	Wire.begin();
	RTC.begin();

	if (!RTC.isrunning()) {
		Serial.println("RTC is NOT running!");
	}

	pinMode(10, OUTPUT);
	SD.begin(4);

}

File myFile;
float value;
float time;

void loop() {
	myFile = SD.open("test.csv", FILE_WRITE);
	DateTime now = RTC.now();

	if (myFile) {
		value = analogRead(0) * 5 / 1024.0;
		value = value - 0.5;
		value = value / 0.01;
		myFile.print(now.day(), DEC);
		myFile.print('/');
		myFile.print(now.month(), DEC);
		myFile.print('/');
		myFile.print(now.year(), DEC);
		myFile.print(' ');
		myFile.print(now.hour(), DEC);
		myFile.print(':');
		myFile.print(now.minute(), DEC);
		myFile.print(':');
		myFile.print(now.second(), DEC);
		myFile.print(",");
		myFile.println(value);
		Serial.print(now.day(), DEC);
		Serial.print('/');
		Serial.print(now.month(), DEC);
		Serial.print('/');
		Serial.print(now.year(), DEC);
		Serial.print(' ');
		Serial.print(now.hour(), DEC);
		Serial.print(':');
		Serial.print(now.minute(), DEC);
		Serial.print(':');
		Serial.print(now.second(), DEC);
		Serial.print(" - ");
		Serial.println(value);

	}
	myFile.close();
	delay(1000);
}

when I open the data in Excel it has two nice columns; the first filled with the date and time, the second filled with the temperature readings. What would i add to the sketch to get a column heading at the top of each column when I open the CSV file in Excel???

Thanks

Stevie-G:
Hi There,

I am currently using this sketch to record and log the temperature of 1 sensor attached to my uno with the Adafruit Logger shield.

#include <SD.h>

#include <Wire.h>
#include "RTClib.h"

#define ECHO_TO_SERIAL 1

RTC_DS1307 RTC;

void setup() {
Serial.begin(57600);
Wire.begin();
RTC.begin();

if (!RTC.isrunning()) {
Serial.println("RTC is NOT running!");
}

pinMode(10, OUTPUT);
SD.begin(4);

}

File myFile;
float value;
float time;

void loop() {
myFile = SD.open("test.csv", FILE_WRITE);
DateTime now = RTC.now();

if (myFile) {
value = analogRead(0) * 5 / 1024.0;
value = value - 0.5;
value = value / 0.01;
myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(' ');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
myFile.println(value);
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(" - ");
Serial.println(value);

}
myFile.close();
delay(1000);
}



when I open the data in Excel it has two nice columns; the first filled with the date and time, the second filled with the temperature readings. What would i add to the sketch to get a column heading at the top of each column when I open the CSV file in Excel???

Thanks

Simple,
CSV files assume the data is separated by commas between columns, and records are separated by line feeds between records. From Excel what is the difference between a header and data? (nothing) they are both records of data.

Just write your headers as the first record.

myFile.println("Column 1 header,Column 2 header");

Chuck.

p.s. Remember, if you want a comma in your header, kinda like this:

Sample Date,Time Value

you would need to put quotes around the comma'd heading. Since quotes identify the println() string, you have to escape them(i.e. " ).

myFile.println(""Sample Date, Time",Value");