Turn ON your LEDs and Lamps with a Clap



Turn ON your LEDs and Lamps with a Clap

In this tutorial we will learn how to control the Arduino Sound Sensor. So we will control our LEDs and Lamps with a clap. We will use a sound sensor and a relay module for this.

Hardware Required:

Arduino Board - https://goo.gl/Rqc5w2
Relay Module - https://goo.gl/yAxcPL
Sound Sensor - https://goo.gl/2pXWsw
LED Strip 5M - https://goo.gl/Wv08ZS
LED Adapter - https://goo.gl/Qf3YA8
9V Battery - https://goo.gl/3EIJcf
F to F Jumper - https://goo.gl/TdGrkk
M to M Jumper - https://goo.gl/VRzUN4
Breadboard - https://goo.gl/08nnr1
Battery Cable - https://goo.gl/aBKusM

Recomended Site - https://goo.gl/V9nYtx

---

Get the Code
int sound_sensor = 4;
int relay = 5;

int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
 
void setup() {
  pinMode(sound_sensor, INPUT);
  pinMode(relay, OUTPUT);
}
 
void loop() {
  int status_sensor = digitalRead(sound_sensor);
  if (status_sensor == 0)
  {
    if (clap == 0)
    {
      detection_range_start = detection_range = millis();
      clap++;
    }
    else if (clap > 0 && millis()-detection_range >= 50)
    {
      detection_range = millis();
      clap++;
    }
  }
  if (millis()-detection_range_start >= 400)
  {
    if (clap == 2)
    {
      if (!status_lights)
        {
          status_lights = true;
          digitalWrite(relay, HIGH);
        }
        else if (status_lights)
        {
          status_lights = false;
          digitalWrite(relay, LOW);
        }
    }
    clap = 0;
  }
}