[Hua Diao Experience] 17 Beetle ESP32C3 and WS2812 screen music visualization rhythm light
experiment program three: display spectrum on ws2812b light board through fast Fourier transform
/*
【花雕体验】17 Beetle ESP32C3与WS2812屏音乐可视化节奏灯
实验程序三:通过快速傅里叶变换在ws2812b灯板上显示频谱
*/
#include "arduinoFFT.h"
#include <FastLED.h>
#define NUM_LEDS 256
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
arduinoFFT FFT = arduinoFFT();
CRGB leds[NUM_LEDS];
#define CHANNEL A0
#define DATA_PIN 6
const uint8_t max_bright = 2;
const uint16_t samples = NUM_LEDS / 4;
const byte halfsamples = samples / 2;
uint8_t gHue;
int value;
double vReal[samples];
double vImag[samples];
char toData[halfsamples];
int pointJump[halfsamples];
int uJump[halfsamples];
int dJump[halfsamples];
int uValue;
int dValue;
int tValue;
int toDown = 0;
uint8_t toDownSpeed = 3;
int pointDown = 0;
uint8_t pointDownSpeed = 9;
void setup(){
delay(100);
Serial.println("Ready");
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(max_bright);
}
void loop(){
FastLED.clear();
EVERY_N_MILLISECONDS(10) {
gHue += 10;
}
for (int i = 0; i < samples; i++)
{
value = analogRead(CHANNEL);
vReal[i] = value;
vImag[i] = 0.0;
}
FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, samples);
for (int i = 0; i < halfsamples; i++)
{
toData[i] = vReal[i + halfsamples / 2];
toData[i] = constrain(toData[i], 0, 100);
toData[i] = map(toData[i], 0, 100, 1, 7);
}
for (int i = 0; i < halfsamples; i++)
{
uValue = toData[i];
uJump[i]++;
if (uValue > uJump[i])
{
uValue = uJump[i];
}
else
{
uJump[i] = uValue;
}
dValue = uValue;
toDown++;
if (toDown % toDownSpeed == 0)
{
dJump[i]--;
toDown = 0;
}
if (dValue > pointJump[i])
{
dJump[i] = dValue;
}
else
{
dValue = dJump[i];
}
tValue = uValue;
pointDown++;
if (pointDown % pointDownSpeed == 0)
{
pointJump[i]--;
pointDown = 0;
}
if (tValue > pointJump[i])
{
pointJump[i] = tValue;
}
else
{
tValue = pointJump[i];
}
fill_rainbow(leds + 8 * i, uValue, gHue, 30);
fill_rainbow(leds + 8 * i, dValue, gHue, 30);
fill_solid(leds + 8 * i + tValue, 1, CRGB::White);
}
FastLED.show();
delay(10);
}
|