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.

141 linhas
3.6KB

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