No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

230 líneas
6.0KB

  1. // Dial Tone (DTMF) decoding example.
  2. //
  3. // The audio with dial tones is connected to audio shield
  4. // Left Line-In pin. Dial tone output is produced on the
  5. // Line-Out and headphones.
  6. //
  7. // Use the Arduino Serial Monitor to watch for incoming
  8. // dial tones, and to send digits to be played as dial tones.
  9. //
  10. // This example code is in the public domain.
  11. #include <Audio.h>
  12. #include <Wire.h>
  13. #include <SPI.h>
  14. #include <SD.h>
  15. // Create the Audio components. These should be created in the
  16. // order data flows, inputs/sources -> processing -> outputs
  17. //
  18. AudioInputI2S audioIn;
  19. AudioAnalyzeToneDetect row1; // 7 tone detectors are needed
  20. AudioAnalyzeToneDetect row2; // to receive DTMF dial tones
  21. AudioAnalyzeToneDetect row3;
  22. AudioAnalyzeToneDetect row4;
  23. AudioAnalyzeToneDetect column1;
  24. AudioAnalyzeToneDetect column2;
  25. AudioAnalyzeToneDetect column3;
  26. AudioSynthWaveformSine sine1; // 2 sine wave
  27. AudioSynthWaveformSine sine2; // to create DTMF
  28. AudioMixer4 mixer;
  29. AudioOutputI2S audioOut;
  30. // Create Audio connections between the components
  31. //
  32. AudioConnection patchCord01(audioIn, 0, row1, 0);
  33. AudioConnection patchCord02(audioIn, 0, row2, 0);
  34. AudioConnection patchCord03(audioIn, 0, row3, 0);
  35. AudioConnection patchCord04(audioIn, 0, row4, 0);
  36. AudioConnection patchCord05(audioIn, 0, column1, 0);
  37. AudioConnection patchCord06(audioIn, 0, column2, 0);
  38. AudioConnection patchCord07(audioIn, 0, column3, 0);
  39. AudioConnection patchCord10(sine1, 0, mixer, 0);
  40. AudioConnection patchCord11(sine2, 0, mixer, 1);
  41. AudioConnection patchCord12(mixer, 0, audioOut, 0);
  42. AudioConnection patchCord13(mixer, 0, audioOut, 1);
  43. // Create an object to control the audio shield.
  44. //
  45. AudioControlSGTL5000 audioShield;
  46. void setup() {
  47. // Audio connections require memory to work. For more
  48. // detailed information, see the MemoryAndCpuUsage example
  49. AudioMemory(12);
  50. // Enable the audio shield and set the output volume.
  51. audioShield.enable();
  52. audioShield.volume(0.5);
  53. while (!Serial) ;
  54. delay(100);
  55. // Configure the tone detectors with the frequency and number
  56. // of cycles to match. These numbers were picked for match
  57. // times of approx 30 ms. Longer times are more precise.
  58. row1.frequency(697, 21);
  59. row2.frequency(770, 23);
  60. row3.frequency(852, 25);
  61. row4.frequency(941, 28);
  62. column1.frequency(1209, 36);
  63. column2.frequency(1336, 40);
  64. column3.frequency(1477, 44);
  65. }
  66. const float row_threshold = 0.2;
  67. const float column_threshold = 0.2;
  68. void loop() {
  69. float r1, r2, r3, r4, c1, c2, c3;
  70. char digit=0;
  71. // read all seven tone detectors
  72. r1 = row1.read();
  73. r2 = row2.read();
  74. r3 = row3.read();
  75. r4 = row4.read();
  76. c1 = column1.read();
  77. c2 = column2.read();
  78. c3 = column3.read();
  79. // print the raw data, for troubleshooting
  80. Serial.print("tones: ");
  81. Serial.print(r1);
  82. Serial.print(", ");
  83. Serial.print(r2);
  84. Serial.print(", ");
  85. Serial.print(r3);
  86. Serial.print(", ");
  87. Serial.print(r4);
  88. Serial.print(", ");
  89. Serial.print(c1);
  90. Serial.print(", ");
  91. Serial.print(c2);
  92. Serial.print(", ");
  93. Serial.print(c3);
  94. // check all 12 combinations for key press
  95. if (r1 >= row_threshold) {
  96. if (c1 > column_threshold) {
  97. digit = '1';
  98. } else if (c2 > column_threshold) {
  99. digit = '2';
  100. } else if (c3 > column_threshold) {
  101. digit = '3';
  102. }
  103. } else if (r2 >= row_threshold) {
  104. if (c1 > column_threshold) {
  105. digit = '4';
  106. } else if (c2 > column_threshold) {
  107. digit = '5';
  108. } else if (c3 > column_threshold) {
  109. digit = '6';
  110. }
  111. } else if (r3 >= row_threshold) {
  112. if (c1 > column_threshold) {
  113. digit = '7';
  114. } else if (c2 > column_threshold) {
  115. digit = '8';
  116. } else if (c3 > column_threshold) {
  117. digit = '9';
  118. }
  119. } else if (r4 >= row_threshold) {
  120. if (c1 > column_threshold) {
  121. digit = '*';
  122. } else if (c2 > column_threshold) {
  123. digit = '0';
  124. } else if (c3 > column_threshold) {
  125. digit = '#';
  126. }
  127. }
  128. // print the key, if any found
  129. if (digit > 0) {
  130. Serial.print(" --> Key: ");
  131. Serial.print(digit);
  132. }
  133. Serial.println();
  134. // uncomment these lines to see how much CPU time
  135. // the tone detectors and audio library are using
  136. //Serial.print("CPU=");
  137. //Serial.print(AudioProcessorUsage());
  138. //Serial.print("%, max=");
  139. //Serial.print(AudioProcessorUsageMax());
  140. //Serial.print("% ");
  141. // check if any data has arrived from the serial monitor
  142. if (Serial.available()) {
  143. char key = Serial.read();
  144. int low=0;
  145. int high=0;
  146. if (key == '1') {
  147. low = 697;
  148. high = 1209;
  149. } else if (key == '2') {
  150. low = 697;
  151. high = 1336;
  152. } else if (key == '3') {
  153. low = 697;
  154. high = 1477;
  155. } else if (key == '4') {
  156. low = 770;
  157. high = 1209;
  158. } else if (key == '5') {
  159. low = 770;
  160. high = 1336;
  161. } else if (key == '6') {
  162. low = 770;
  163. high = 1477;
  164. } else if (key == '7') {
  165. low = 852;
  166. high = 1209;
  167. } else if (key == '8') {
  168. low = 852;
  169. high = 1336;
  170. } else if (key == '9') {
  171. low = 852;
  172. high = 1477;
  173. } else if (key == '*') {
  174. low = 941;
  175. high = 1209;
  176. } else if (key == '0') {
  177. low = 941;
  178. high = 1336;
  179. } else if (key == '#') {
  180. low = 941;
  181. high = 1477;
  182. }
  183. // play the DTMF tones, if characters send from the Arduino Serial Monitor
  184. if (low > 0 && high > 0) {
  185. Serial.print("Output sound for key ");
  186. Serial.print(key);
  187. Serial.print(", low freq=");
  188. Serial.print(low);
  189. Serial.print(", high freq=");
  190. Serial.print(high);
  191. Serial.println();
  192. AudioNoInterrupts(); // disable audio library momentarily
  193. sine1.frequency(low);
  194. sine1.amplitude(0.4);
  195. sine2.frequency(high);
  196. sine2.amplitude(0.45);
  197. AudioInterrupts(); // enable, both tones will start together
  198. delay(100); // let the sound play for 0.1 second
  199. AudioNoInterrupts();
  200. sine1.amplitude(0);
  201. sine2.amplitude(0);
  202. AudioInterrupts();
  203. delay(50); // make sure we have 0.05 second silence after
  204. }
  205. }
  206. delay(25);
  207. }