CSV Column Headings

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

When the file is created, simply write something like

myFile.print("ColA, ColB");

The best is probably to check if the file exists before writing anything. If the file does not exists, open it and use the above line; if it does, don't use it.

I don't have an SD reader so can't test.

Heading as in, a name? What about you just print them to the file?

Would have been easy if you would not have opened and closed the file all the time...

sterretje:
When the file is created, simply write something like

myFile.print("ColA, ColB");

The best is probably to check if the file exists before writing anything. If the file does not exists, open it and use the above line; if it does, don't use it.

I don't have an SD reader so can't test.

I have tried this but it repeats the headings every time new data is added to the csv file. I think this is because its part of the loop but I'm very new to coding so may be wrong.

septillion:
Heading as in, a name? What about you just print them to the file?

Would have been easy if you would not have opened and closed the file all the time...

And yes the reason it opens and closes so much in my thinking is to prevent data loss encase of power failure.

think this is because its part of the loop but I'm very new to coding so may be wrong.

So either do it in setup(), or in loop() flag that a heading has been written and don't do it again if the flag is set.

UKHeliBob:
So either do it in setup(), or in loop() flag that a heading has been written and don't do it again if the flag is set.

Real newbie question. How would i do that?

#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);
  
  if(openFile()){
    myFile.print("ColA, ColB");
  }
  myFile.close();

}

File myFile;
//float value; //no need for global
//float time; never used

void loop() {  
	DateTime now = RTC.now();

	if (openFile()) {
		float 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);
    
    //Why do everything twice????????
		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);
}

File& openFile(){
  myFile = SD.open("test.csv", FILE_WRITE);
  return myFile;
}

PS, start giving your pins names. Now it's unclear what pin10 of A0 do.

septillion:

#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);

if(openFile()){
    myFile.print("ColA, ColB");
  }
  myFile.close();

}

File myFile;
//float value; //no need for global
//float time; never used

void loop() { 
DateTime now = RTC.now();

if (openFile()) {
	float 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);

//Why do everything twice????????
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);

}

File& openFile(){
  myFile = SD.open("test.csv", FILE_WRITE);
  return myFile;
}




PS, start giving your pins names. Now it's unclear what pin10 of A0 do.

Okay, thank you I will give that a go!

Stevie-G:
I have tried this but it repeats the headings every time new data is added to the csv file. I think this is because its part of the loop but I'm very new to coding so may be wrong.

As I said, do it when the file is created. That is not the same as when the file is written :wink:

I also stated that you should do that when the file does not exist :wink: