Clapper Calibration Program Arrays acting up

I have been working on a clapper recently, and also wrote a calibration program so that I can, well, calibrate the clapper. Here’s the cal program:

/* Calibration program for clapper
 * Determines clap length and time b/w two claps
 * baum, 2011-2012
 */
 
/* --DEFINES-- */

/* The clapper uses a Schmitt Trigger-like detection system.
 * Specify the High and Low Values below.
 */
#define H_THRESH      150
#define L_THRESH      50


/* Pins:
 * Defines for the Mic and Relay pins.
 */
 
#define ANALOG_PIN    A0
#define RELAY_PIN     13

/* --VARIABLES-- */
unsigned long mainTime;
unsigned long clapTime[2]; //one for first clap, one for second clap

void setup() {
  
  Serial.begin(9600);
  Serial.println("Clapper Calibration Program");
  Serial.println("baum, 2011-2012");
  Serial.println();
  Serial.println("Ready. Clap twice");
  
  /* --FIRST CLAP-- */
  while(analogRead(ANALOG_PIN) < L_THRESH) {
  }//wait until above low (for clap timer)
  
  clapTime[0] = millis(); //start counting clap timer 1
  mainTime = clapTime[0]; //also start main timer;
  
  while(analogRead(ANALOG_PIN) < H_THRESH) {
  } //wait until above high threshold
  while(analogRead(ANALOG_PIN) > L_THRESH) {
  } //wait until below low threshold
  
  clapTime[0] = millis() - clapTime[0]; 
  /* --END FIRST CLAP-- */
  
  /* --SECOND CLAP-- */
  while(analogRead(ANALOG_PIN) < L_THRESH) {
  }//wait until above low (for clap timer)
  
  clapTime[1] = millis(); //start counting clap timer 2
  
  while(analogRead(ANALOG_PIN) < H_THRESH) {
  } //wait until above high threshold
  while(analogRead(ANALOG_PIN) > L_THRESH) {
  } //wait until below low threshold
  
  clapTime[1] = millis() - clapTime[1]; //stop clap timer 2
  /* --END SECOND CLAP-- */
  
  mainTime = millis() - mainTime; //stop main timer
  
  /* Print results */
  
  Serial.println();
  Serial.println("Results:");
  Serial.print("Clap 1 length:\t"); Serial.println(clapTime[0]);
  Serial.print("Clap 2 length:\t"); Serial.println(clapTime[1]);
  Serial.print("Overall length:\t"); Serial.println(mainTime);
  Serial.println();
  Serial.println("Press RESET do restart");
  Serial.println();
  Serial.println();
}

void loop() {
  //everything happens in setup()
}

So what’s SUPPOSED to happen is that the program will tell you the lengths of each clap as well as the total time. My circuit is just an electret + opamp + a filtering cap and rectifying diode.

However, instead of acting the right way, I get something like this every time:

Clapper Calibration Program
baum, 2011-2012

Ready. Clap twice

Results:
Clap 1 length:	125
Clap 2 length:	205
Overall length:	330

Press RESET do restart

I have done it many times, yet Clap 1 + Clap 2 always equals Overall. What I want is to see how long clap 2 was… so what is the problem? Is it something with the arrays?

Thanks!

baum

How fast does the clap sound decay?
This:

  while(analogRead(ANALOG_PIN) > L_THRESH) {
  } //wait until below low threshold
  
  clapTime[0] = millis() - clapTime[0]; 
  /* --END FIRST CLAP-- */
  
  /* --SECOND CLAP-- */
  while(analogRead(ANALOG_PIN) < L_THRESH) {
  }//wait until above low (for clap timer)

Might be problematic. If the first analogRead returns L_THRESH, your loop exits (contrary to the comment). If it’s still returning L_THRESH when you read it for the second clap, you will immediately exit that loop and start counting time. Claps will them appear to be back to back, explaining your timing results. Try >= and <. I’m not convinced this is your issue, but it is a hole in the logic.

I tried that, and it worked…

As for the clap:
There is an immediate spike to about 300, then up to 500, then it comes down to about 20, where it ripples a bit. Yet somehow, EVERY TIME, it reads exactly 50 on both while statements… (I put a println in there to see) Why is it doing this? If I just run analogReadSerial, then it rarely hits 50… I only see this behavior with this one program! But the <= worked, so its fine…

Thanks!

baum