Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /****************************************
  2. Example Sound Level Sketch for the
  3. Adafruit Microphone Amplifier
  4. ****************************************/
  5.  
  6. const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
  7. unsigned int sample;
  8.  
  9. void setup()
  10. {
  11. Serial.begin(9600);
  12. }
  13.  
  14. boolean coin = true;
  15. float maxValue = 0.00;
  16. void loop()
  17. {
  18.  
  19. unsigned long startMillis= millis(); // Start of sample window
  20. unsigned int peakToPeak = 0; // peak-to-peak level
  21.  
  22. unsigned int signalMax = 0;
  23. unsigned int signalMin = 1024;
  24.  
  25. // collect data for 50 mS
  26. while (millis() - startMillis < sampleWindow)
  27. {
  28. sample = analogRead(0);
  29. if (sample < 1024) // toss out spurious readings
  30. {
  31. if (sample > signalMax)
  32. {
  33. signalMax = sample; // save just the max levels
  34. }
  35. else if (sample < signalMin)
  36. {
  37. signalMin = sample; // save just the min levels
  38. }
  39. }
  40. }
  41. peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
  42. double volts = (peakToPeak * 5.0) / 1024; // convert to volts
  43. if(volts > maxValue) {
  44. maxValue = volts;
  45. }
  46. if(coin == true) {
  47. Serial.println(volts);
  48. }
  49. if(volts > 0.60) {
  50. coin = false;
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement