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