Trying to time the length of a sound

Hi all

I'm not sure whether this is the correct subforum as could be my programming or something to do with the sensor (but probably the former so I posted here!).

I have my Uno connected to a sound sensor which provide analogue output (which I've connected to my analogue pin 5).

I know the sensor works and will trigger if I clap, click my hands but I can't seem to get it to measure the length of the sound using the below code:

const int threshold_perc = 30;
const int LISTEN_PIN = 5;

void setup() {
  Serial.begin (9600);
}

void loop() {
  
  int actual_perc = (float) analogRead (LISTEN_PIN) / (float) 1023 * 100; //calculate percentage of max analogue

  if (actual_perc >= threshold_perc)
  {
      unsigned long msStart = micros();
      unsigned long msCount = 0;    
      
      while (actual_perc >= threshold_perc)  //start counting
      {
        msCount = micros() - msStart;  //calculate total time for each loop

        actual_perc = (float) analogRead (LISTEN_PIN) / (float) 1023 * 100; //update sensor values
      }
      
      Serial.println(msCount);
      delay (100);
    
  }

Whenever I use this code and click my fingers (leaving a few seconds in between) I get output like the following from Serial:

0
0
4
320
320
172
0
160
4
4
0
168
4
4
0
164

Thanks in advance

  delay (100);Stop listening for 1/10th of a second.

AWOL:
  delay (100);Stop listening for 1/10th of a second.

thanks - now when I click my fingers once I get six - 10 different numbers?

thanks - now when I click my fingers once I get six - 10 different numbers?

Now, when I am using different code, that I haven't posted, I get 6 to ten different numbers that I'm not going to show you...

Is that a question or a statement?

PaulS:
Now, when I am using different code, that I haven't posted, I get 6 to ten different numbers that I'm not going to show you...

Is that a question or a statement?

It's pretty obvious that I meant the code is the same minus AWOL's suggestion above. Use some common sense. Generally a question mark implies a question.

Generally a question mark implies a question.

Indeed. So, why are you asking us whether you get 6 to 10 different numbers? We can't see your current code or your serial output.

Use some common sense.

That would mean NOT replying to incomplete posts. Bye.

sbaratheon:
Generally a question mark implies a question.

Well no, a ? is actually explicit, not implicit :wink:

But be that as it may, the words don't make up a question, in spite of the ? on the end.

(Side note: I wonder if there's such a word as "explies", where implies:implicit::explies:explicit.))

PaulS:
Indeed. So, why are you asking us whether you get 6 to 10 different numbers? We can't see your current code or your serial output.
That would mean NOT replying to incomplete posts. Bye.

I'm asking why it isn't working with 6 to 10 different numbers. I know that and you know that (as does everyone else).

Perhaps it's the intensity of the sound varying from nothing, to a little, a bit more, to max, down again?

So they're all part of the snap of your fingers but it's more sine wave than a square.

If you have an oscilloscope it would be very interesting to see the waveform.

kenwood120s:
Well no, a ? is actually explicit, not implicit :wink:

But be that as it may, the words don't make up a question, in spite of the ? on the end.

(Side note: I wonder if there's such a word as "explies", where implies:implicit::explies:explicit.))

What the hell are you talking about? I admit I didn't put my question as clearly as I could have done but everyone who says that they didn't understand me is being disingenuous.

I'm thinking the time between rising and falling thresholds will vary greatly with the attack and decay profiles of the sound, a dull "thump" would result in a longer time than a sharp "snap" even if both have the same duration.

An oscilloscope will paint a simple picture of volts vs time and allow for calibration of the tthresholds and timing in the sketch.

We don't know what sensor you're using. We don't know anything about its output except it's apparently "analogue".

That makes it very difficult to work out what it is about that output that is giving those results.

What you really want is a device which gives you the envelope of a sound NOT the sound itself. Is that what you have?

Steve

slipstick:
We don't know what sensor you're using. We don't know anything about its output except it's apparently "analogue".

That makes it very difficult to work out what it is about that output that is giving those results.

What you really want is a device which gives you the envelope of a sound NOT the sound itself. Is that what you have?

Steve

It came as a kit and wasn't labelled with a model number. I managed to find a reference to it here: http://wiki.epalsite.com/index.php?title=Starter_Kit_for_Arduino#Lesson_21_Sound_Sensor_Playing

sbaratheon:
It came as a kit and wasn't labelled with a model number. I managed to find a reference to it here: http://wiki.epalsite.com/index.php?title=Starter_Kit_for_Arduino#Lesson_21_Sound_Sensor_Playing

that is a big help! You have what Steve was asking about. It gives a variable voltage based on the sound envelope or power level of the sound. Now, what was your original question? Oh, yeah. The length of the sound.

Pick a low level number given by the sensor before the sound. If the sound level goes above that level, save the time in milliseconds and continue to watch the analog value. When the sound level drops back to the first number, or below, get a new millisecond value.

Subtract the first millisecond value from the last and that is the length of the sound.

Paul

Thanks

I've written the following piece of code:

const int threshold_perc = 30;
const int LISTEN_PIN = 5;

void setup() {
  Serial.begin (9600);
}
unsigned long msCount = micros();

void loop() {
  
        int actual_perc = (float) analogRead (LISTEN_PIN) / (float) 1023 * 100; //calculate percentage of max analogue

        Serial.print(actual_perc);
        Serial.print(",");
        Serial.println(micros() - msCount);
        
        msCount = micros();  //calculate total time for each loop
}

just to see what values are coming out when I play a single beep from my computer (and the time for each measurement, I'm guessing this will be slowed down by Serial).

I get the following output:

0,2080
0,2080
0,2080
0,2080
52,3120
32,3120
16,3120
0,2080
53,3120
30,3120
17,3120
0,2080
49,3120
27,3120
15,3120
0,2080
47,3120
25,3120
14,3120
0,2080
45,3120
25,3120
14,3116
0,2080
0,2080
10,3120
0,2080
18,3120
20,3120
8,2080
0,2080
5,2080
3,2080
0,2080
3,2080
0,2080
0,2080
0,2080

It seems odd to me that there are readings (during the 'middle' of the sound) that are 0. Does that seem odd to you?

Why do you think the single beep is really a single beep and not a series of short beeps? Will sound the same to you. You REALLY need to see the signal on an oscilloscope if you want this to work.

Paul

Paul_KD7HB:
Why do you think the single beep is really a single beep and not a series of short beeps? Will sound the same to you. You REALLY need to see the signal on an oscilloscope if you want this to work.

Paul

Because I created an MP3 with a single tonal noise.

sbaratheon:
Because I created an MP3 with a single tonal noise.

Good reason!

Does your sensor have a resonance at or near that frequency? Again, unless you monitor the sensor output with an oscilloscope so you can see the voltage output, you will always encounter problems like this.

Perhaps the sensor sees an echo from something nearby. Who knows.

Paul

Please provide some concrete information about the sensor, and a schematic of how it is connected.