Hi,
I have a SD data logger (based on the data logger in Adafruit) which is sending data to the serial port (using echo to serial) but it not actually logging anything into the SD card. The key issue is that the data on serial port is correct, including data, RTC timestamp, etc. But while the SD card CSV files have been created and correctly named, they have no data in them and the timestamp is 01/01/2000 @ 00:00. Here is a snippet of the serial port data;
Initializing SD card...card initialized.
Logging to: LOGGER01.CSV
millis,time,Light,Heat,angle
999, 2013/4/25 10:37:1, 2297.00, 436, -0.01
1998, 2013/4/25 10:37:2, 1847.00, 377, -0.02
3000, 2013/4/25 10:37:3, 1700.00, 347, -0.02
etc.
I have formatted the SD card both as FAT and also FAT32 (in case it made a difference). I am using Arduino Uno S/W version 1.0.3. Here is the code I am using;
// INCLUDE THE LIBRARIES
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
// DEFINE PARAMETERS
#define LOG_INTERVAL 1000 // mills between entries
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// DEFINE THE DIGITAL PINS THAT ARE CONNECTED TO THE LEDs
#define redLEDpin 3
#define greenLEDpin 4
// DEFINE THE ANALOG PINS THAT ARE CONNECTED TO THE STYLUS SENSORS
#define light_Pin 0 // LDR connected to A0
int light_Reading; // the analog reading from the LDR
float light_Voltage; // used to get value between 0 - 5000mV
#define heat_Pin 1 // Thermistor connected to A1
int heat_Reading; // the analog reading from the temp
float heat_Voltage; // used to get value between 0 - 5000mV
#define angle_Pin 2 // Inclinometer connected to pin A2
int angle_Reading; // the analog reading from the angle
float angle_Voltage; //turned into a voltage between 0 - 5000mV
float angle; // angle in degrees +/-90deg
// REAL TIME CLOCK CODE
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
// SD CARD AND RTC SET UP
void setup(void)
{
Serial.begin(9600);
Serial.println();
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// 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.");
// 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))
{
// only open a new file if it doesn't exist
logfile = SD.open(filename,FILE_WRITE);
break; // leave the loop!
}
}
if (!logfile)
{
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
Wire.begin();
RTC.begin();
if (!RTC.isrunning())
{
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,time,Light,Heat,angle");
#if ECHO_TO_SERIAL
Serial.println("millis,time,Light,Heat,angle");
#endif //ECHO_TO_SERIAL
//#if ECHO_TO_SERIAL// attempt to write out the header to the file
// if (logfile.writeError || !logfile.sync())
// {
// error("write header");
// }
// #endif //ECHO_TO_SERIAL
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
// If want to set the aref to something other than 5v
//analogReference(EXTERNAL);
}
// MAIN CODE LOOP
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH);
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
// logfile.print(now.get()); // seconds since 2000
// logfile.print(", ");
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
// Serial.print(now.get()); // seconds since 2000
// 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);
#endif //ECHO_TO_SERIAL
//GET READINGS FROM ANALOG INPUT PINS
light_Reading = analogRead(light_Pin);
delay(10);
heat_Reading = analogRead(heat_Pin);
delay(10);
angle_Reading = analogRead(angle_Pin);
delay(10);
//CODE FOR Light
light_Voltage = map(light_Reading, 0, 1023, 0, 5000);
//CODE FOR Heat
heat_Voltage = map(heat_Reading, 0, 1023, 0, 5000);
//CODE FOR INCLINOMETER
angle_Voltage = map(angle_Reading, 0, 1023, 0, 5000);
angle = ((angle_Voltage - 2500)/36000); //angle in degrees +/-90deg
// LOG THE DATA TO THE SD CARD FILE
logfile.print(", ");
logfile.print(light_Voltage);
logfile.print(", ");
logfile.print(heat_Reading);
logfile.print(", ");
logfile.println(angle);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(light_Voltage);
Serial.print(", ");
Serial.print(heat_Reading);
Serial.print(", ");
Serial.println(angle);
#endif //ECHO_TO_SERIAL
digitalWrite(greenLEDpin, LOW);
}
Any help, advice, guidance or suggestions are kindly received.
Thanks,
Phil