I have just started writing a handy "Average" library.
At the moment it just deals with rolling averages. I have used it with this piece of analog button debounce code:
#include <Average.h>
#define SMOOTH 10
int history[SMOOTH];
int bvals[] = {1023,819,768,683,513,0};
int analogButton(int channel, int *vals, int nvals)
{
int value;
int lastValue = 0;
int l;
while(1)
{
value = rollingAverage(history,SMOOTH,analogRead(channel));
if(value == lastValue)
{
for(l=0; l<nvals; l++)
{
if(value == vals[l])
{
return l;
}
}
}
lastValue = value;
}
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
int value;
static int lastValue = 0;
value = analogButton(0,bvals,6);
while(lastValue == value)
{
value = analogButton(0,bvals,6);
}
lastValue = value;
Serial.println(value);
}
You will need my Average library for this:
http://hacking.majenko.co.uk/libs/average