Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DialTone_7segment.ino 7.6KB

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