i am using 2 accelerometer to detect a tap dance knock.
however i get a series bangs from one tap, the numbers are like:
1
22
34
66
5
86
34
3
but i just need the highest one, anyone knows how to do it in a easier way? I don’t want to do it in max/msp cause I want to reduce the data from serial port.
//this is the analog port of the accelerator axis (probably Z - check the board)
const unsigned char accelerator_input= 1;
const unsigned char accelerator_input2= 4;
const unsigned char output_value = 0;
const unsigned char output_value2 = 0;
//this is the avaeraging factor to smooth out the noise - be aware this must be below 50
//do not make it too big either - since you want to detect impulses!
const byte smooth_factor = 0;
const byte smooth_factor2 = 0;
//is a tap in positive (>) or negative (<) direction - to differentiate between tap and lifting your foot
#define TAP_CRITERIA > 750
#define TAP_CRITERIA2 > 750
//how big must the acceleration bee to detect a tap
const int tap_treshold = 750;
const int tap_treshold2 = 750;
//by what value it is scaled down?
const byte scale_down_factor = 2;
const byte scale_down_factor2 = 2;
//a rule of thumb - if you connect 3.3V to AREF:
//(1024-tap_treshold) / scale_factor must be smaller than 256
//this is used to store the average
int acceleration_value = 0;
int acceleration_value2 = 0;
void setup(){
Serial.begin(9600);
//this is to tell the Arduino to use AREF - connect 3.3V
analogReference(EXTERNAL);
}
void loop(){
//read the acceleration
int acceleration_readout = analogRead(accelerator_input);
int acceleration_readout2 = analogRead(accelerator_input2);
//smooth the readout
acceleration_value = (acceleration_value*smooth_factor+acceleration_readout)/(smooth_factor+1);
acceleration_value2 = (acceleration_value2*smooth_factor2+acceleration_readout2)/(smooth_factor2+1);
//is it a tap or foot lifting?
if (acceleration_value TAP_CRITERIA) {
//ok, now we are interested in the absolute value
unsigned int abs_acceleration = abs(acceleration_value);
//is it bigger than our treshold?
if (abs_acceleration>tap_treshold) {
//then apply some math to get
unsigned char output_value = (unsigned char) ((abs_acceleration-tap_treshold)/scale_down_factor);
Serial.print("A");
Serial.print(output_value);
Serial.flush();
}
}
if (acceleration_value2 TAP_CRITERIA2) {
unsigned int abs_acceleration2 = abs(acceleration_value2);
if (abs_acceleration2>tap_treshold2) {
unsigned char output_value2 = (unsigned char) ((abs_acceleration2-tap_treshold2)/scale_down_factor2);
Serial.print("C");
Serial.print(output_value2);
Serial.flush();
}
}
}