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;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
void setup() {
pinMode(Sensor, INPUT);
pinMode(13,OUTPUT);
}
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;
}
}
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 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;
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);
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;
}
}
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;
}
}
dude is the code in-correct?
ReplyDeletebecause when i verifiy the code it says 'Sensor' was not declared in this scope for line '12'
Make sure that you have library file for mic
DeleteIt is declared. Look on the first line.
Deletecheck 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.
DeleteIf I want to the sensor to detect 2 beep from computer does it work as well as clap
ReplyDeleteHow can I make a free blog like you have here?
ReplyDeleteHi.Log on to blogspot dot com to create a new free blog.
Deletei did everything correctly but its just not turning the light on
ReplyDelete:(
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