How to read sound sensor readings and light up sensor with water sensor if the readings is above threshold & when water is detected for 2 mins? I am student that is new to Arduino and working on a project to trigger an alert if the sound level detected exceeds the threshold for a period of 2 mins. But I am stuck, would like to check how do I write a code to analog read sound sensor input and light up sensor if the readings is above datum for 2 mins? I had simple code but it triggers the light up immediately. :/

I have combined the coding together and this is the new code that worked with the help gotten:

    #define led 4 // led to D4 
const int soundsensor= A0; //sound sensor to A0
const int threshold= 100; // to set the threshold value for sound sensor
int ssanalogread; //name of sound sensor analog read
bool wasOver;//add to globals
unsigned long firstDetect;


void setup() {
  Serial.begin(9600);
  pinMode(led,OUTPUT);
  pinMode(soundsensor,INPUT);// to get input from sound sensor

} // end void setup


void loop() {

     sound1();

} // close void loop


void sound1() {

  ssanalogread=analogRead(soundsensor); // reads analog data from sound sensor
  Serial.println(ssanalogread);

   if ((ssanalogread &gt; threshold)){ //replace if else
      if(!wasOver){
       firstDetect = millis();
   }
   if(millis() - firstDetect &gt; 1*10*1000){
      digitalWrite(led,HIGH); //turns led on if the sensor reads more than threshold
   } //end if
      wasOver = true;
   } // end first if
  else {
     wasOver = false;
     digitalWrite(led,LOW); 
  } // end else

} //end loop


//this code works and is ok to run. can adjust the timing
You set a timestamp when the sound first exceeds the threshold and only set the output high when millis() - firstDetect &gt; 2*60*1000

//add to globals
bool wasOver;
unsigned long firstDetect;

//replace if else
if ((ssanalogread &gt; threshold)){
   if(!wasOver){
       firstDetect = millis();
   }
   if(millis() - firstDetect &gt; 2*60*1000){
      digitalWrite(led,HIGH); //turns led on if the sensor reads more than threshold
   } //end if
   wasOver = true;
} else {
   wasOver = false;
   digitalWrite(led,LOW); 
}