Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DialTone_7segment.ino 7.6KB

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