Hello, im using the default simple data logger sketch from the default library. Im also using the RTC library and have a data logging sheild with RTc. I am trying to get the arduino to put a time date stamp as well as the readings form several analog sensors that i have attached, and then go to sleep between reading. Everything displays correctly in the serial view but i cant figure out how to write the date/time of each reading to the sd card (all i get are the 3 sensor values separated by comas) The default sketch creates a string out of the sensor readings and then writes that string to the card, im sure there is a simple way to include the time data as well but i am struggling and after much googling was hoping someone here might have an easy answer
My sketch
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
- analog sensors on analog ins 0, 1, and 2
- SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Narcoleptic.h>
#include <RTClib.h>
RTC_PCF8523 RTC; // define the Real Time Clock object
const int chipSelect = 10;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Wire.begin();
RTC.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
// Addition
DateTime now = RTC.now();;
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
}
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
delay (5000);
Narcoleptic.delay (50000);
}