Hi all,
I want to save data from ADXL345 to SD Card using this module SD Card Shield V3.0 | Seeed Studio Wiki and the latest SdFat libraries from Google Code Archive - Long-term storage for Google Code Project Hosting.. 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:
#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:
#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