【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主: eagler8

【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试

[复制链接]
 楼主| 发表于 2022-7-20 09:33 | 显示全部楼层
  【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试
  实验程序三:点亮LED—WS2812FX自动模式循环20种

  1. /*
  2.   【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试
  3.   实验程序三:点亮LED—WS2812FX自动模式循环20种
  4.   模块接线:WS2812B接D9
  5.   MAX9814   ESP32_C3
  6.   VCC          5V
  7.   GND         GND
  8.   OUT       D4(ADC4)
  9. */

  10. #include <WS2812FX.h>

  11. #define LED_COUNT 256
  12. #define LED_PIN    9

  13. #define TIMER_MS 5000

  14. WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);

  15. unsigned long last_change = 0;
  16. unsigned long now = 0;

  17. void setup() {
  18.   ws2812fx.init();
  19.   ws2812fx.setBrightness(28);
  20.   ws2812fx.setSpeed(1000);
  21.   ws2812fx.setColor(0x007BFF);
  22.   ws2812fx.setMode(FX_MODE_STATIC);
  23.   ws2812fx.start();
  24. }

  25. void loop() {
  26.   now = millis();
  27.   ws2812fx.service();
  28.   if(now - last_change > TIMER_MS) {
  29.     ws2812fx.setMode((ws2812fx.getMode() + 1) % ws2812fx.getModeCount());
  30.     last_change = now;
  31.   }
  32. }
复制代码


 楼主| 发表于 2022-7-20 09:53 | 显示全部楼层
实验场景图


17.jpg
 楼主| 发表于 2022-7-20 11:15 | 显示全部楼层
实验的视频记录


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


 楼主| 发表于 2022-7-20 20:40 | 显示全部楼层
  【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试
  实验程序四:MegunoLink音乐反应式LED灯带
  模块接线:WS2812B接D9
  MAX9814   ESP32_C3
  VCC          5V
  GND         GND
  OUT       D4(ADC4)

  1. /*
  2.   【花雕体验】20 音乐可视化:ESP32_C3与WS2812B的系列尝试
  3.   实验程序四:MegunoLink音乐反应式LED灯带
  4.   模块接线:WS2812B接D9
  5.   MAX9814   ESP32_C3
  6.   VCC          5V
  7.   GND         GND
  8.   OUT       D4(ADC4)
  9. */

  10. #include<FastLED.h>
  11. #include<MegunoLink.h>
  12. #include<Filter.h>

  13. // define necessary parameters
  14. #define N_PIXELS  24
  15. #define MIC_PIN   4
  16. #define LED_PIN   9
  17. // the following parameters can be tweaked according to your audio levels
  18. #define NOISE 150
  19. #define TOP   (N_PIXELS+2) // allow the max level to be slightly off scale
  20. #define LED_TYPE  WS2811
  21. #define BRIGHTNESS  18     // a little dim for recording purposes
  22. #define COLOR_ORDER GRB

  23. // declare the LED array
  24. CRGB leds[N_PIXELS];

  25. // define the variables needed for the audio levels
  26. int lvl = 0, minLvl = 0, maxLvl = 100; // tweak the min and max as needed

  27. // instantiate the filter class for smoothing the raw audio signal
  28. ExponentialFilter<long> ADCFilter(5,0);

  29. void setup() {
  30.   // put your setup code here, to run once:
  31.   Serial.begin(115200);
  32.   // initialize the LED object
  33.   FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,N_PIXELS).setCorrection(TypicalLEDStrip);
  34.   FastLED.setBrightness(BRIGHTNESS);
  35. }

  36. void loop() {
  37.   // put your main code here, to run repeatedly:
  38.   // read the audio signal and filter it
  39.   int n, height;
  40.   n = analogRead(MIC_PIN);
  41.   // remove the MX9614 bias of 1.25VDC
  42.   n = abs(1023 - n);
  43.   // hard limit noise/hum
  44.   n = (n <= NOISE) ? 0 : abs(n - NOISE);
  45.   // apply the exponential filter to smooth the raw signal
  46.   ADCFilter.Filter(n);
  47.   lvl = ADCFilter.Current();
  48. //  // plot the raw versus filtered signals
  49. //  Serial.print(n);
  50. //  Serial.print(" ");
  51. //  Serial.println(lvl);
  52.   // calculate the number of pixels as a percentage of the range
  53.   // TO-DO: can be done dynamically by using a running average of min/max audio levels
  54.   height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  55.   if(height < 0L) height = 0;
  56.   else if(height > TOP) height = TOP;
  57.   // turn the LEDs corresponding to the level on/off
  58.   for(uint8_t i = 0; i < N_PIXELS; i++) {
  59.     // turn off LEDs above the current level
  60.     if(i >= height) leds[i] = CRGB(0,0,0);
  61.     // otherwise, turn them on!
  62.     else leds[i] = Wheel( map( i, 0, N_PIXELS-1, 30, 150 ) );
  63.   }
  64.   FastLED.show();
  65. }

  66. CRGB Wheel(byte WheelPos) {
  67.   // return a color value based on an input value between 0 and 255
  68.   if(WheelPos < 85)
  69.     return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
  70.   else if(WheelPos < 170) {
  71.     WheelPos -= 85;
  72.     return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
  73.   } else {
  74.     WheelPos -= 170;
  75.     return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
  76.   }
  77. }
复制代码


 楼主| 发表于 2022-7-20 21:05 | 显示全部楼层
实验场景图  动态图


 楼主| 发表于 2022-7-20 21:50 | 显示全部楼层
本帖最后由 eagler8 于 2022-7-21 09:13 编辑

实验的视频记录(21秒)

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




 楼主| 发表于 2022-7-21 09:28 | 显示全部楼层
实验的视频记录(4分28秒)


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


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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 10:36 , Processed in 0.235176 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表