If the sound level is below the threshold, the DO pin of the sensor signals a high value. If the sound level exceeds the threshold, i.e you clap you hands, the DO is pulled to GND and the signal gets LOW for a short while. After this short while the signals gets HIGH again.
Now look at you loop:
void loop()
{
int status_sensor = digitalRead(Sensor);
if (status_sensor == 0)
{
// you enter this block if you clap your hands and the
// mcu is fast enough to capture the signal / DO pin (LOW)
// within the short time interval
// I'm sure it is fast enough to enter this block multiple
// times in the interval the clap is active.
if (status_lights == true)
{
status_lights = false;
digitalWrite(13, LOW);
}
else
{
status_lights = true;
digitalWrite(13, HIGH);
}
}
}
You see, the LED is toggled as long as the LOW interval of the sensor is active (DO=LOW).
I interpret you statement "My code only turns on the led but dosen't turn it off when I clap." so, that the LED is on, when the program starts without a clap. That means that the environmental noise is loud enough to activate the sensor. Then the loop above is iterated permanently and you switch on and off the LED at a very fast speed. That's like you would apply a PWM waveform on the LED pin. The LED is on but dimmed to about 50%.
To find out if I'm right, just add a delay in the loop. My assumption seems to be correct, if the LED blinks with one herz (one time per second). The you can adjust the sound threshold until the LED stops blinking.
void loop()
{
int status_sensor = digitalRead(Sensor);
// Add the delay here ..........
delay(500);
if (status_sensor == 0)
{
If you have calibrated the sensor, remove the delay from the code and add a new delay after changing the LED states. That should be much shorter, because it's only there to ensure that you don't toggle the LED while the sensor is active.
if (status_lights == true)
{
status_lights = false;
digitalWrite(13, LOW);
}
else
{
status_lights = true;
digitalWrite(13, HIGH);
}
// wait until the clap is over
delay(100);
status_sensor
to see what it actually is