# Averaging the Data from an Accelerometer

Hi,
Im working on a project that involves an accelerometer. What my project is trying to do is measure the amount of movement someone does based on the data the accelerometer is sending to the serial monitor. The data comes in as a value for the X axis, Y axis, and Z axis. Is there someway to maybe average this data to produce some sort of way to tell how much a person is moving, for example, little to no movement, moderate movement, and constant movement.

Thanks so much. Ill include the code i was using in case it can help.

``````#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;

void setup()
{
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(5); //sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
accelero.calibrate();
}

void loop()
{
x = accelero.getXAccel();
y = accelero.getYAccel();
z = accelero.getZAccel();
Serial.print("\nx: ");
Serial.print(x);
Serial.print(" \ty: ");
Serial.print(y);
Serial.print(" \tz: ");
Serial.print(z);
delay(500);
}
``````

If you really want the average of a reading, take several readings, add each one to a variable that was initialised to zero before starting and divide the total by the number of readings. If you want the moving average of the readings it is more complicated because you have to drop the oldest reading from the total before calculating the average, but it is still possible to do it.

Could you explain in more detail on how to do the moving average? do you have a code or a block of code that you could post? Im a high school student who is semi-new to arduino and coding, so the more help people are able to supply, the better i am able to understand and hopefully become more advanced.

SImple way is multiply a running total by, say 9/10, and adding in 1/10th of the new reading.

You should consider averaging the magnitude of the acceleration, which is independent of the direction.

``````float mag = sqrt(X*X + Y*Y + Z*Z);  //where X,Y and Z are floating point values
``````

Well what my project is really trying to do is measure how much movement someone does while sleeping in a sitting position. So once the accelerometer is placed on the person, and they are done sleeping i want the serial monitor to say how well they slept. Minimal movement while sleeping, moderate movement while sleeping, or constant movement while sleeping. Is there a way to average out all three axis (X,Y and Z)? For example, and im making up numbers here. If X+Y+Z >=0 & <= 250 then print minimal movement. I know thats not how you would set it up in the code, its just an example of what i would like it to do.

Placed where on the person, measuring movement of which part of the body?
--Michael

The accelerometer is going to be placed inside a neck pillow. Within the neck pillow, its going to be placed on either of the sides that surrounds the neck, it hasnt been decided which side yet, which i dont think really matters.

Is there a way to average out all three axis (X,Y and Z)?

The average acceleration is always 1 g, due to the chair pushing up and counteracting the force of gravity.