Hi,
We are trying to use a SD-card to store data from an ADXL335 accelerometer. We want to control the sample rate (fs) of the accelerometer, and the runtime, as this determains how many values we obtain. We need to have a precise sample rate for our project, hence why we are asking for help. We are having problems with the fs, as it seems to work with about 30 Hz, but we want it to be at least 100 Hz. We tried creating the script so that it stored all the data in 3 arrays in a for-loop, before converting it to the right values and then storeing it on the SD-card. We would appreciate if someone would take the time to look through our script, and give some advice to how to improve it, and hopefully solve our problems. We are not that experienced with programming, and especially not c++. We have linked the code. Thanks in advance ![]()
#include <SPI.h>
#include <SD.h>
const int xpin = A1;
const int ypin = A2;
const int zpin = A3;
File myFile;
//String fileName;
////////////////////////////////////////////////////////
// Input values
const String fileName = "File.txt";
const int runtime = 10;
const int fs = 100;
const int n = runtime * fs;
const float meanX = 1.0784; //These values are constant
const float meanY = 1.0705;
const float meanZ = 1.3122;
int xRaw[n], yRaw[n], zRaw[n]; //Creates 3 empty arrays for the data
//////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial){
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("Initialization failed!");
while (1);
}
Serial.println("Initialization done.");
//
delay(2000);
Serial.println("Data collecting starting now");
for (int a = 0; a < n; a++) {
xRaw[a] = analogRead(xpin); //Collects the data in the arrays
yRaw[a] = analogRead(ypin);
zRaw[a] = analogRead(zpin);
Serial.print("Iteration "); Serial.print(a); Serial.println(" done");
delay(1000/fs); //The delay/time between each measurement
}
Serial.println("Data collection finished"); //Prints to serial monitor that collection of data is done
//delay(1000);
float xVal[n], yVal[n], zVal[n]; //Creates 3 empty arrays for the float values
for (int a = 0; a < n; a++) {
xVal[a] = (((xRaw[a] * (3.3/1023.0)) - meanX) / 0.3) * 9.82;
yVal[a] = (((yRaw[a] * (3.3/1023.0)) - meanY) / 0.3) * 9.82;
zVal[a] = (((zRaw[a] * (3.3/1023.0)) - meanZ) / 0.3) * 9.82;
delay(50);
}
Serial.println("Data conversion complete"); //Prints to serial monitor that conversion of data is done
delay(2000);
myFile = SD.open(fileName, FILE_WRITE); //Creates and opens the file
if (myFile) {
for (int a = 0; a < n; a++) {
myFile.print(xVal[a]);
myFile.print(", ");
myFile.print(yVal[a]);
myFile.print(", ");
myFile.println(zVal[a]);
delay(50);
}
Serial.println("Logging sensor data to SD card finished."); //Prints to the serial monitor that the measurements is done
} else {
Serial.println("Error opening file");
}
myFile.close(); //Closes the file (This is important to do)
}
void loop() {
//Remains empty
}