Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. /*
  2. * The Circuit:
  3. * Connect AUD to analog input 0
  4. * Connect GND to GND
  5. * Connect VCC to 3.3V (3.3V yields the best results)
  6. *
  7. * To adjust when the LED turns on based on audio input:
  8. * Open up the serial com port (Top right hand corner of the Arduino IDE)
  9. * It looks like a magnifying glass. Perform several experiments
  10. * clapping, snapping, blowing, door slamming, knocking etc and see where the
  11. * resting noise level is and where the loud noises are. Adjust the if statement
  12. * according to your findings.
  13. *
  14. * You can also adjust how long you take samples for by updating the "SampleWindow"
  15. *
  16. * This code has been adapted from the
  17. * Example Sound Level Sketch for the
  18. * Adafruit Microphone Amplifier
  19. *
  20. */
  21.  
  22. #include <pins_arduino.h>
  23.  
  24. const int sampleWindow = 250; // Sample window width in mS (250 mS = 4Hz)
  25. unsigned int knock;
  26. int ledPin = D4;
  27.  
  28. void setup()
  29. {
  30. Serial.begin(115200);
  31. pinMode(ledPin, OUTPUT);
  32. Serial.println("Starting...");
  33. }
  34.  
  35. void loop()
  36. {
  37. unsigned long start= millis(); // Start of sample window
  38. unsigned int peakToPeak = 0; // peak-to-peak level
  39.  
  40. unsigned int signalMax = 0;
  41. unsigned int signalMin = 1024;
  42.  
  43. // collect data for 250 miliseconds
  44. while (millis() - start < sampleWindow)
  45. {
  46. knock = analogRead(A0);
  47.  
  48. if (knock < 1024) //This is the max of the 10-bit ADC so this loop will include all readings
  49. {
  50. if (knock > signalMax)
  51. {
  52. signalMax = knock; // save just the max levels
  53. }
  54. else if (knock < signalMin)
  55. {
  56. signalMin = knock; // save just the min levels
  57. }
  58. }
  59. }
  60.  
  61. peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
  62. double volts = (peakToPeak * 3.3) / 1024; // convert to volts
  63.  
  64. Serial.println(volts);
  65. if (volts >=1.0)
  66. {
  67. //turn on LED
  68. digitalWrite(ledPin, HIGH);
  69. delay(500);
  70. Serial.println("Knock Knock");
  71. }
  72. else
  73. {
  74. //turn LED off
  75. digitalWrite(ledPin, LOW);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement