Hi guys
I am using an SD card module and Arduino Uno to store acceleration data from ADXL345 sensor to an SD card. But I have some problems as follows:
-
In my code, I can give a float sample (e.g. 1000) to store only a specific amount of data. It works with 1000, but when I increase it to 100,000, it does not work.
-
When I put a delay at the end of the code, it seems, it does not work. I want to reduce the speed of data storage. For example, if the sampling rate of data storage is 50, I want to reduce it to 10 or 15.
-
My last question is that how I can change my code if I want Arduino to store the unlimited number of acceleration data in the SD card until I take out the battery from Arduino. Could anybody help me? Thank you so much in advance
Below is my code (I also attached the code):
#include <Wire.h> //Call the I2C library built in Arduino
//Set the address of the register
#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
#define Register_Z0 0x36
#define Register_Z1 0x37
//
#define redLEDpin 3
#define greenLEDpin 4
//
int chipSelect = 4; //chipSelect pin for the SD card Reader
File myFile; //Data object you will write your sesnor data to
//
int ADXAddress = 0x53; //I2C address
int reading = 0;
int val = 0;
int X0, X1, X_out;
int Y0, Y1, Y_out;
int Z1, Z0, Z_out;
double Xg, Yg, Zg;
float samples = 100000;//Number of samples
void setup()
{
Serial.begin(9600);//Set the baud rate of serial monitor as 9600bps
delay(100);
Wire.begin(); //Initialize I2C
delay(100);
Wire.beginTransmission(ADXAddress);
Wire.write(Register_2D);
Wire.write(8);
Wire.endTransmission();
Serial.println("\n");
Serial.println("Accelerometer Test ");
// SD Card Initialization
if (SD.begin())
{
Serial.println("SD card is ready to use.");
} else
{
Serial.println("SD card initialization failed");
return;
}
// Create/Open file
myFile = SD.open("test.txt", FILE_WRITE);
while (samples != 0) {
Wire.beginTransmission(ADXAddress);
Wire.write(Register_X0);
Wire.write(Register_X1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress, 2);
if (Wire.available() <= 2);
{
X0 = Wire.read();
X1 = Wire.read();
X1 = X1 << 8;
X_out = X0 + X1;
}
Wire.beginTransmission(ADXAddress);
Wire.write(Register_Y0);
Wire.write(Register_Y1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress, 2);
if (Wire.available() <= 2);
{
Y0 = Wire.read();
Y1 = Wire.read();
Y1 = Y1 << 8;
Y_out = Y0 + Y1;
}
Wire.beginTransmission(ADXAddress);
Wire.write(Register_Z0);
Wire.write(Register_Z1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress, 2);
if (Wire.available() <= 2);
{
Z0 = Wire.read();
Z1 = Wire.read();
Z1 = Z1 << 8;
Z_out = Z0 + Z1;
}
Xg = X_out;// / 256.00; //Convert the output result into the acceleration g, accurate to 2 decimal points.
Yg = Y_out;// / 256.00;
Zg = Z_out;// / 256.00;
if (myFile) {
//Serial.println("Writing to file...");
myFile.print(Xg);
myFile.print("\t");
myFile.print(Yg);
myFile.print("\t");
myFile.println(Zg);
}
else {
Serial.println("error opening test.txt");
}
samples = samples - 1;
}
myFile.close();
Serial.println("Done!!");
pinMode(LED_BUILTIN, OUTPUT);
//delay(100); //Optional
}
// the loop function runs over and over again forever
void loop() {
}
ADXL345_SD.ino (3.08 KB)
ADXL345_SD.ino (3.08 KB)