sound activated switch code

I am kind of new to the whole Arduino thing and i am trying to get some script to make an electret microphone turn on/off an led when it detects a loud sound like a clap for example. any suggestions?

How about this?

I tried but it dident seem to work the way i wanted it to. I want to have it so i clap once and the LED turns on and clap again to turn it off.

I want to have it so i clap once and the LED turns on and clap again to turn it off

But that's what that sketch does- it toggles the pin....

ledState = !ledState;          
digitalWrite(ledPin, ledState);

What does yours do?

oh I see thank you lots :slight_smile:

Is there a way to block out electrical noise from things like a laptop for instance because its registering a knock even though its silent.

Other than using longer cables 8), have you tried to adjust the threshold value?

const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not

yes but now the thing doesn't even register anything. is there a way to do it with an electret microphone? would that make a difference?

Don't know.... my knowledge of this stuff just ran out

thank u for trying to help