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.
#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