ESP8266 WS2812B 60灯珠拾音频谱灯渐变代码
时间: 2023-07-09 14:50:24 浏览: 165
8266-rgb_rgb8266_esp8266_ws2812rgb_WS2812_led5ad_
以下是一个基于ESP8266和WS2812B的60个LED灯珠的拾音频谱灯渐变代码示例:
```
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 5
#define MIC_PIN A0
CRGB leds[NUM_LEDS];
uint8_t spectrumValue[7];
uint8_t spectrumAvg;
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(64);
pinMode(MIC_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
uint16_t sample;
uint32_t sum = 0;
uint8_t peak = 0;
for (int i = 0; i < 7; i++) {
sample = analogRead(MIC_PIN);
sum += sample;
if (sample > peak) {
peak = sample;
}
}
spectrumAvg = sum / 7;
for (int i = 0; i < 7; i++) {
spectrumValue[i] = map(analogRead(MIC_PIN), 0, 1023, 0, 255);
}
for (int i = 0; i < NUM_LEDS; i++) {
uint8_t index = map(i, 0, NUM_LEDS, 0, 7);
uint8_t value = spectrumValue[index];
leds[i] = CHSV(map(value, 0, 255, 0, 160), 255, value);
}
FastLED.show();
FastLED.delay(10);
}
```
此代码使用FastLED库来控制WS2812B LED灯珠,并使用ESP8266的模拟输入引脚来读取麦克风的音频输入。代码读取音频数据并将其映射到LED灯珠的颜色上,产生拾音频谱灯效果。
阅读全文