SD Card Shield configuration on Arduino mega 2560 REV3?

Hi Guys,

I'm having similar issues. I'm using a MEGA ADK and the Arduino WiFi shield to log data to the SD card - not actually using the WiFi function of the shield.

For some reason I can't detect the SD card. I have tried several SD cards and starting to think it is an issue with the MEGA ADK. I have two of these boards and dosnt work on either of them.

The same WiFi shield and SD cards work fine on my DUE board but need it to work on my MEGA. I use the following code.

// libraries used
#include <Debounce.h>
#include <SPI.h>
#include <SD.h>
#include <math.h> // math library not imported by default

/*
THIS IS A HACK setting the ADC prescale to 16 for faster analog read speed.
C function used:
sbi() "clear the bits"
cli() "set the bit"
C variables use in the source code use during complie time
srb "shift register name" sketch variable
bit "address of the register in bits" sketch variable
_SFR_BYTE() "shift register name" dummy variable to access the source code
_BV() "address of the register in bits" dummy variable to access the source code
*/
#ifndef cbi
#define cbi(sfr,bits) (_SFR_BYTE(sfr) &= ~_BV(bits))
#endif
#ifndef sbi
#define sbi(sfr,bits) (_SFR_BYTE(sfr) |= _BV(bits))
#endif

// constant variables before compile
#define stopSwitch 48 // stop switch connected to pin 48

// create instances of the classes included in the libraries
Debounce debouncer1=Debounce(20,stopSwitch);

// declare global constants and their data types
const float deltaVolts=5.0/pow(2,10); // 5.0 volts input divided by the 2^10 (MEGA) or 2^12 (DUE) ADC i.e. 12 bits of resolution

// declare global variables and their data types
int sensorValue;
int id=1;
float volts;

// declare files
File dataFile;

// setup code here, to run once
void setup() 
{
  //analogReference(DEFAULT);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  Serial.begin(115200); // initialize serial communication
  while(!Serial)
  {
  }
  Serial.print("Initializing SD card..."); // print to the serial monitor a message
  pinMode(53,OUTPUT); // pin 53 must be left as an OUTPUT for the SD library to work
  pinMode(10,OUTPUT); // pin 10 must be left as an OUTPUT so it can be set HIGH
  digitalWrite(10,HIGH); // pin 10 needs to be put HIGH to explicitly deselect the WiFi HDG104 chip
  pinMode(stopSwitch,INPUT);
  digitalWrite(stopSwitch,LOW);
  if(!SD.begin(4)) // Chip Select pin 4 default channel on WiFi sheild for SD communication. If the statement !SD.begin(SS) comes back as FALSE execute the "if" statement
  {
    Serial.println("Card failed or not present"); // print to screen the SD card status
    return; // start the loop again and check if the SD is faulty or not installed
  }
  else
  {
    Serial.println("Card initialized"); // else everything is all good
  }
  if(SD.exists("volts.csv"))
  {
    Serial.println("File exists and now being removed");
    SD.remove("volts.csv");
  }
  else
  {
    Serial.println("No file could be found");
  }
  Serial.print("Sample No.");
  Serial.print(",");
  Serial.println("Volts");
  dataFile=SD.open("volts.csv",FILE_WRITE);
  dataFile.print("Sample No.");
  dataFile.print(",");
  dataFile.println("Volts");
  dataFile.close();
  //analogReadResolution(12); // default resolution is 10 bit statment changes to 12 bit for the DUE board but line not use when using the MEGA or low boards
}

// main code
void loop() 
{
  debouncer1.update(); // update the debouncer
  sensorValue=analogRead(A0);
  volts=deltaVolts*sensorValue;
  dataFile=SD.open("volts.csv",FILE_WRITE);
  if(dataFile && debouncer1.read()==LOW)
  {
    dataFile.print(id,DEC);
    dataFile.print(",");
    dataFile.println(volts,DEC);
    dataFile.close();
    Serial.print(id,DEC);
    Serial.print(",");
    Serial.println(volts,DEC);
    float jam=log10(volts);
  }
  else
  {
    dataFile.close();
    return;
  }
  id++;
}

But keeps stopping at

if(!SD.begin(4))

on my ADK.

Any help would be much appreciated.