Clap Switch with Arduino and Sound Sensor.


Welcome Visitors. Here is the code for  clap switch project.
int Sensor = A0;

int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
void setup() {
pinMode(Sensor, INPUT);
pinMode(13,OUTPUT);
}
void loop() {
int status_sensor = digitalRead(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(13, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(13, LOW);
}
}
clap = 0;
}
}


END. If you want to play a buzzer with led use the code below:::::
int Sensor = A0;
int buzzer =12; // add a buzzer/speaker( with a 10-100ohm resistor) to pin 12 + Gnd
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
void setup() {
pinMode(Sensor, INPUT);
pinMode(13,OUTPUT);
}
void Tone(){
tone(buzzer,1200);
}
void loop() {
int status_sensor = digitalRead(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(13, HIGH);
Tone();
}
else if (status_lights)
{
status_lights = false;
digitalWrite(13, LOW);
noTone(buzzer);
}
}
clap = 0;
}
}

Comments

  1. dude is the code in-correct?
    because when i verifiy the code it says 'Sensor' was not declared in this scope for line '12'

    ReplyDelete
    Replies
    1. Make sure that you have library file for mic

      Delete
    2. It is declared. Look on the first line.

      Delete
    3. check whether you capitalised 'Sensor'. I had written it in lowercase and i got a similar error. The logic of the code checks out 10/10.

      Delete
  2. If I want to the sensor to detect 2 beep from computer does it work as well as clap

    ReplyDelete
  3. How can I make a free blog like you have here?

    ReplyDelete
    Replies
    1. Hi.Log on to blogspot dot com to create a new free blog.

      Delete
  4. i did everything correctly but its just not turning the light on
    :(

    I followed the video several times
    I connected GND to GND and + to 5v
    I Copy-Pasted the full code and verified it
    The short part of the led is in ground and long is in Pin13
    And my parts are totally new and in mint condition
    And im using the same pins as in the video
    but the led is simply not working

    ReplyDelete

Post a Comment