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.

SpectrumAnalyzer.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SD.h>
  4. #include <LiquidCrystal.h>
  5. //const int myInput = AUDIO_INPUT_LINEIN;
  6. const int myInput = AUDIO_INPUT_MIC;
  7. // Create the Audio components. These should be created in the
  8. // order data flows, inputs/sources -> processing -> outputs
  9. //
  10. AudioInputI2S audioInput; // audio shield: mic or line-in
  11. AudioAnalyzeFFT256 myFFT(11);
  12. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  13. // Create Audio connections between the components
  14. //
  15. AudioConnection c1(audioInput, 0, audioOutput, 0);
  16. AudioConnection c2(audioInput, 0, myFFT, 0);
  17. AudioConnection c3(audioInput, 1, audioOutput, 1);
  18. // Create an object to control the audio shield.
  19. //
  20. AudioControlSGTL5000 audioShield;
  21. // Use the LiquidCrystal library to display the spectrum
  22. //
  23. LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
  24. byte bar1[8] = {0,0,0,0,0,0,0,255};
  25. byte bar2[8] = {0,0,0,0,0,0,255,255};
  26. byte bar3[8] = {0,0,0,0,0,255,255,255};
  27. byte bar4[8] = {0,0,0,0,255,255,255,255};
  28. byte bar5[8] = {0,0,0,255,255,255,255,255};
  29. byte bar6[8] = {0,0,255,255,255,255,255,255};
  30. byte bar7[8] = {0,255,255,255,255,255,255,255};
  31. byte bar8[8] = {255,255,255,255,255,255,255,255};
  32. void setup() {
  33. // Audio connections require memory to work. For more
  34. // detailed information, see the MemoryAndCpuUsage example
  35. AudioMemory(12);
  36. // Enable the audio shield and set the output volume.
  37. audioShield.enable();
  38. audioShield.inputSelect(myInput);
  39. audioShield.volume(60);
  40. lcd.begin(16, 2);
  41. lcd.print("Audio Spectrum");
  42. lcd.createChar(0, bar1);
  43. lcd.createChar(1, bar2);
  44. lcd.createChar(2, bar3);
  45. lcd.createChar(3, bar4);
  46. lcd.createChar(4, bar5);
  47. lcd.createChar(5, bar6);
  48. lcd.createChar(6, bar7);
  49. lcd.createChar(7, bar8);
  50. // pin 21 will select rapid vs animated display
  51. pinMode(21, INPUT_PULLUP);
  52. }
  53. int count=0;
  54. const int nsum[16] = {1, 1, 2, 2, 3, 4, 5, 6, 6, 8, 12, 14, 16, 20, 28, 24};
  55. int maximum[16];
  56. void loop() {
  57. if (myFFT.available()) {
  58. // convert the 128 FFT frequency bins
  59. // to only 16 sums, for a 16 character LCD
  60. int sum[16];
  61. int i;
  62. for (i=0; i<16; i++) {
  63. sum[i] = 0;
  64. }
  65. int n=0;
  66. int count=0;
  67. for (i=0; i<128; i++) {
  68. sum[n] = sum[n] + myFFT.output[i];
  69. count = count + 1;
  70. if (count >= nsum[n]) {
  71. Serial.print(count);
  72. Serial.print(" ");
  73. n = n + 1;
  74. if (n >= 16) break;
  75. count = 0;
  76. }
  77. }
  78. // The range is set by the audio shield's
  79. // knob, which connects to analog pin A1.
  80. int scale;
  81. scale = 2 + (1023 - analogRead(A1)) / 7;
  82. Serial.print(" - ");
  83. Serial.print(scale);
  84. Serial.print(" ");
  85. lcd.setCursor(0, 1);
  86. for (int i=0; i<16; i++) {
  87. // Reduce the range to 0-8
  88. int val = sum[i] / scale;
  89. if (val > 8) val = 8;
  90. // Compute an animated maximum, where increases
  91. // show instantly, but if the number is less that
  92. // the last displayed value, decrease it by 1 for
  93. // a slow decay (looks pretty)
  94. if (val >= maximum[i]) {
  95. maximum[i] = val;
  96. } else {
  97. if (maximum[i] > 0) maximum[i] = maximum[i] - 1;
  98. }
  99. // a switch on pin 22 select whether we show the
  100. // slower animation or the direct/fast data
  101. if (digitalRead(21) == HIGH) {
  102. val = maximum[i];
  103. }
  104. // print each custom digit
  105. if (val == 0) {
  106. lcd.write(' ');
  107. } else {
  108. lcd.write(val - 1);
  109. }
  110. Serial.print(sum[i]);
  111. Serial.print("=");
  112. Serial.print(val);
  113. Serial.print(",");
  114. }
  115. Serial.println();
  116. count = 0;
  117. }
  118. }