Hi,
I would like some help with stabilizing / filtering the data of the 3 axis, from the ADXL335 accelerometer.
What I want:
I am trying to print out the displacement values of the 3 axis. The displacement data will be used in another software to produce sound.
Issue:
When the accelerometer is static, all of the 3 output values fluctuate between 0 and 3-4. How can I make the output values (Xval, Yval, Zval) more stable (stay closer to zero) when it's at a station point (when the accelerometer is not moving)?
What I've done:
As seen in the code below, I have tried getting the "displacement" value (how much the accelerometer has moved - comparing to the last reading and the current reading, subtracting the former from the latter). I have learned that there is a method called "double integration", but I had a hard time understanding it, and I came up with this method of just calculating the difference in previous & current readings. There might be critical mistakes - I'd really appreciate your corrections/suggestions.
The set-up:
The accelerometer is hanging down loose on a string from the ceiling, and it's meant to swing randomly in mid-air (in all 3 axis directions) from 10cm to around 1m (imagine something like a pinata) - by human hand interaction.
Thanks for reading.
My code:
const int ap1 = A5;
const int ap2 = A4;
const int ap3 = A3;
const int numReadings = 30;
int inByte = 0; // incoming serial byte
int readingsX[numReadings]; // the readings from the analog input
int readingsY[numReadings]; // the readings from the analog input
int readingsZ[numReadings]; // the readings from the analog input
int readIndexX = 0; // the index of the current reading
int readIndexY = 0; // the index of the current reading
int readIndexZ = 0; // the index of the current reading
int totalX = 0; // the running total
int totalY = 0; // the running total
int totalZ = 0; // the running total
int averageX = 0; // the average
int averageY = 0; // the average
int averageZ = 0; // the average
int lastX = 0;
int lastY = 0;
int lastZ = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readingsX[thisReading] = 0;
readingsY[thisReading] = 0;
readingsZ[thisReading] = 0;
}
}
void loop() {
int Xval = getXval();
int Yval = getYval();
int Zval = getZval();
Serial.print(Xval);
Serial.print(",");
Serial.print(Yval);
Serial.print(",");
Serial.print(Zval);
Serial.print("\n");
delay(2);
} //------End of Loop-----
int getXval(){
// X------------------------------
// subtract the last reading:
totalX = totalX - readingsX[readIndexX];
//read from the sensor:
int x = abs(analogRead(ap1));
int xMapped = map(x,0,10,0,1000);
readingsX[readIndexX] = xMapped;
// add the reading to the total:
totalX = totalX + readingsX[readIndexX];
// advance to the next position in the array:
readIndexX = readIndexX + 1;
// if we're at the end of the array...
if (readIndexX >= numReadings) {
// ...wrap around to the beginning:
readIndexX = 0;
}
// calculate the average:
averageX = totalX / numReadings;
int accelerationX = abs(averageX - lastX);
lastX = averageX;
return accelerationX;
}
int getYval(){
// Y------------------------------
// subtract the last reading:
totalY = totalY- readingsY[readIndexY];
//read from the sensor:
int y = abs(analogRead(ap2));
int yMapped = map(y,0,10,0,1000);
readingsY[readIndexY] = yMapped;
// add the reading to the total:
totalY = totalY + readingsY[readIndexY];
// advance to the next position in the array:
readIndexY = readIndexY + 1;
// if we're at the end of the array...
if (readIndexY >= numReadings) {
// ...wrap around to the beginning:
readIndexY = 0;
}
// calculate the average:
averageY = totalY / numReadings;
int accelerationY = abs(averageY - lastY);
lastY = averageY;
return accelerationY;
}
int getZval(){
// Z------------------------------
// subtract the last reading:
totalZ = totalZ - readingsZ[readIndexZ];
//read from the sensor:
int z = abs(analogRead(ap3));
int zMapped = map(z,0,10,0,1000);
readingsZ[readIndexZ] = zMapped;
// add the reading to the total:
totalZ = totalZ + readingsZ[readIndexZ];
// advance to the next position in the array:
readIndexZ = readIndexZ + 1;
// if we're at the end of the array...
if (readIndexZ >= numReadings) {
// ...wrap around to the beginning:
readIndexZ = 0;
}
// calculate the average:
averageZ = totalZ / numReadings;
int accelerationZ = abs(averageZ - lastZ);
lastZ = averageZ;
return accelerationZ;
}