Mechatronics LAB
Published © Apache-2.0

Arduino Light Clapper use Sound Detector

You can in this tutorial Arduino Light Clapper uses Sound Detector step by step complete process.

BeginnerFull instructions provided1 hour1,917
Arduino Light Clapper use Sound Detector

Things used in this project

Hardware components

Arduino uno
×1
LED
×1
breadboard
×1
SparkFun Sound Detector (with Headers)
SparkFun Sound Detector (with Headers)
×1
connection wire
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Arduino Light Clapper use Sound Detector

Code

Arduino Light Clapper use Sound Detector

Arduino
int NumberSounds = 0;
int SoundPin = 8;
int RawValue = 0;
int NumberClaps = 0;
int LightOn = 0;
unsigned long SoundDetectedTime = 0;
unsigned long PreviousSoundDetectedTime = 0;
int UniqueClapMinTime = 100;
int LEDPin = 7;
unsigned long PreviousClapTime = 0;
unsigned long CurrentClapTime = 0;
unsigned long MaxTimeBetweenClaps = 2000;


void setup()
{
 pinMode(SoundPin, INPUT);
 pinMode(LEDPin, OUTPUT);
 Serial.begin(9600);
 Serial.println("The Light Clapper ...");
}


int IsSoundPartOfUniqueClap()
{
 int result = 0;
 unsigned long ElapsedTime = 
   SoundDetectedTime -  
   PreviousSoundDetectedTime;
 if (ElapsedTime >= UniqueClapMinTime)
 {
    result = 1;
 }
 return result;
}



int CheckTurnOnOffLight()
{
 int result = 0;
 unsigned long ElapsedTime =  
   CurrentClapTime - PreviousClapTime;
 if (ElapsedTime <= MaxTimeBetweenClaps)
 {
    if (NumberClaps == 2)
    {
       result = 1;
       NumberClaps = 0;
    }
 }
 else
 {
    NumberClaps = 1;
 }
 return result;
}


void loop()
{
 RawValue = digitalRead(SoundPin);
 if (RawValue == 0)
 {
    Serial.print("SOUND DETECTED ... ");
    Serial.print(", Sound Number: ");
    Serial.print(NumberSounds);
    Serial.print(", RawValue: ");
    Serial.println(RawValue);
    NumberSounds++;

    // Process raw data for claps
    PreviousSoundDetectedTime = 
      SoundDetectedTime;
    SoundDetectedTime = millis();
    if(IsSoundPartOfUniqueClap())
    {
       NumberClaps++;

       // Update Clap Times
       PreviousClapTime = 
         CurrentClapTime;
       CurrentClapTime = millis();

       // Turn Light ON/OFF as needed
       if (CheckTurnOnOffLight())
       {
          LightOn = ~LightOn;
          if (LightOn)
          {
             digitalWrite(LEDPin, HIGH);
          }
          else
          {
             digitalWrite(LEDPin, LOW);
          }
       }
    }
 }
}

Credits

Mechatronics LAB
75 projects • 48 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .
Contact

Comments

Please log in or sign up to comment.