Go Down

Topic: one bang (Read 521 times) previous topic - next topic

dontmarryme

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.
Code: [Select]

//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();
   }
   
 }
 
 
 }
 
     




drhex

Code: [Select]
Serial.print("A");
Serial.print(output_value);
Serial.flush();


On other systems, flush() can be used to force a write of stuff hiding in an output buffer, but on Arduino everything is already transmitted upon return from Serial.print(). flush() only clears the input queue.

Quote
I don't want to do it in max/msp


msp?

Are you saying you don't want to read a few values, and remember only the max() of them so only that value will eventually be sent on the serial port? Why not?

dontmarryme

Yes, maybe I can do it in max/msp
but if I can do it in arduino code that will save a lot space in communication.
also when you stuck somewhere, u just want to know how!

sxdemon

I know a way, but in C++ and I thnk its RAM consuming.

Go Up