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.

231 líneas
6.1KB

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