neuberfran

Untitled

Jun 4th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // The library toggles digital pin 4 in the sampling loop which allows
  2. // measurement of the actual sampling frequency.
  3. // If you call .sample() continuously like this:
  4. // while(1)dtmf.sample(sensorPin);
  5. // you can put a frequency counter on pin 4 to determine what the
  6. // sampling frequency is on your Arduino. Note that the frequency
  7. // counter will show half the actual rate. My meter showed 4.463kHz
  8. // so the sampling rate is 8926Hz
  9. #include <DTMF.h>
  10.  
  11. int sensorPin = A0;
  12. int led = 13;
  13.  
  14.  
  15. // NOTE that N MUST NOT exceed 160
  16. // This is the number of samples which are taken in a call to
  17. // .sample. The smaller the value of N the wider the bandwidth.
  18. // For example, with N=128 at a sample rate of 8926Hz the tone
  19. // detection bandwidth will be 8926/128 = 70Hz. If you make N
  20. // smaller, the bandwidth increases which makes it harder to detect
  21. // the tones because some of the energy of one tone can cross into
  22. // an adjacent (in frequency) tone. But a larger value of N also means
  23. // that it takes longer to collect the samples.
  24. // A value of 64 works just fine, as does 128.
  25. // NOTE that the value of N does NOT have to be a power of 2.
  26. float n=128.0;
  27. // sampling rate in Hz
  28. float sampling_rate=8926.0;
  29.  
  30. // Instantiate the dtmf library with the number of samples to be taken
  31. // and the sampling rate.
  32. DTMF dtmf = DTMF(n,sampling_rate);
  33.  
  34. void setup(){
  35. pinMode(led, OUTPUT);
  36. Serial.begin(115200);
  37. }
  38.  
  39. int nochar_count = 0;
  40. float d_mags[8];
  41. void loop()
  42. {
  43. char thischar;
  44.  
  45. // This reads N samples from sensorpin (must be an analog input)
  46. // and stores them in an array within the library. Use while(1)
  47. // to determine the actual sampling frequency as described in the
  48. // comment at the top of this file
  49. /* while(1) */dtmf.sample(sensorPin);
  50.  
  51. // The first argument is the address of a user-supplied array
  52. // of 8 floats in which the function will return the magnitudes
  53. // of the eight tones.
  54. // The second argument is the value read by the ADC when there
  55. // is no signal present. A voltage divider with precisely equal
  56. // resistors will presumably give a value of 511 or 512.
  57. // My divider gives a value of 506.
  58. // If you aren't sure what to use, set this to 512
  59. dtmf.detect(d_mags,506);
  60.  
  61. // detect the button
  62. // If it is recognized, returns one of 0123456789ABCD*#
  63. // If unrecognized, returns binary zero
  64.  
  65. // Pass it the magnitude array used when calling .sample
  66. // and specify a magnitude which is used as the threshold
  67. // for determining whether a tone is present or not
  68. //
  69. // If N=64 magnitude needs to be around 1200
  70. // If N=128 the magnitude can be set to 1800
  71. // but you will need to play with it to get the right value
  72. thischar = dtmf.button(d_mags,1800.);
  73. if(thischar) {
  74. Serial.print(thischar);
  75. nochar_count = 0;
  76. // Print the magnitudes for debugging
  77. //#define DEBUG_PRINT
  78. #ifdef DEBUG_PRINT
  79. for(int i = 0;i < 8;i++) {
  80. Serial.print(" ");
  81. Serial.print(d_mags[i]);
  82. }
  83. Serial.println("");
  84. #endif
  85. } else {
  86. // print a newline
  87. if(++nochar_count == 50)Serial.println("");
  88. // don't let it wrap around
  89. if(nochar_count > 30000)nochar_count = 51;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment