【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百七十七:Wemos D1 R32 ESP32开发板
项目之四十七:快速傅里叶变换256位频谱仪
- /*
- 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
- 实验一百七十七:Wemos D1 R32 ESP32开发板
- 项目之四十七:快速傅里叶变换256位频谱仪
- */
-
- #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 39
- #define DATA_PIN 23
-
- 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(2);
- }
复制代码
|