您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

218 行
5.6KB

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