Audio Spectrum Analyzer using ESP32, MSGEQ7 and TFT 1.8" display.
Code complies but spectrum does not decay. What is it that I am missing?
Any help will be appreciated.
```cpp
/*
BY: Juan Acevedo 9/25/2022
This PROGRAM needs work - does not decay the bands
Using: ESP32, MSGEQ7, TFT 1.8" Display
Code for the MSGEQ7 taken from public domain
Code for ESP32 and TFT 1.8" adapted from: SoundAnalyzer_ESP32_TTGO
*/
#include <TFT_eSPI.h>
TFT_eSPI display = TFT_eSPI();
// PINOUT FOR TFT 1.8" 128 x 160 DISPLAY
#define TFT_SDA 23
#define TFT_SCL 18
#define TFT_CS 5
#define TFT_DC 2
#define TFT_RST 4
// PINOUT FOR ESP32
int strobePin = 26;
int resetPin = 27;
int outPin = 34;
int level[7];
void setup(){
Serial.begin(115200);
display.init();
display.fillScreen(TFT_BLACK);
display.setRotation(1);
display.setTextColor(TFT_WHITE);
display.setTextSize(1);
display.drawString("Audio Spectrum Analyzer",10,5);
display.setCursor(0,0);
display.drawFastVLine(0,15,97, TFT_BLUE);
display.drawFastHLine(0,112,159,TFT_BLUE);
display.drawString("63 .16 .4 1 2.5 6 16K",5,120);
// define our pinModes
pinMode (resetPin, OUTPUT);
pinMode (strobePin, OUTPUT);
pinMode (outPin, INPUT);
// end of pinModes
// Create an initial state for our pins
digitalWrite (resetPin, LOW);
digitalWrite (strobePin, LOW);
delay(1);
// Reset the MSGEQ7 as per the datasheet timing diagram
digitalWrite(resetPin, HIGH);
delay(1);
digitalWrite(resetPin,LOW);
digitalWrite(strobePin,HIGH);
delay(1);
} // end of setup
void rectangleGR (int x0, int w, int h) {
const uint16_t colors[8] = {TFT_DARKGREEN, TFT_GREEN, TFT_GREENYELLOW, TFT_YELLOW,TFT_GOLD, TFT_ORANGE, TFT_RED, TFT_BROWN};
const int decal = 16;
int x = x0 * (w + 2);
int dh = (128 - decal) / 8;
for (int i = 1; i < 8; i++) {
if (h > i * dh) {
int y = 128 - decal - i * dh;
display.fillRect(x, y, w, dh, colors[i - 1]);
}else{
int y = 128 - decal - h;
display.fillRect(x, y, w, h - (i - 1) * dh, colors[i - 1]);
break;
}
}
}
void loop(){
// Cycle through each frequency band by pulsating the strobe
for ( int band = 0; band < 7; band++){
digitalWrite( strobePin, LOW);
delayMicroseconds(100); // Delay necessary due to timing diagram
level[band] = analogRead(outPin);
digitalWrite ( strobePin, HIGH);
delayMicroseconds(100);
}
// Affichage du spectre
for(int i = 0; i < 7; i++){
int amplitude = (int)level[i]/30;
amplitude = min(amplitude, 120);
rectangleGR (i, 20, amplitude+1);
}
}