Pages: [1]   Go Down
Author Topic: Problem saving ADXL345 data to SD Card  (Read 1032 times)
0 Members and 1 Guest are viewing this topic.
Depok, Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I want to save data from ADXL345 to SD Card using this module http://www.seeedstudio.com/wiki/SD_Card_Shield_V3.0 and the latest SdFat libraries 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. However, the program didn't run as I expected because even the "Push the button to start" command didn't show up on the serial monitor.

Here is the code I used:
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("Type any chracter to start");
  //while(Serial.read() <= 0) {}
  //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() {
  /*
  for(int x = 0; x < 15; x++) {
   myFile.print(x);
   myFile.print("\t");
   myFile.print("tes");
   myFile.println(x);
   //delay(10);
   }
   */

  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;
}

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;
}


So, why the SD card couldn't save any data when use with the ADXL345? Is there any problem with my code?

Thanks

Fino
« Last Edit: April 09, 2013, 12:05:18 am by fino1990 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 611
Posts: 49097
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is your ADXL345 connected to the Arduino?

Without trying to use the SD card, can you read data from the accelerometer?
Logged

Depok, Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh I forgot to mention that I am using Sparkfun IMU 6 DOF (https://www.sparkfun.com/products/10121) and the connection between Arduino and ADXL345:
Arduino             ADXL345
3.3 V                 VCC
Analog 4             SDA
Analog 5             SCL
GND                  GND

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;
}

However, when I connected the ADXL345 through that SD card shield using the same code above, there wasn't any data at the serial monitor.
« Last Edit: April 09, 2013, 10:27:04 am by fino1990 » Logged

Pages: [1]   Go Up
Jump to: