Not Logging to SD card

hi all,

i have been using the adafruit sd shield to log serial data. I am having trouble getting the data to log onto the SD card. The information is displaying on the serial monitor. I have attached to could with a pointer as to where i think the problem might lie but having trouble getting past it. Any help would be appreciated.

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

// A simple data logger for the Arduino analog pins 
#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()

// the digital pins that connect to the LEDs 
#define redLEDpin 3 
#define greenLEDpin 4


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;

File logfile;  // This is the logging file

// the logging file File logfile;
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);  
  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();    
        if (!RTC.begin()) {    
          logfile.println("RTC failed"); 
          #if ECHO_TO_SERIAL    
          Serial.println("RTC failed"); 
          #endif  //ECHO_TO_SERIAL  } 
           
  logfile.println("Multi sniffer");    
  if ECHO_TO_SERIAL  
  Serial.println("Multi sniffer"); 
  if ECHO_TO_SERIAL // attempt to write out the header to the file  
if logfile.write() {    <------------------Here i think the problem lies
    error("write header");  
    //}    
    pinMode(redLEDpin, OUTPUT);  
    pinMode(greenLEDpin, OUTPUT); 
   // If you want to set the aref to something other than 5v  
   //analogReference(EXTERNAL); }

    }
  }
  

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(", ");  
  logfile.print(now.year(), DEC);
  logfile.write(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(", ");  
  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

  int my_array[9] = {0}; //This has to read the serial input
  int  i;
  
  // display each number from the array in the serial monitor window
  for (int i = 0; i < 9; i++){ 
    Serial.println(my_array[i]);

     digitalWrite(greenLEDpin, LOW);
}
}

Are you sure this is correct?

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

Do you ever see this done?

    Serial.println("card initialized.");

It isn't clear why you aree doing this

File logfile;  // This is the logging file

and this

    char filename[] = "LOGGER00.CSV";

I think you are making the whole deal more convoluted than it needs to be, and the problem happens before you think it might. Once you have firmly settled on a filename and have established that the card is kosher, That's all you need. Then, in the loop, it is something like"

myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(m);
  myFile.print(",");

tralala

  myFile.println(data);
myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE

Thanks for the response.

Well i'm quiet new to this and got this from the adafruit data logger example. in response to some of the code i get

Serial.println("card initialized.");

on the serial monitor as well as

char filename[] = "LOGGER00.CSV";

file being created. I have attached a screen shot of the serial monitor so you can see. As i said the file is being created on the SD card as an excel file but with no content.

OK, the file names are OK. I have doubts about your attempts to change the file name, and the code comment
        // only open a new file if it doesn't exist
reads like nonsense.

The actual print code seems OK. Have you removed the card, put it in a PC, and checked that the filename is what you think it ought to be?

I assume you want to start a new logfile each time you press reset. If that is the case, I believe the thing to do is to store an index on the SD, recover it next time, and then replace it with a new one..

I literally just solved this issue. I found that creating the File object in a seperate function didn't make it properly available in the loop function, despite declaring it as a global variable.

You might consider doing what I did, creating a seperate function that creates and returns the log file. Of course, that's going to create a new log every cycle of the main loop, so you would have to create your own loop to use after calling that function.

I've included my code, albeit poorly documented, for you to take a look at. Feel free to ask any questions!

#include <Adafruit_Sensor.h>
#include <RTClib.h>
#include <Adafruit_MMA8451.h>
#include <SD.h>
#include <Wire.h>

#define LOG_INTERVAL  1000 //mills between entries
#define ECHO_TO_SERIAL  0 //echo logged data to serial monitor. 1 for on, 0 for off.

//onboard LEDs
const int redLED = 7;
const int greenLED = 6;

//switch for inputs
const int switchIn = 2;
const int chipSelect = 10;

//variables
uint32_t logMillis;
RTC_DS1307 RTC;

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  digitalWrite(redLED, HIGH);

  while(1);
}

File newLog() {
  
  logMillis = millis();
  
  char filename[] = "LOG00.TXT";
  for(uint8_t i = 0; i < 100; i++) {
    filename[3] = i/10 + '0';
    filename[4] = i%10 + '0';

    if(! SD.exists(filename)) {
      break;
    }
  }

  File logfile = SD.open(filename, FILE_WRITE);

  if(! logfile) {
    error("couldn't create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);

  DateTime now;
  now = RTC.now();

  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.println(now.second(), DEC);

  #if ECHO_TO_SERIAL
    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.println(now.second(), DEC);
  #endif //ECHO_TO_SERIAL

  logfile.println("millis,speed,locx,locy,locz,accx,accy,accz");
  #if ECHO_TO_SERIAL
    Serial.println("millis,speed,locx,locy,locz,accx,accy,accz");
  #endif //ECHO_TO_SERIAL

  return logfile;
  
}

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  Serial.println();
  
  
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(switchIn, INPUT_PULLUP);
  pinMode(chipSelect, OUTPUT);

  Serial.println("Initializing SD card");

  if(!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }

  Serial.println("card initialized");

  Wire.begin();
  if (!RTC.begin()) {
    Serial.println("RTC failed");
  }
}

void loop() {
  if(digitalRead(switchIn)) {
    digitalWrite(redLED, LOW);
    File logfile = newLog();
    
    while(digitalRead(switchIn)) {
      digitalWrite(greenLED, HIGH);

      uint32_t m = millis();

      logfile.println(m-logMillis);
      #if ECHO_TO_SERIAL
        Serial.println(m-logMillis);
      #endif

      delay(100);
      digitalWrite(greenLED, LOW);
      delay(LOG_INTERVAL - 100);
    }

    logfile.close();
    digitalWrite(redLED, HIGH);
  }
}
[\code]