I have a lib that controls the card. This is the code in the setup:
#include <FileControl.h>
#include <RTC.h>
#include <XBee.h>
#include <SD.h>
#include <Wire.h>//I2C header file
#include <SoftwareSerial.h>
// Xbee
XBee xbee = XBee();
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();
XBeeAddress64 xbeeID = XBeeAddress64();
int numMods = 8; // number of Modules
int Values[8]; // array to hold pyranometer data
uint32_t Names[8] = {1084176782, 1084176774, 1084176750, 1084176731, 1084176749, 1084176970, 1084176956, 1084177011}; // array to hold Xbee serial numbers
int uploadDay = 4;
int delayTime = 0; // used to calculate time to next iteration
long now = 0;
int startHr = 5;
int endHr = 20;
unsigned long sendDuration = 1800000;
unsigned long cupReads = 0;
unsigned int cupPeak = 0;
unsigned int cup = 0;
float cupms = 0;
int cupPin = 15;
int TimeInfo[7];
RTC rtc;
FileControl FC;
String Data = "";
int reads = 0;
String LogFilename = "";
String DataFilename = "";
void setup()
{
analogReference(INTERNAL2V56);
delay(2000); // let shields initialize
//Serial.begin(19200);
//Serial.println("Starting...");
// uncomment to set the time on the RTC
// TimeInfo[0] = 15; // seconds
// TimeInfo[1] = 25; // minutes
// TimeInfo[2] = 9; // 24 hour
// TimeInfo[3] = 4; // 0-6 -> sun-sat
// TimeInfo[4] = 13; // day
// TimeInfo[5] = 6; // month
// TimeInfo[6] = 13; // year
// rtc.SetTime(TimeInfo);
//xbee.begin(9600); // initialize Xbee
while(!FC.IsInitialized())
{
//Serial.println("Initializing SD...");
rtc.GetTime(TimeInfo);
FC.InitializeSD(2, TimeInfo);
}
rtc.GetTime(TimeInfo);
DataFilename = "Pyr" + String(TimeInfo[5]) + "-" + String(TimeInfo[4]) + ".txt";
LogFilename = "Log" + String(TimeInfo[5]) + "-" + String(TimeInfo[4]) + ".txt";
//Serial.println("Starting...");
}
Here is the init procedure in FileControl.cpp
boolean FileControl::InitSD(int TimeInfo[7])
{
bool DFresult = false;
SD.begin(4); // init SD card
delay(2000);
File okfile = SD.open("okfile.txt", FILE_WRITE);
boolean CardOk = (boolean)okfile;
// write start time
okfile.print("Started at: ");
okfile.print(TimeInfo[5]);
okfile.print("/");
okfile.print(TimeInfo[4]);
okfile.print("/");
okfile.print(TimeInfo[6]);
okfile.print("-");
okfile.print(TimeInfo[2]);
okfile.print(":");
okfile.print(TimeInfo[1]);
okfile.print(":");
okfile.println(TimeInfo[0]);
okfile.close();
// check if file exists, if not create one with a header
if(!SD.exists("PyrData.txt"))
{
File DataFile = SD.open("PyrData.txt", FILE_WRITE);
DataFile.println("Timestamp,millis,1084176782, 1084176774, 1084176750, 1084176731, 1084176749, 1084176970, 1084176956, 1084177011");
DataFile.close();
}
return CardOk;
}
It will only initialize the card when the shield is set to usb, after that I can switch it to micro and communicate with the xbees and write the data.
Thanks for your help.