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