You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 satır
5.6KB

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