HELP Sound to trigger LED to stay on until 10 seconds of silence

I'm totally new to Ardunio programming and couldn't find a relevant topic

I'd like someone to help provide code which will perform the following.

  1. When digital input form a sound sensor goes high, turn on LED_BUILTIN for 10 seconds.
  2. If there's sound within this 10 seconds, remain on
  3. After 10 seconds of silence, turn off LED

The main objective is to keep a consistent HIGH state without toggling the LED_BUILTIN until the 10 seconds of silence is archived. There'll be a relay in my circuit and I'd like to prevent toggling.

#define LED_ON_TIME     10000ul //10000mS == 10-seconds

#define LED_OFF         0
#define LED_ON          1
//

const byte pinSensor = 2;   //whatever pin you're using...
const byte pinLED = LED_BUILTIN;

unsigned long
    timeLED,
    timeNow;

byte
    stateLED;

void setup() 
{
    Serial.begin( 9600 );
    pinMode( pinSensor, INPUT );    //INPUT_PULLUP if device is OC
    pinMode( pinLED, OUTPUT );

    stateLED = LED_OFF;

}//setup

void loop() 
{
    timeNow = millis();
    switch( stateLED )
    {
        case    LED_OFF:
            //LED is off now; does sensor report sound?
            if( digitalRead( pinSensor ) == HIGH )
            {
                //yes; turn on LED and set timer
                digitalWrite( pinLED, HIGH );
                timeLED = timeNow;
                //then move to ON state
                stateLED = LED_ON;
                
            }//if
        break;

        case    LED_ON:
            //whenever sensor shows high, reset LED on timer
            if( digitalRead( pinSensor ) == HIGH )
                timeLED = timeNow;

            //when ontime has expired, turn off
            if( timeNow - timeLED >= LED_ON_TIME )
            {
                digitalWrite( pinLED, LOW );
                stateLED = LED_OFF;
                
            }//if
        break;
        
    }//switch 
    
}//loop

Fantastic thank you

It took me an additional hour to determine the sound sensor Potentiometer was set in the wrong direction. The "out" port LED on the microphone has a logic of NO SOUND = ON, SOUND = OFF.... strange.

You're scripting worked. I added missing brackets for one of the if statements, I wouldn't be surprised if it worked without them... Thank you!

DCarnevale:
I added missing brackets for one of the if statements, I wouldn't be surprised if it worked without them...

After a quick look at the code I posted it doesn't immediately jump out at me: Where did you add brackets?

If there is only one statement for the if() to execute, then the braces are optional.

For example

if( digitalRead( pinSensor ) == HIGH )
   timeLED = timeNow;

is the same as

if( digitalRead( pinSensor ) == HIGH )
  {
  timeLED = timeNow;
  }

And the same as:

if( digitalRead( pinSensor ) == HIGH ) timeLED = timeNow;

FALSE is zero, anything else is TRUE, and HIGH is "1". So, since HIGH equals TRUE, you could write it like this:

if( digitalRead( pinSensor )) timeLED = timeNow;

All of these are equivalent to the compiler, so use whichever construct makes sense to you.

if( digitalRead( pinSensor ) == HIGH ) timeLED = timeNow;
if( digitalRead( pinSensor ) == HIGH ) { timeLED = timeNow;}
if( digitalRead( pinSensor ) == HIGH ) 
{
timeLED = timeNow;
}

Although the braces are optional when only one action is performed, putting in the braces is a good habit.

dave-in-nj:
Although the braces are optional when only one action is performed, putting in the braces is a good habit.

Perhaps, but on the other hand it may be appropriate to highlight the difference between a single action, and a complete functional block.