Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

256 linhas
7.7KB

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