Trigger for analog input

Hello,
it’s me again!

I want to trigger sudden loud sound and using 4 mics & my arduino due for it.
I’m comparing the average of 2048 last samples with the threshold.

I’m not satisfied with my code in several ways:

  1. it’s dependent on the threshold, which is fixed and in case of loud environment will give positive respond all the time
  2. if it detects the first loud sound it fail to detect following since trigger depends on the mean value and takes into consideration previous abrupt noises…

I think about making threshold value self-calibrating depending on the increase of average loudness level. but may be you would suggest me better solutions for my ‘problem’

thank you in advance!

here is my code:

#define SAMPLES 2048
#define BUFFERSIZE 8192

byte SV[BUFFERSIZE]; // every 4 entries are for 4 sensors
int k=0; //number of samples for buffer
byte sig;


int p=0; //number of pins
int pin[4]={A0, A1, A2, A3};
int limCounter[4]={0,0,0,0};
bool event[4]={0,0,0,0};
long TOA[4]={0,0,0,0};


float AA=0;
float THRESHOLD = 35;
long startTime;

int sumElements ( byte arr[], int a) {
 
 int summ=0;
 
 for (int m=0; m<SAMPLES; m++){
    summ+=(int) arr[4*m+a];    
  }
  //if (event[a])
 return summ;
  
}


void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
SV[BUFFERSIZE-4]=SV[BUFFERSIZE-3]=SV[BUFFERSIZE-2]=SV[BUFFERSIZE-1]=0;

}

void loop() {
  // read the input on analog pin 0..3:
startTime=micros();
  
  //read data
  sig = analogRead(pin[p]);
  //Serial.println(sig);
  //save it to array
  SV[4*k+p]=sig;
  AA=sumElements(SV, p)/SAMPLES;
//  Serial.println(AA);
  if (AA > THRESHOLD && !event[p]) {
    event[p]=1;
    Serial.println(p);
    limCounter[p]=1;
    TOA[p]=micros()-startTime;
  }
  else limCounter[p]++;



//in case event is outdated, reset all values
  if (limCounter[p] > SAMPLES) {
    event[p]=0; 
    limCounter[p]=0;
    
    }

//check when we recieve event:
if (event[0] && event[1] && event [2] && event [3]){
  Serial.println ("event occured");
  event[0]=event[1]=event[2]=event[3]=0;
  
}

//increment sample and pin number  
  k++;
  p++;
  if (k==2048) k=0;
  if (p==4) p=0;

  
//  sendVals(TOA);
  
//delay(10);       
  }

I want to trigger sudden loud sound

You want to cause a sudden loud sound? Or you want to trigger something else when a sudden loud sound happens?

  1. it's dependent on the threshold, which is fixed

How could it be otherwise? A "sudden loud sound" clearly means "some amount louder than the background level". That amount is a threshold. I suppose that the threshold could be variable - a percentage increase, for instance, but that's trivial to implement.

  1. if it detects the first loud sound it fail to detect following since trigger depends on the mean value and takes into consideration previous abrupt noises...

I'm not following this. Must be time to look at the code.

int p=0; //number of pins

No pins, huh?

int pin[4]={A0, A1, A2, A3};

These are subject to change in the code? Which Arduino has more than 255 pins?

float AA=0;
float THRESHOLD = 35;

Is it really that much trouble to type 0.0 or 35.0, so that it is clear that you are using the correct type?

    summ+=(int) arr[4*m+a];

Why is this cast necessary?

  sig = analogRead(pin[p]);

The Due has 12 analog inputs, each of which can provide 12 bits of resolution (i.e. 4096 different values). By default, the resolution of the readings is set at 10 bits, for compatibility with other Arduino boards.

You don't deal with the 10 vs. 12 bit resolution, so the values returned are 10 bit, that you truncate storing in a byte. Why?

That was a far as I got. I don't believe you are getting good data, so what you do with the data is suspect.