18865浏览
楼主: 驴友花雕

[项目] 【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2021-12-15 20:10:22

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之五:快速哈特利变换FHT音乐反应灯板(8X8位WS2812硬屏)

  实验视频剪辑

https://v.youku.com/v_show/id_XN ... hcb.playlsit.page.1



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2021-12-16 11:04:32

  实验场景动态图

【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2021-12-16 11:34:13

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之六:快速哈特利变换FHT音乐反应灯板(8X32位WS2812硬屏)

  实验开源代码

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之六:快速哈特利变换FHT音乐反应灯板(8X32位 WS2812硬屏)
  4. */
  5. #define qsubd(x, b) ((x>b)?wavebright:0)                     // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  6. #define qsuba(x, b) ((x>b)?x-b:0)                            // Unsigned subtraction macro. if result <0, then => 0.
  7. #define wavebright 128    // qsubd result will be this value if subtraction is >0.
  8. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  9. #if FASTLED_VERSION < 3001000
  10. #error "Requires FastLED 3.1 or later; check github for latest code."
  11. #endif
  12. // Fixed definitions cannot change on the fly.
  13. #define LED_DT 6                                             // Data pin to connect to the strip.
  14. //#define LED_CK 11                                             // Clock pin for APA102 or WS2801
  15. #define COLOR_ORDER GRB                                       // It's GRB for WS2812
  16. #define LED_TYPE WS2812B                                       // What kind of strip are you using (APA102, WS2801 or WS2812B)
  17. #define NUM_LEDS 256                                       // Number of LED's.
  18. // Initialize changeable global variables.
  19. uint8_t max_bright = 255;                                     // Overall brightness definition. It can be changed on the fly.
  20. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  21. #define LOG_OUT 1
  22. #define FHT_N 256                                             // Set to 256 point fht.
  23. #define inputPin A0
  24. //#define potPin A4
  25. #include <FHT.h>                                              // FHT library
  26. uint8_t hueinc = 0;                                               // A hue increment value to make it rotate a bit.
  27. uint8_t micmult = 25;
  28. uint8_t fadetime = 900;
  29. uint8_t noiseval = 25;                                        // Increase this to reduce sensitivity. 30 seems best for quiet
  30. void setup() {
  31.   analogReference(EXTERNAL);                                  // Connect 3.3V to AREF pin for any microphones using 3.3V
  32.   Serial.begin(9600);                                        // use the serial port
  33.   FastLED.setBrightness (22);
  34.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  35.   //  LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);
  36.   FastLED.setBrightness(max_bright);
  37.   set_max_power_in_volts_and_milliamps(5, 300);               // FastLED Power management set at 5V, 500mA.
  38. }
  39. void loop() {
  40.   //    noiseval = map(analogRead(potPin), 0, 1023, 16, 48);          // Adjust sensitivity of cutoff.
  41.   EVERY_N_MILLISECONDS(13) {
  42.     fhtsound();
  43.   }
  44.   show_at_max_brightness_for_power();
  45.   Serial.println(LEDS.getFPS(), DEC);         // Display frames per second on the serial monitor.
  46.   Serial.println(" ");          // Display frames per second on the serial monitor.
  47.   Serial.println(analogRead(inputPin));       // print as an ASCII-encoded decimal         */
  48. }
  49. void fhtsound() {
  50.   // hueinc++;                                                   // A cute little hue incrementer.
  51.   GetFHT();                                                   // Let's take FHT_N samples and crunch 'em.
  52.   for (int i = 0; i < NUM_LEDS; i++) {                        // Run through the LED array.
  53.     int tmp = qsuba(fht_log_out[2 * i + 2], noiseval);       // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  54.     if (tmp > (leds[i].r + leds[i].g + leds[i].b) / 2)          // Refresh an LED only when the intensity is low
  55.       leds[i] = CHSV((i * 4) + tmp * micmult, 255, tmp * micmult); // Note how we really cranked up the tmp value to get BRIGHT LED's. Also increment the hue for fun.
  56.     leds[i].nscale8(fadetime);                                     // Let's fade the whole thing over time as well.
  57.   }
  58. } // fhtsound()
  59. void GetFHT() {
  60.   cli();
  61.   for (int i = 0 ; i < FHT_N ; i++) fht_input[i] = analogRead(inputPin);
  62.   sei();
  63.   fht_window();                                               // Window the data for better frequency response.
  64.   fht_reorder();                                              // Reorder the data before doing the fht.
  65.   fht_run();                                                  // Process the data in the fht.
  66.   fht_mag_log();
  67. } // GetFHT()
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2021-12-16 11:37:28

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之六:快速哈特利变换FHT音乐反应灯板(8X32位WS2812硬屏)

  实验视频剪辑

