Advertisement
learnelectronics

Arduino Clapper

Oct 24th, 2023
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.34 KB | Source Code | 0 0
  1. /**********************************************************
  2.  *                     Arduino Clapper v3
  3.  *                learnelectronics 23 OCT 2023
  4.  *                email: [email protected]
  5.  *          https://www.youtube.com/learnelectronics
  6.  *                
  7.  *                Original Sketch by: Sarful
  8.  *              https://www.hackster.io/sarful
  9.  ***********************************************************/
  10.  
  11.  
  12.  
  13.  
  14. int NumberSounds = 0;
  15. int SoundPin = 3;
  16. int RawValue = 0;
  17. int NumberClaps = 0;
  18. int LightOn = 0;
  19. unsigned long SoundDetectedTime = 0;
  20. unsigned long PreviousSoundDetectedTime = 0;
  21. int UniqueClapMinTime = 100;
  22. int LEDPin = 2;
  23. unsigned long PreviousClapTime = 0;
  24. unsigned long CurrentClapTime = 0;
  25. unsigned long MaxTimeBetweenClaps = 2000;
  26.  
  27.  
  28. void setup()
  29. {
  30.  pinMode(SoundPin, INPUT);
  31.  pinMode(LEDPin, OUTPUT);
  32.  Serial.begin(9600);
  33.  Serial.println("The Light Clapper ...");
  34. }
  35.  
  36.  
  37. int IsSoundPartOfUniqueClap()
  38. {
  39.  int result = 0;
  40.  unsigned long ElapsedTime =
  41.    SoundDetectedTime -  
  42.    PreviousSoundDetectedTime;
  43.  if (ElapsedTime >= UniqueClapMinTime)
  44.  {
  45.     result = 1;
  46.  }
  47.  return result;
  48. }
  49.  
  50.  
  51.  
  52. int CheckTurnOnOffLight()
  53. {
  54.  int result = 0;
  55.  unsigned long ElapsedTime =  
  56.    CurrentClapTime - PreviousClapTime;
  57.  if (ElapsedTime <= MaxTimeBetweenClaps)
  58.  {
  59.     if (NumberClaps == 2)
  60.     {
  61.        result = 1;
  62.        NumberClaps = 0;
  63.     }
  64.  }
  65.  else
  66.  {
  67.     NumberClaps = 1;
  68.  }
  69.  return result;
  70. }
  71.  
  72.  
  73. void loop()
  74. {
  75.  RawValue = digitalRead(SoundPin);
  76.  if (RawValue == 0)
  77.  {
  78.     Serial.print("SOUND DETECTED ... ");
  79.     Serial.print(", Sound Number: ");
  80.     Serial.print(NumberSounds);
  81.     Serial.print(", RawValue: ");
  82.     Serial.println(RawValue);
  83.     NumberSounds++;
  84.  
  85.     // Process raw data for claps
  86.     PreviousSoundDetectedTime =
  87.       SoundDetectedTime;
  88.     SoundDetectedTime = millis();
  89.     if(IsSoundPartOfUniqueClap())
  90.     {
  91.        NumberClaps++;
  92.  
  93.        // Update Clap Times
  94.        PreviousClapTime =
  95.          CurrentClapTime;
  96.        CurrentClapTime = millis();
  97.  
  98.        // Turn Light ON/OFF as needed
  99.        if (CheckTurnOnOffLight())
  100.        {
  101.           LightOn = ~LightOn;
  102.           if (LightOn)
  103.           {
  104.              digitalWrite(LEDPin, HIGH);
  105.           }
  106.           else
  107.           {
  108.              digitalWrite(LEDPin, LOW);
  109.           }
  110.        }
  111.     }
  112.  }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement