Trouble Writing to Sd Card

Hey, I’m working on a Rfid time log. I have the Rfid and ds1307 module working but I can’t write the data to the Sd card. Any help would be awesome.
Here’s the code:

void loop()
{
   mySerial.print("!RW");
  mySerial.write(byte(RFID_LEGACY));
  
    if(mySerial.available() > 0) {          // if data available from reader 

    if((val = mySerial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( mySerial.available() > 0) { 
          val = mySerial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 

      if(bytesread == 10) {              // if 10 digit read is complete 
         
             Serial.println(code);  
         
         if(strcmp(code,tag1) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}   
         }
  
      if(strcmp(code,tag2) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}  
         }
  
      if(strcmp(code,tag3) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
} 
         }
    
        if(strcmp(code,tag4) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}
         }
         
       if(strcmp(code,tag5) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
} 
     }

    if(strcmp(code,tag6) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}
         }
         
         
         else {Serial.println("try Again");}
         
      }
    }
    }
}

That's only some of the code.

I can't write the data to the Sd card.

What happens when you try ?

Have you tried just getting the SD to work using any of the SD example sketches?

Note that the SD card must be pre-formatted to work. I put text files on mine on the PC just to have known data to read when I tested.

Getting mine to work took about 5 formats. Go to the adafruit site and search for their data logger. On the instructions they mentioned a formatter utility that worked for me.

I use FAT-16 which is the most compatible but at least one library will use FAT-32 as well.

The thing is that you are able to read and write to SD when it's just the SD program?

This is going into troubleshooting, finding out what you are and are not sure of.

Sorry, My code exceeded the character limit.
Here’s the first part:

#include <SoftwareSerial.h>
#define txPin 6
#define rxPin 8          
#define RFID_LEGACY 0x0F  


SoftwareSerial mySerial(rxPin, txPin);
int  val = 0; 
char code[12];     
int bytesread = 0;  //Rfid Code

char tag1[12] = {'0', '4', '0', '0', '2', '2', '7', '8', '0', '9'};   //the tags id
char tag2[12] = {'0', '4', '0', '0', '2', '8', 'C', 'A', '4', '6'};
char tag3[12] = {'0', '4', '0', '0', '2', '2', '9', '7', '6' ,'F'};
char tag4[12] = {'0', '4', '0', '0', '2', '8', 'C', 'B', '2', '8'};
char tag5[12] = {'0', '4', '0', '0', '2', '8', 'E', '0', '2', '9'};
char tag6[12] = {'0', '4', '0', '0', '2', '8', 'C', '6', 'B', '9'};



#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 

char* dow[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}  // Clock Code





#include <SD.h>

const int chipSelect = 10;// SD shield Code


void setup()
{
  Serial.begin(9600);
  Wire.begin();
  pinMode(10, OUTPUT);
  mySerial.begin(9600);

  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(txPin, OUTPUT);     //pin 6
  pinMode(rxPin, INPUT);      //pin 8 Rfid Code
  Serial.println("Ready");
  
    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.");
}

And the second:

void loop()
{
   mySerial.print("!RW");
  mySerial.write(byte(RFID_LEGACY));
  
    if(mySerial.available() > 0) {          // if data available from reader 

    if((val = mySerial.read()) == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( mySerial.available() > 0) { 
          val = mySerial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                       // stop reading 
          } 
          code[bytesread] = val;         // add the digit           
          bytesread++;                   // ready to read next digit  
        } 
      } 

      if(bytesread == 10) {              // if 10 digit read is complete 
         
             Serial.println(code);  
         
         if(strcmp(code,tag1) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}   
         }
  
      if(strcmp(code,tag2) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}  
         }
  
      if(strcmp(code,tag3) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
} 
         }
    
        if(strcmp(code,tag4) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}
         }
         
       if(strcmp(code,tag5) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
} 
     }

    if(strcmp(code,tag6) == 0)
         {    // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  int second     = bcdToDec(Wire.read() & 0x7f);
  int minute     = bcdToDec(Wire.read());
  int hour       = bcdToDec(Wire.read() & 0x3f);  // Need to change this if 12 hour am/pm
  int dayOfWeek  = bcdToDec(Wire.read());
  int dayOfMonth = bcdToDec(Wire.read());
   int month      = bcdToDec(Wire.read());
  int year       = bcdToDec(Wire.read());
           
           File dataFile = SD.open("datalog.txt");

    // if the file is available, write to it:
    if (dataFile) {
       dataFile.println(month);
       dataFile.print("/ ");
       dataFile.print(dayOfMonth);
       dataFile.print("/ ");
       dataFile.print(year);
       dataFile.print(" ");   
       dataFile.print(hour);
       dataFile.print(":");
       dataFile.print(minute);
       dataFile.print(":");
       dataFile.print(second);
       dataFile.print("          ");
       dataFile.print(code);
       dataFile.close();
       Serial.println("Time Stamped");
}
         }
         
         
         else {Serial.println("try Again");}
         
      }
    }
    }
}

Whenever I try it tells me to try again.

I'm using FAT 16 also.

I have seen Adafruits datalogger but i used the built in SD example and tronixstuffs.

The example code worked when first tried it.

I don't see the problem. I don't have the DS hardware to try a run and see what happens.

Can you start adding debug prints to Serial Monitor and see where your code executes and maybe some key values to track with?

I don't know what a debug print is.

It's a way to tell what your code is actually doing besides the regular output.

For example, everywhere you have code that writes to SD, you put in a Serial.print line that says " point X " and prints any variables that help you know HOW it got there and/or where the next if() or other path will go. You start with a little and keep adding until the reasons why it's not doing what you expect become clear. You may discover bad wiring or hardware or bad code or bad data driving good code or a combination of any of those. Just be happy that the positions of the Sun, Moon, Stars and Planets don't play into it!

Debugging is a core programming skill. Printing lines of debug info gives you data to work with. You collect as much data as you need to get a clear picture. There's only so much anyone can do just staring at lines of code because those lines are not everything. Sometimes you're lucky but of you do this much for non-trivial tasks (like yours now), most of the time you have to debug. So lock and load, it's time to go in and get definite.

Thank you all