257 satır
7.8KB

  1. // Dial Tone (DTMF) decoding example.
  2. //
  3. // The audio with dial tones is connected to analog input A2,
  4. // without using the audio shield. See the "DialTone_DTMF"
  5. // example for using the audio shield.
  6. //
  7. // This example code is in the public domain.
  8. #include <Audio.h>
  9. #include <Wire.h>
  10. #include <SPI.h>
  11. #include <SD.h>
  12. #include <SerialFlash.h>
  13. // Create the Audio components. These should be created in the
  14. // order data flows, inputs/sources -> processing -> outputs
  15. //
  16. AudioInputAnalog audioIn;
  17. AudioAnalyzeToneDetect row1; // 7 tone detectors are needed
  18. AudioAnalyzeToneDetect row2; // to receive DTMF dial tones
  19. AudioAnalyzeToneDetect row3;
  20. AudioAnalyzeToneDetect row4;
  21. AudioAnalyzeToneDetect column1;
  22. AudioAnalyzeToneDetect column2;
  23. AudioAnalyzeToneDetect column3;
  24. // Create Audio connections between the components
  25. //
  26. AudioConnection patchCord1(audioIn, 0, row1, 0);
  27. AudioConnection patchCord2(audioIn, 0, row2, 0);
  28. AudioConnection patchCord3(audioIn, 0, row3, 0);
  29. AudioConnection patchCord4(audioIn, 0, row4, 0);
  30. AudioConnection patchCord5(audioIn, 0, column1, 0);
  31. AudioConnection patchCord6(audioIn, 0, column2, 0);
  32. AudioConnection patchCord7(audioIn, 0, column3, 0);
  33. // pins where the 7 segment LEDs are connected
  34. const int sevenseg_a = 17; // aaa
  35. const int sevenseg_b = 9; // f b
  36. const int sevenseg_c = 11; // f b
  37. const int sevenseg_d = 12; // ggg
  38. const int sevenseg_e = 14; // e c
  39. const int sevenseg_f = 15; // e c
  40. const int sevenseg_g = 10; // ddd
  41. void setup() {
  42. // Audio connections require memory to work. For more
  43. // detailed information, see the MemoryAndCpuUsage example
  44. AudioMemory(3);
  45. //while (!Serial) ;
  46. //delay(100);
  47. // Configure the tone detectors with the frequency and number
  48. // of cycles to match. These numbers were picked for match
  49. // times of approx 30 ms. Longer times are more precise.
  50. row1.frequency(697, 21);
  51. row2.frequency(770, 23);
  52. row3.frequency(852, 25);
  53. row4.frequency(941, 28);
  54. column1.frequency(1209, 36);
  55. column2.frequency(1336, 40);
  56. column3.frequency(1477, 44);
  57. // The 7 segment display is "common anode), where the
  58. // common pin connects to +3.3V. LOW turns the LED on
  59. // and HIGH turns the LED off. If you use a common
  60. // cathode display, you will need to change all the HIGH
  61. // to LOW and LOW to HIGH.
  62. pinMode(sevenseg_a, OUTPUT);
  63. pinMode(sevenseg_b, OUTPUT);
  64. pinMode(sevenseg_c, OUTPUT);
  65. pinMode(sevenseg_d, OUTPUT);
  66. pinMode(sevenseg_e, OUTPUT);
  67. pinMode(sevenseg_f, OUTPUT);
  68. pinMode(sevenseg_g, OUTPUT);
  69. digitalWrite(sevenseg_a, HIGH);
  70. digitalWrite(sevenseg_b, HIGH);
  71. digitalWrite(sevenseg_c, HIGH);
  72. digitalWrite(sevenseg_d, HIGH);
  73. digitalWrite(sevenseg_e, HIGH);
  74. digitalWrite(sevenseg_f, HIGH);
  75. digitalWrite(sevenseg_g, HIGH);
  76. }
  77. const float row_threshold = 0.2;
  78. const float column_threshold = 0.2;
  79. void loop() {
  80. float r1, r2, r3, r4, c1, c2, c3;
  81. char digit=0;
  82. // read all seven tone detectors
  83. r1 = row1.read();
  84. r2 = row2.read();
  85. r3 = row3.read();
  86. r4 = row4.read();
  87. c1 = column1.read();
  88. c2 = column2.read();
  89. c3 = column3.read();
  90. // print the raw data, for troubleshooting
  91. Serial.print("tones: ");
  92. Serial.print(r1);
  93. Serial.print(", ");
  94. Serial.print(r2);
  95. Serial.print(", ");
  96. Serial.print(r3);
  97. Serial.print(", ");
  98. Serial.print(r4);
  99. Serial.print(", ");
  100. Serial.print(c1);
  101. Serial.print(", ");
  102. Serial.print(c2);
  103. Serial.print(", ");
  104. Serial.print(c3);
  105. // check all 12 combinations for key press
  106. if (r1 >= row_threshold) {
  107. if (c1 > column_threshold) {
  108. digit = '1';
  109. digitalWrite(sevenseg_a, HIGH);
  110. digitalWrite(sevenseg_b, LOW);
  111. digitalWrite(sevenseg_c, LOW);
  112. digitalWrite(sevenseg_d, HIGH);
  113. digitalWrite(sevenseg_e, HIGH);
  114. digitalWrite(sevenseg_f, HIGH);
  115. digitalWrite(sevenseg_g, HIGH);
  116. } else if (c2 > column_threshold) {
  117. digit = '2';
  118. digitalWrite(sevenseg_a, LOW);
  119. digitalWrite(sevenseg_b, LOW);
  120. digitalWrite(sevenseg_c, HIGH);
  121. digitalWrite(sevenseg_d, LOW);
  122. digitalWrite(sevenseg_e, LOW);
  123. digitalWrite(sevenseg_f, HIGH);
  124. digitalWrite(sevenseg_g, LOW);
  125. } else if (c3 > column_threshold) {
  126. digit = '3';
  127. digitalWrite(sevenseg_a, LOW);
  128. digitalWrite(sevenseg_b, LOW);
  129. digitalWrite(sevenseg_c, LOW);
  130. digitalWrite(sevenseg_d, LOW);
  131. digitalWrite(sevenseg_e, HIGH);
  132. digitalWrite(sevenseg_f, HIGH);
  133. digitalWrite(sevenseg_g, LOW);
  134. }
  135. } else if (r2 >= row_threshold) {
  136. if (c1 > column_threshold) {
  137. digit = '4';
  138. digitalWrite(sevenseg_a, HIGH);
  139. digitalWrite(sevenseg_b, LOW);
  140. digitalWrite(sevenseg_c, LOW);
  141. digitalWrite(sevenseg_d, HIGH);
  142. digitalWrite(sevenseg_e, HIGH);
  143. digitalWrite(sevenseg_f, LOW);
  144. digitalWrite(sevenseg_g, LOW);
  145. } else if (c2 > column_threshold) {
  146. digit = '5';
  147. digitalWrite(sevenseg_a, LOW);
  148. digitalWrite(sevenseg_b, HIGH);
  149. digitalWrite(sevenseg_c, LOW);
  150. digitalWrite(sevenseg_d, LOW);
  151. digitalWrite(sevenseg_e, HIGH);
  152. digitalWrite(sevenseg_f, LOW);
  153. digitalWrite(sevenseg_g, LOW);
  154. } else if (c3 > column_threshold) {
  155. digit = '6';
  156. digitalWrite(sevenseg_a, LOW);
  157. digitalWrite(sevenseg_b, HIGH);
  158. digitalWrite(sevenseg_c, LOW);
  159. digitalWrite(sevenseg_d, LOW);
  160. digitalWrite(sevenseg_e, LOW);
  161. digitalWrite(sevenseg_f, LOW);
  162. digitalWrite(sevenseg_g, LOW);
  163. }
  164. } else if (r3 >= row_threshold) {
  165. if (c1 > column_threshold) {
  166. digit = '7';
  167. digitalWrite(sevenseg_a, LOW);
  168. digitalWrite(sevenseg_b, LOW);
  169. digitalWrite(sevenseg_c, LOW);
  170. digitalWrite(sevenseg_d, HIGH);
  171. digitalWrite(sevenseg_e, HIGH);
  172. digitalWrite(sevenseg_f, HIGH);
  173. digitalWrite(sevenseg_g, HIGH);
  174. } else if (c2 > column_threshold) {
  175. digit = '8';
  176. digitalWrite(sevenseg_a, LOW);
  177. digitalWrite(sevenseg_b, LOW);
  178. digitalWrite(sevenseg_c, LOW);
  179. digitalWrite(sevenseg_d, LOW);
  180. digitalWrite(sevenseg_e, LOW);
  181. digitalWrite(sevenseg_f, LOW);
  182. digitalWrite(sevenseg_g, LOW);
  183. } else if (c3 > column_threshold) {
  184. digit = '9';
  185. digitalWrite(sevenseg_a, LOW);
  186. digitalWrite(sevenseg_b, LOW);
  187. digitalWrite(sevenseg_c, LOW);
  188. digitalWrite(sevenseg_d, LOW);
  189. digitalWrite(sevenseg_e, HIGH);
  190. digitalWrite(sevenseg_f, LOW);
  191. digitalWrite(sevenseg_g, LOW);
  192. }
  193. } else if (r4 >= row_threshold) {
  194. if (c1 > column_threshold) {
  195. digit = '*';
  196. digitalWrite(sevenseg_a, HIGH);
  197. digitalWrite(sevenseg_b, HIGH);
  198. digitalWrite(sevenseg_c, HIGH);
  199. digitalWrite(sevenseg_d, HIGH);
  200. digitalWrite(sevenseg_e, HIGH);
  201. digitalWrite(sevenseg_f, HIGH);
  202. digitalWrite(sevenseg_g, LOW);
  203. } else if (c2 > column_threshold) {
  204. digit = '0';
  205. digitalWrite(sevenseg_a, LOW);
  206. digitalWrite(sevenseg_b, LOW);
  207. digitalWrite(sevenseg_c, LOW);
  208. digitalWrite(sevenseg_d, LOW);
  209. digitalWrite(sevenseg_e, LOW);
  210. digitalWrite(sevenseg_f, LOW);
  211. digitalWrite(sevenseg_g, HIGH);
  212. } else if (c3 > column_threshold) {
  213. digit = '#';
  214. digitalWrite(sevenseg_a, HIGH);
  215. digitalWrite(sevenseg_b, LOW);
  216. digitalWrite(sevenseg_c, LOW);
  217. digitalWrite(sevenseg_d, HIGH);
  218. digitalWrite(sevenseg_e, LOW);
  219. digitalWrite(sevenseg_f, LOW);
  220. digitalWrite(sevenseg_g, LOW);
  221. }
  222. }
  223. // print the key, if any found
  224. if (digit > 0) {
  225. Serial.print(" --> Key: ");
  226. Serial.print(digit);
  227. }
  228. Serial.println();
  229. // uncomment these lines to see how much CPU time
  230. // the tone detectors and audio library are using
  231. //Serial.print("CPU=");
  232. //Serial.print(AudioProcessorUsage());
  233. //Serial.print("%, max=");
  234. //Serial.print(AudioProcessorUsageMax());
  235. //Serial.print("% ");
  236. delay(25);
  237. }