【花雕动手做】有趣好玩的音乐可视化系列项目(28)--LED乒乓球灯
项目程序之三:多彩MegunoLink音乐节拍灯
模块接线:WS2812B接D6
MAX4466 UNO
VCC 5V
GND GND
OUT A0
- /*
- 【花雕动手做】有趣好玩的音乐可视化系列项目(28)--LED乒乓球灯
- 项目程序之三:多彩MegunoLink音乐节拍灯
- 模块接线:WS2812B接D6
- MAX4466 UNO
- VCC 5V
- GND GND
- OUT A0
- */
-
- #include<FastLED.h>
- #include<MegunoLink.h>
- #include<Filter.h>
-
- #define N_PIXELS 101
- #define MIC_PIN A0
- #define LED_PIN 6
- #define NOISE 10
- #define TOP (N_PIXELS+2)
- #define LED_TYPE WS2811
- #define BRIGHTNESS 10
- #define COLOR_ORDER GRB
-
- CRGB leds[N_PIXELS];
- int lvl = 0, minLvl = 0, maxLvl = 10;
-
- ExponentialFilter<long> ADCFilter(5, 0);
-
- void setup() {
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, N_PIXELS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- }
-
- void loop() {
- int n, height;
- n = analogRead(MIC_PIN);
- n = abs(1023 - n);
- n = (n <= NOISE) ? 0 : abs(n - NOISE);
- ADCFilter.Filter(n);
- lvl = ADCFilter.Current();
- // Serial.print(n);
- // Serial.print(" ");
- // Serial.println(lvl);
- height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
- if (height < 0L) height = 0;
- else if (height > TOP) height = TOP;
- for (uint8_t i = 0; i < N_PIXELS; i++) {
- if (i >= height) leds[i] = CRGB(0, 0, 0);
- else leds[i] = Wheel( map( i, 0, N_PIXELS - 1, 30, 150 ) );
- }
- FastLED.show();
- }
-
- CRGB Wheel(byte WheelPos) {
- if (WheelPos < 85)
- return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
- else if (WheelPos < 170) {
- WheelPos -= 85;
- return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
- } else {
- WheelPos -= 170;
- return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- }
复制代码
|