Problem saving ADXL345 data using SD card shield v3.1

Hi all,
I want to save ADXL345 (sparkfun’s 6 DOF IMU product: ) data to SD card using the seeedstudio SD card shield v3.1. I am using the latest SdFat library from http://code.google.com/p/sdfatlib/.
A push button (connected to digital pin 2 and ground) is used to start and stop writing to SD Card. I connect the ADXL345 through the SD card shield as:
ADXL345-----SD card shield
3.3 V--------3.3 V
SDA---------Analog In 4
SCL---------Analog In 5
GND---------GND

Here is the code I use:

[code]#include <SdFat.h>
#include <Wire.h>
#include <ADXL345.h>

SdFat sd;
SdFile myFile;

ADXL345 adxl;

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 10; //CS pin for the SD Card module

const int rangeSetting = 2; // range setting for accelerometer = ± 2 G
const double rateSetting = 100; // rate setting for accelerometer = 100 Hz
int x,y,z; // variables hold accelerometer datas

const int button = 2; // button is connectod digital pin 2 and ground
int buttonVal = HIGH;
int lastButtonVal;
int i = 1;

void setup() {
Serial.begin(57600);
while(!Serial) {
}

adxl.powerOn();
adxl.setRangeSetting(rangeSetting);
adxl.setRate(rateSetting);

pinMode(button, INPUT_PULLUP);
delay(1);

Serial.println(“Push the button to start”);
while(buttonVal == HIGH) {
buttonVal = digitalRead(button);
//Serial.println(".");
//delay(1);
}

// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

// open the file for write at end like the Native SD library
if (!myFile.open(“Tulis1.txt”, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt(“opening test.txt for write failed”);
}

// if the file opened okay, write to it:
Serial.println(“Start”);

//tanda buat awal file
myFile.println(“8/4/2013”);
}

void loop() {

adxl.readAccel(&x, &y, &z);

myFile.print(i);
myFile.print("\t");
myFile.println(x);
i++;

cekButton();
}

void cekButton() {
buttonVal = digitalRead(button);
//Serial.print("buttonVal: ");
//Serial.println(buttonVal);

if(buttonVal != lastButtonVal) {
if(buttonVal == LOW) {
Serial.println(“Stop”);
myFile.close();
while(1) {
}
}
}

lastButtonVal = buttonVal;
}[/code]

However, there has nothing written on the SD card. Even the “Push the button to start” command didn’t show up on the serial monitor. Is there something wrong with the SD card shield or I have some problems on my code?

Thanks for any suggestions

Fikri

I tried to use the SD card module and push button without the ADXL345 and it worked to save all the data until I push the button. I used this code:

[code]#include <SdFat.h>
SdFat sd;
SdFile myFile;

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 10;

const int button = 2;
int buttonVal = HIGH;
int lastButtonVal;
int i = 1;

void setup() {
Serial.begin(57600);
while(!Serial) {
}

pinMode(button, INPUT_PULLUP);
delay(1);

//Serial.println(“Type any chracter to start”);
//while(Serial.read() <= 0) {}
//delay(1);

Serial.println(“Tekan tombol untuk mulai”);
while(buttonVal == HIGH) {
buttonVal = digitalRead(button);
//Serial.println(".");
//delay(1);
}

// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

// open the file for write at end like the Native SD library
if (!myFile.open(“Tulis1.txt”, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt(“opening test.txt for write failed”);
}

// if the file opened okay, write to it:
Serial.println(“Mulai”);

//tanda buat awal file
myFile.println(“31/3/2013”);
}

void loop() {
/*
for(int x = 0; x < 15; x++) {
myFile.print(x);
myFile.print("\t");
myFile.print(“tes”);
myFile.println(x);
//delay(10);
}
*/

myFile.print(i);
myFile.print("\t");
myFile.print(“tes”);
myFile.println(i);
i++;

cekButton();
}

void cekButton() {
buttonVal = digitalRead(button);
//Serial.print("buttonVal: ");
//Serial.println(buttonVal);

if(buttonVal != lastButtonVal) {
if(buttonVal == LOW) {
Serial.println(“Stop”);
myFile.close();
while(1) {
}
}
}

lastButtonVal = buttonVal;
}[/code]

I’ve also tried to read the Accelerometer data without using the SD card shield and it worked fine. It gave the X,Y,Z data on serial monitor. Here is the code I used to test it:

[code]#include <Wire.h>
#include <ADXL345.h>

ADXL345 adxl;

const int rangeSetting = 2; //ADXL345 data range setting ± 2 G
const double rateSetting = 100; //ADXL345 rate setting 100 Hz

int x,y,z; // variables to hold ADXL345 data

const int button = 2;
int buttonVal = HIGH;
int lastButtonVal;

void setup() {
Serial.begin(57600);
adxl.powerOn();

adxl.setRangeSetting(rangeSetting);
adxl.setRate(rateSetting);

pinMode(button, INPUT_PULLUP);
delay(1);

Serial.println(“Push the button to start”);
while(buttonVal == HIGH) {
buttonVal = digitalRead(button);
//Serial.println(buttonVal);
delay(1);
}
Serial.println(“Start!”);
delay(10);
}

void loop() {
adxl.readAccel(&x, &y, &z);
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);

cekButton();

delay(10);
}

void cekButton() {
buttonVal = digitalRead(button);
//Serial.print("buttonVal: ");
//Serial.println(buttonVal);

if(buttonVal != lastButtonVal) {
if(buttonVal == LOW) {
Serial.println(“Stop”);
//myFile.close();
while(1) {
}
}
}

lastButtonVal = buttonVal;
}[/code]

However, when I hook up the ADXL345 to the SD card, the problem exist. I think there is problem with the I2C line here, something like between the I2C line of ADXl345 and the SD card shield are conflicting each other? Any help will be really appreciated.