Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************
- * Arduino Clapper v3
- * learnelectronics 23 OCT 2023
- * email: [email protected]
- * https://www.youtube.com/learnelectronics
- *
- * Original Sketch by: Sarful
- * https://www.hackster.io/sarful
- ***********************************************************/
- int NumberSounds = 0;
- int SoundPin = 3;
- int RawValue = 0;
- int NumberClaps = 0;
- int LightOn = 0;
- unsigned long SoundDetectedTime = 0;
- unsigned long PreviousSoundDetectedTime = 0;
- int UniqueClapMinTime = 100;
- int LEDPin = 2;
- 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);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement