Arduino Grove Sound Sensor with LED- HELP

Hi, I am a student working on a project to program and light up Arduino's LED only after the sound level hits a threshold of 450 for a period of time, maybe say 3 mins. I am currently using arduino uno with base shield and I am new to Arduino. Am having trouble with adding the function in (lighting the LED up only when the captured value is higher than 450 for 3mins). Was wondering if any kind souls could help out in taking a look at my current coding. :confused:

#define ledpin 4 // led to D4
const int soundpin= 3; //sound sensor to D3
const int threshold= 450; // Threshold value for sound sensor
int soundDetectedVal = HIGH; // To record Sound Measurement

unsigned long lastSoundDetectTime; // Record the time that a sound is measured
int soundAlarmTime = 5000; // Number of milli seconds to keep the sound alarm high

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

pinMode(ledpin,OUTPUT);
pinMode(soundpin,INPUT);// to get input from sound module

}

void loop()
{

long soundsensor = 0;
for(int i=0; i<32; i++)
{
soundsensor += analogRead(soundpin);
}

soundsensor >>= 5;

// delay(1000); /* 1 second before next reading is collected */
/*first part end */

soundDetectedVal = digitalRead (soundpin) ; // read the sound alarm time

if ((soundDetectedVal == LOW)) {// If hear a sound

lastSoundDetectTime = millis(); // record the time of the sound alarm

if ((soundsensor>=threshold))
{

Serial.println("Suspected fault");
Serial.println(" ");
digitalWrite(ledpin,HIGH); //turns led on
Serial.println(soundsensor);
delay(1000); /* 1 second before next action */

}

else
{

if(( (millis()-lastSoundDetectTime) < soundAlarmTime) && (soundsensor<threshold))

Serial.println("No fault");
Serial.println(" ");
digitalWrite(ledpin,LOW);
Serial.println(soundsensor);
delay(1000); /* 1 second before next action */

} // close else if
} //close main if
} // close void loop

Did you write that code or did you copy it from somewhere? The averaging is a little "too cute" and I'm not sure you even want averaging... Where did that 450 value come from?

The first question is, what kind of analog readings are you getting? i.e. Run [u]Analog Read Serial Example[/u]. Except, take-out the delay and of course change it to read A3.

What are you reading with "silence"?

And, maybe add a couple lines of code to keep track of the minimum & maximum. If you do that, add a ~1 second delay during setup (before the loop starts) so things can stabilize before you start keeping track of the minimum & maximum.

After you know what kinds of readings you're getting you can set the threshold.

The delays in your program are a problem. 99% of the time your program is delaying (not reading anything).

If you are reading from analog pin 3, use A3, not 3

If you use digital it will trigger at approx 615.

Hi, thank for the replies. I took reference from Henry's Bench, credit: (Arduino Sound Sensor Module Tutorial | Henry's Bench) and modified it to try it out, but wasn't sure if I am editing it right..

Will try to edit again according to the comments. Can I check what's averaging?