https://v.youku.com/v_show/id_XN ... hcb.playlsit.page.1



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2021-12-16 11:39:06

  实验场景动态图

【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)图1

回复

使用道具 举报

鹏3  见习技师

发表于 2021-12-17 10:11:35

#include <FHT.h> // include the library  这个库文件哪里下载呢
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2021-12-17 17:34:49

鹏3 发表于 2021-12-17 10:11
#include  // include the library  这个库文件哪里下载呢

在这里下载

https://github.com/Evg33/ArduinoFHT
回复

使用道具 举报

QQQQQQQ  初级技匠

发表于 2022-1-12 20:49:24

好厉害!!!!!!!!!!!!!!!!!!!!
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-1-13 06:20:29

QQQQQQQ 发表于 2022-1-12 20:49
好厉害!!!!!!!!!!!!!!!!!!!!

早上好,谢谢鼓励
回复

使用道具 举报

QQQQQQQ  初级技匠

发表于 2022-2-11 17:50:19


你也好。
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-2-12 21:56:09


晚上好,有空多交流
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-10 13:23:27

  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之七:基于虚拟轮生成颜色的音乐可视化(8X32位 WS2812硬屏)

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之七:基于虚拟轮生成颜色的音乐可视化(8X32位 WS2812硬屏)
  4. */
  5. #include <FastLED.h>
  6. // LED LIGHTING SETUP
  7. #define LED_PIN     6
  8. #define NUM_LEDS    480
  9. #define BRIGHTNESS  30
  10. #define LED_TYPE    WS2811
  11. #define COLOR_ORDER GRB
  12. CRGB leds[NUM_LEDS];
  13. #define UPDATES_PER_SECOND 100
  14. // AUDIO INPUT SETUP
  15. int audio = A0;
  16. // STANDARD VISUALIZER VARIABLES
  17. int loop_max = 0;
  18. int k = 255; // COLOR WHEEL POSITION
  19. int decay = 0; // HOW MANY MS BEFORE ONE LIGHT DECAY
  20. int decay_check = 0;
  21. long pre_react = 0; // NEW SPIKE CONVERSION
  22. long react = 0; // NUMBER OF LEDs BEING LIT
  23. long post_react = 0; // OLD SPIKE CONVERSION
  24. // RAINBOW WAVE SETTINGS
  25. int wheel_speed = 4;
  26. void setup()
  27. {
  28.   // LED LIGHTING SETUP
  29.   delay( 3000 ); // power-up safety delay
  30.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  31.   FastLED.setBrightness(  BRIGHTNESS );
  32.   // CLEAR LEDS
  33.   for (int i = 0; i < NUM_LEDS; i++)
  34.     leds[i] = CRGB(0, 0, 0);
  35.   FastLED.show();
  36.   // SERIAL AND INPUT SETUP
  37.   Serial.begin(115200);
  38.   pinMode(audio, INPUT);
  39.   Serial.println("\nListening...");
  40. }
  41. CRGB Scroll(int pos) {
  42.   CRGB color (0,0,0);
  43.   if(pos < 85) {
  44.     color.g = 0;
  45.     color.r = ((float)pos / 85.0f) * 255.0f;
  46.     color.b = 255 - color.r;
  47.   } else if(pos < 170) {
  48.     color.g = ((float)(pos - 85) / 85.0f) * 255.0f;
  49.     color.r = 255 - color.g;
  50.     color.b = 0;
  51.   } else if(pos < 256) {
  52.     color.b = ((float)(pos - 170) / 85.0f) * 255.0f;
  53.     color.g = 255 - color.b;
  54.     color.r = 1;
  55.   }
  56.   return color;
  57. }
  58. void rainbow(){
  59.   for(int i = NUM_LEDS - 1; i >= 0; i--) {
  60.     if (i < react)
  61.       leds[i] = Scroll((i * 256 / 50 + k) % 256);
  62.     else
  63.       leds[i] = CRGB(0, 0, 0);      
  64.   }
  65.   FastLED.show();
  66. }
  67. void loop()
  68. {
  69.   int audio_input = analogRead(audio); // ADD x2 HERE FOR MORE SENSITIVITY  
  70.   if (audio_input > 0)
  71.   {
  72.     pre_react = ((long)NUM_LEDS * (long)audio_input) / 1023L; // TRANSLATE AUDIO LEVEL TO NUMBER OF LEDs
  73.     if (pre_react > react) // ONLY ADJUST LEVEL OF LED IF LEVEL HIGHER THAN CURRENT LEVEL
  74.       react = pre_react;
  75.     Serial.print(audio_input);
  76.     Serial.print(" -> ");
  77.     Serial.println(pre_react);
  78.   }
  79.   rainbow(); // APPLY COLOR
  80.   k = k - wheel_speed; // SPEED OF COLOR WHEEL
  81.   if (k < 0) // RESET COLOR WHEEL
  82.     k = 255;
  83.   // REMOVE LEDs
  84.   decay_check++;
  85.   if (decay_check > decay)
  86.   {
  87.     decay_check = 0;
  88.     if (react > 0)
  89.       react--;
  90.   }
  91.   //delay(1);
  92. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-10 13:25:29

实验场景图  动态图

【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-10 17:22:59

  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之八:通过快速傅里叶变换在ws2812b8*8灯板上显示频谱

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之八:通过快速傅里叶变换在ws2812b8*8灯板上显示频谱
  4. */
  5. #include  "arduinoFFT.h"
  6. #include <FastLED.h>   
  7. #define NUM_LEDS 64   
  8. #define LED_TYPE WS2812
  9. #define COLOR_ORDER GRB
  10. arduinoFFT FFT = arduinoFFT();
  11. CRGB leds[NUM_LEDS];           
  12. #define CHANNEL A0
  13. #define DATA_PIN 6
  14. const uint8_t max_bright = 2;         
  15. const uint16_t samples = NUM_LEDS / 4;
  16. const byte halfsamples = samples / 2;  
  17. uint8_t gHue;                          
  18. int value;                             
  19. double vReal[samples];                 
  20. double vImag[samples];                 
  21. char toData[halfsamples];              
  22. int pointJump[halfsamples];
  23. int uJump[halfsamples];     
  24. int dJump[halfsamples];   
  25. int uValue;                 
  26. int dValue;                 
  27. int tValue;                 
  28. int toDown = 0;            
  29. uint8_t toDownSpeed = 3;   
  30. int pointDown = 0;         
  31. uint8_t pointDownSpeed = 9;
  32. void setup(){
  33.   delay(100);              
  34.   Serial.println("Ready");
  35.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  36.   FastLED.setBrightness(max_bright);
  37. }
  38. void loop(){
  39.   FastLED.clear();                        
  40.   EVERY_N_MILLISECONDS(10) {
  41.     gHue += 10;  
  42.   }
  43.   for (int i = 0; i < samples; i++)        
  44.   {
  45.     value = analogRead(CHANNEL);
  46.     vReal[i] = value;      
  47.     vImag[i] = 0.0;         
  48.   }
  49.   
  50.   FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  51.   FFT.Compute(vReal, vImag, samples, FFT_FORWARD);
  52.   FFT.ComplexToMagnitude(vReal, vImag, samples);
  53.   
  54.   for (int i = 0; i < halfsamples; i++)
  55.   {
  56.     toData[i] = vReal[i + halfsamples / 2];   
  57.     toData[i] = constrain(toData[i], 0, 100);
  58.     toData[i] = map(toData[i], 0, 100, 1, 7);
  59.   }
  60.   for (int i = 0; i < halfsamples; i++)
  61.   {
  62.     uValue = toData[i];   
  63.     uJump[i]++;            
  64.     if (uValue > uJump[i])
  65.     {
  66.       uValue = uJump[i];
  67.     }
  68.     else
  69.     {
  70.       uJump[i] = uValue;
  71.     }
  72.     dValue = uValue;
  73.     toDown++;                     
  74.     if (toDown % toDownSpeed == 0)
  75.     {
  76.       dJump[i]--;
  77.       toDown = 0;
  78.     }
  79.     if (dValue > pointJump[i])
  80.     {
  81.       dJump[i] = dValue;
  82.     }
  83.     else
  84.     {
  85.       dValue = dJump[i];
  86.     }
  87.     tValue = uValue;                     
  88.     pointDown++;                        
  89.     if (pointDown % pointDownSpeed == 0)
  90.     {
  91.       pointJump[i]--;
  92.       pointDown = 0;  
  93.     }
  94.     if (tValue > pointJump[i])
  95.     {
  96.       pointJump[i] = tValue;
  97.     }
  98.     else
  99.     {
  100.       tValue = pointJump[i];
  101.     }
  102.     fill_rainbow(leds + 8 * i, uValue, gHue, 30);
  103.     fill_rainbow(leds + 8 * i, dValue, gHue, 30);
  104.     fill_solid(leds + 8 * i + tValue, 1, CRGB::White);
  105.    
  106.   }
  107.   FastLED.show();
  108.   delay(10);      
  109. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-10 17:24:31

实验场景图  动态图

【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-10 18:32:01

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-20 13:42:28

  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之九:FastLED多彩音乐节奏屏灯

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之九:FastLED多彩音乐节奏屏灯
  4. */
  5. #include<FastLED.h>
  6. #include<MegunoLink.h>
  7. #include<Filter.h>
  8. // define necessary parameters
  9. #define N_PIXELS  64
  10. #define MIC_PIN   A0
  11. #define LED_PIN   6
  12. // the following parameters can be tweaked according to your audio levels
  13. #define NOISE 240
  14. #define TOP   (N_PIXELS+2) // allow the max level to be slightly off scale
  15. #define LED_TYPE  WS2811
  16. #define BRIGHTNESS  34     // a little dim for recording purposes
  17. #define COLOR_ORDER GRB
  18. // declare the LED array
  19. CRGB leds[N_PIXELS];
  20. // define the variables needed for the audio levels
  21. int lvl = 0, minLvl = 0, maxLvl = 300; // tweak the min and max as needed
  22. // instantiate the filter class for smoothing the raw audio signal
  23. ExponentialFilter<long> ADCFilter(5, 0);
  24. void setup() {
  25.   // put your setup code here, to run once:
  26.   // Serial.begin(115200);
  27.   // initialize the LED object
  28.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection(TypicalLEDStrip);
  29.   FastLED.setBrightness(BRIGHTNESS);
  30. }
  31. void loop() {
  32.   // put your main code here, to run repeatedly:
  33.   // read the audio signal and filter it
  34.   int n, height;
  35.   n = analogRead(MIC_PIN);
  36.   // remove the MX9614 bias of 1.25VDC
  37.   n = abs(1023 - n);
  38.   // hard limit noise/hum
  39.   n = (n <= NOISE) ? 0 : abs(n - NOISE);
  40.   // apply the exponential filter to smooth the raw signal
  41.   ADCFilter.Filter(n);
  42.   lvl = ADCFilter.Current();
  43.   //  // plot the raw versus filtered signals
  44.   //Serial.print(n);
  45.   //Serial.print(" ");
  46.   //Serial.println(lvl);
  47.   // calculate the number of pixels as a percentage of the range
  48.   // TO-DO: can be done dynamically by using a running average of min/max audio levels
  49.   height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  50.   if (height < 0L) height = 0;
  51.   else if (height > TOP) height = TOP;
  52.   // turn the LEDs corresponding to the level on/off
  53.   for (uint8_t i = 0; i < N_PIXELS; i++) {
  54.     // turn off LEDs above the current level
  55.     if (i >= height) leds[i] = CRGB(0, 0, 0);
  56.     // otherwise, turn them on!
  57.     else leds[i] = Wheel( map( i, 0, N_PIXELS - 1, 30, 150 ) );
  58.   }
  59.   FastLED.show();
  60. }
  61. CRGB Wheel(byte WheelPos) {
  62.   // return a color value based on an input value between 0 and 255
  63.   if (WheelPos < 85)
  64.     return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
  65.   else if (WheelPos < 170) {
  66.     WheelPos -= 85;
  67.     return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
  68.   } else {
  69.     WheelPos -= 170;
  70.     return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
  71.   }
  72. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-20 13:54:12

实验场景图  动态图

【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-7-20 14:00:22

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-5 09:56:59

【花雕动手做】看见声音,基于Arduino系列音乐可视器(1)---LED节奏灯
https://mc.dfrobot.com.cn/thread-311167-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(2)---OLED频谱灯
https://mc.dfrobot.com.cn/thread-311174-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(3)---RGB律动灯
https://mc.dfrobot.com.cn/thread-311183-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(4)---WS2812条灯
https://mc.dfrobot.com.cn/thread-311190-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(5)---WS2812柱跳灯
https://mc.dfrobot.com.cn/thread-311192-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(6)---点阵频谱灯
https://mc.dfrobot.com.cn/thread-311201-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(7)---大方格频谱灯
https://mc.dfrobot.com.cn/thread-311364-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(8)---四位32段点阵屏
https://mc.dfrobot.com.cn/thread-311490-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(9)---X Music Spectrum
https://mc.dfrobot.com.cn/thread-311627-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(10)---WS2812硬板屏
https://mc.dfrobot.com.cn/thread-311641-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(11)---WS2812幻彩灯带
https://mc.dfrobot.com.cn/thread-313648-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(12)---米管快速节奏灯
https://mc.dfrobot.com.cn/thread-313708-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(13)---有机棒立柱灯
https://mc.dfrobot.com.cn/thread-313723-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(14)---水杯水瓶灯
https://mc.dfrobot.com.cn/thread-313803-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(15)--横排LED方管灯
https://mc.dfrobot.com.cn/thread-313811-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(16)--热干胶棒棒灯
https://mc.dfrobot.com.cn/thread-313844-1-1.html
【花雕动手做】有趣好玩音乐可视化系列(17)--光导纤维灯
https://mc.dfrobot.com.cn/thread-313867-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(18)--LED平面板灯
https://mc.dfrobot.com.cn/thread-313951-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(19)--通体光纤灯
https://mc.dfrobot.com.cn/thread-313962-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(20)--首饰盒镜子灯
https://mc.dfrobot.com.cn/thread-313969-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(21)--CD 光盘灯
https://mc.dfrobot.com.cn/thread-313984-1-1.html
【花雕动手做】看见声音,基于Arduino系列音乐可视器(22)--LED无限魔方
https://mc.dfrobot.com.cn/thread-313994-1-1.html
【花雕动手做】有趣好玩的音乐可视化(23)--3合1闪点光纤
https://mc.dfrobot.com.cn/thread-314168-1-1.html
【花雕动手做】有趣好玩的音乐可视化(24)--无限LED镜子灯
https://mc.dfrobot.com.cn/thread-314180-1-1.html
【花雕动手做】有趣好玩音乐可视化(25)--水龙卷旋涡灯
https://mc.dfrobot.com.cn/thread-314231-1-1.html
【花雕动手做】有趣好玩音乐可视化系列(26)--LED 超立方体
https://mc.dfrobot.com.cn/thread-314244-1-1.html
【花雕动手做】有趣好玩的音乐可视化(27)--磁搅LED水旋灯
https://mc.dfrobot.com.cn/thread-314273-1-1.html


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-27 09:47:09

  【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之十:Arduino 和 FastLED多彩音乐灯

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之十:Arduino 和 FastLED多彩音乐灯
  4. */
  5. #include <FastLED.h>
  6. #define SAMPLEPERIODUS 200
  7. #define MIC_PIN A0
  8. #define LED_DT 6
  9. #define COLOR_ORDER GRB
  10. #define LED_TYPE WS2812
  11. #define NUM_LEDS 256
  12. uint8_t max_bright = 33;
  13. struct CRGB leds[NUM_LEDS];
  14. CRGBPalette16 currentPalette = RainbowColors_p;
  15. CRGBPalette16 targetPalette;
  16. void setup() {
  17.   pinMode(LED_BUILTIN, OUTPUT);
  18.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  19.   FastLED.setBrightness(max_bright);
  20. }
  21. float bassFilter(float sample) {
  22.   static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
  23.   xv[0] = xv[1]; xv[1] = xv[2];
  24.   xv[2] = sample / 9.1f;
  25.   yv[0] = yv[1]; yv[1] = yv[2];
  26.   yv[2] = (xv[2] - xv[0]) + (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
  27.   return yv[2];
  28. }
  29. float envelopeFilter(float sample) {
  30.   static float xv[2] = {0, 0}, yv[2] = {0, 0};
  31.   xv[0] = xv[1];
  32.   xv[1] = sample / 160.f;
  33.   yv[0] = yv[1];
  34.   yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
  35.   return yv[1];
  36. }
  37. float beatFilter(float sample) {
  38.   static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
  39.   xv[0] = xv[1]; xv[1] = xv[2];
  40.   xv[2] = sample / 7.015f;
  41.   yv[0] = yv[1]; yv[1] = yv[2];
  42.   yv[2] = (xv[2] - xv[0]) + (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
  43.   return yv[2];
  44. }
  45. void loop() {
  46.   unsigned long time = micros();
  47.   float sample, value, envelope, beat, thresh, micLev;
  48.   for (uint8_t i = 0; ; ++i) {
  49.     sample = (float)analogRead(MIC_PIN);
  50.     micLev = ((micLev * 67) + sample) / 68;
  51.     sample -= micLev;
  52.     value = bassFilter(sample);
  53.     value = abs(value);
  54.     envelope = envelopeFilter(value);
  55.     if (i == 200) {
  56.       beat = beatFilter(envelope);
  57.       thresh = 0.02f * 75.;
  58.       if (beat > thresh) {
  59.         digitalWrite(LED_BUILTIN, LOW);
  60.         int strt = random8(NUM_LEDS / 2);
  61.         int ende = strt + random8(NUM_LEDS / 2);
  62.         for (int i = strt; i < ende; i++) {
  63.           uint8_t index = inoise8(i * 30, millis() + i * 30);
  64.           leds[i] = ColorFromPalette(currentPalette, index, 255, LINEARBLEND);
  65.         }
  66.       } else {
  67.         digitalWrite(LED_BUILTIN, HIGH);
  68.       }
  69.       i = 0;
  70.     }
  71.     EVERY_N_SECONDS(5) {
  72.       uint8_t baseC = random8();
  73.       targetPalette = CRGBPalette16(CHSV(baseC + random8(32), 255, random8(128, 255)),
  74.                                     CHSV(baseC + random8(64), 255, random8(128, 255)),
  75.                                     CHSV(baseC + random8(64), 192, random8(128, 255)),
  76.                                     CHSV(baseC + random8(),   255, random8(128, 255)));
  77.     }
  78.     EVERY_N_MILLISECONDS(50) {
  79.       uint8_t maxChanges = 24;
  80.       nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);
  81.     }
  82.     EVERY_N_MILLIS(50) {
  83.       fadeToBlackBy(leds, NUM_LEDS, 64);
  84.       FastLED.show();
  85.     }
  86.     for (unsigned long up = time + SAMPLEPERIODUS; time > 20 && time < up; time = micros()) {  }
  87.   } // for i
  88. } // loop()
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail