a running average is calculated with a FIFO queue, you keep adding new data and tossing old. You then calculate the standard deviation of the data in the queue. This standard deviation will grow big when you have much change of your measurement caused by the pothole and will return to a baseline value when the change is gone.

It's been two years since I did that project I call music box:

http://liudr.wordpress.com/2011/01/24/music-box/

The main part of checking is here. Every time you do a measurement, you add it to the stat object for running average calc and then find standard deviation to compare with a threshold.

void checkBox()

{

ax_stats.add(x_level);

ay_stats.add(y_level);

az_stats.add(z_level);

// SerialDebug0();

if ((ax_stats.get_stdev()<gx_factor*0.1)&&(ay_stats.get_stdev()<gy_factor*0.1)&&(az_stats.get_stdev()<gz_factor*0.1)) // Stable condition

{

if (ax_stats.within_eb(gx_factor,gx_factor*0.15)&&last_number!=2) last_number=2;

else if (ax_stats.within_eb(-gx_factor,gx_factor*0.15)&&last_number!=1) last_number=1;

else if (ay_stats.within_eb(-gy_factor,gy_factor*0.15)&&last_number!=4) last_number=4;

else if (ay_stats.within_eb(gy_factor,gy_factor*0.15)&&last_number!=3) last_number=3;

else if (az_stats.within_eb(gz_factor,gz_factor*0.15)&&last_number!=5) last_number=5;

else if (az_stats.within_eb(-gz_factor,gz_factor*0.15)&&last_number!=6) last_number=6;

// else last_number=0;

}

else if ((ax_stats.get_stdev()>gx_factor*0.35)||(ay_stats.get_stdev()>gy_factor*0.35)||(az_stats.get_stdev()>gz_factor*0.35))

{

playTone(last_number);

}

}

stat.h

`#include <Arduino.h>`

#ifndef stat_h

#define stat_h

class stat

{

public:

int buffer[5]; // This stores analog level for running average.

int buffer_pointer; // This is the pointer for analog level for running average.

void add(int number);

float get_avg(); // Calculate for running average.

float get_stdev(); // Calculate for running average and then standard deviation.

boolean within_eb(float center, float dev); // Calculate whether the running average is within center +- dev.

stat();

};

#endif

stat.cpp

`#include "stat.h"`

#include "math.h"

stat::stat()

{

for (short i=0;i<5;i++)

{

buffer[i]=0;

}

buffer_pointer=0;

}

void stat::add(int number)

{

if (buffer_pointer==5) buffer_pointer=0;

buffer[buffer_pointer++]=number;

}

float stat::get_avg()

{

float avg=0;

for (short i=0;i<5;i++)

{

avg+=buffer[i];

}

avg/=5.0;

return avg;

}

float stat::get_stdev()

{

float avg=0;

float stdev=0;

for (short i=0;i<5;i++)

{

avg+=buffer[i];

}

avg/=5.0;

for (short i=0;i<5;i++)

{

stdev+=(buffer[i]-avg)*(buffer[i]-avg);

}

stdev/=5.0;

stdev=sqrt(stdev);

return stdev;

}

boolean stat::within_eb(float center, float dev)

{

float avg=0;

float stdev=0;

for (short i=0;i<5;i++)

{

avg+=buffer[i];

}

avg/=5.0;

if (abs(avg-center)<dev) return true;

else return false;

}