要过年了,给大家一个好玩的,声音控制ws2812声音频谱
本帖最后由 810986815 于 2021-2-10 11:33 编辑要过年了,给大家一个好玩的,声音控制ws2812声音频谱,老外那里扒来的,得来不易,话不多说,直接上代码.
#include<FastLED.h>
#include<MegunoLink.h>
#include<Filter.h>
// define necessary parameters
#define N_PIXELS16//LED数量
#define MIC_PIN A0 //音频模块接口
#define LED_PIN 13 // LED灯带接口
// the following parameters can be tweaked according to your audio levels
#define NOISE 550
#define TOP (N_PIXELS+2) // allow the max level to be slightly off scale
#define LED_TYPEWS2811
#define BRIGHTNESS24 // a little dim for recording purposes
#define COLOR_ORDER GRB
// declare the LED array
CRGB leds;
// define the variables needed for the audio levels
int lvl = 0, minLvl = 0, maxLvl = 300; // tweak the min and max as needed
// instantiate the filter class for smoothing the raw audio signal
ExponentialFilter<long> ADCFilter(5,0);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// initialize the LED object
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,N_PIXELS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
// put your main code here, to run repeatedly:
// read the audio signal and filter it
int n, height;
n = analogRead(MIC_PIN);
// remove the MX9614 bias of 1.25VDC
n = abs(1023 - n);
// hard limit noise/hum
n = (n <= NOISE) ? 0 : abs(n - NOISE);
// apply the exponential filter to smooth the raw signal
ADCFilter.Filter(n);
lvl = ADCFilter.Current();
//// plot the raw versus filtered signals
//Serial.print(n);
//Serial.print(" ");
//Serial.println(lvl);
// calculate the number of pixels as a percentage of the range
// TO-DO: can be done dynamically by using a running average of min/max audio levels
height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
if(height < 0L) height = 0;
else if(height > TOP) height = TOP;
// turn the LEDs corresponding to the level on/off
for(uint8_t i = 0; i < N_PIXELS; i++) {
// turn off LEDs above the current level
if(i >= height) leds = CRGB(0,0,0);
// otherwise, turn them on!
else leds = Wheel( map( i, 0, N_PIXELS-1, 30, 150 ) );
}
FastLED.show();
}
CRGB Wheel(byte WheelPos) {
// return a color value based on an input value between 0 and 255
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);
}
}
开发板选8266 .有什么问题可以@我,下个帖子准备写8266无线升级和一个程序写入多个ID、APP上主界面图标上增加开关按钮,APP上增加声音和震动反馈。(如果有人感兴趣的话)希望版主给增加两个设备,有点不够用:lol
我比较好奇,他用的音频接口是放大之后的信号吗?我看他直接 analogRead了 用麦克风模块,接AO Arduino:1.8.13 (Windows 10), 开发板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"In file included from E:\Arduino\小示例\ws2812\ws2812.ino:1:0:C:\Users\11916\Documents\Arduino\libraries\FastLED\src/FastLED.h:14:21: note: #pragma message: FastLED version 3.004.000 # pragma message "FastLED version 3.004.000" ^In file included from C:\Users\11916\Documents\Arduino\libraries\FastLED\src/FastLED.h:65:0, from E:\Arduino\小示例\ws2812\ws2812.ino:1:C:\Users\11916\Documents\Arduino\libraries\FastLED\src/fastspi.h:135:23: note: #pragma message: No hardware SPI pins defined.All SPI access will default to bitbanged output # pragma message "No hardware SPI pins defined.All SPI access will default to bitbanged output" ^E:\Arduino\小示例\ws2812\ws2812.ino: In function 'void loop()':ws2812:57:26: error: incompatible types in assignment of 'CRGB' to 'CRGB 'ws2812:59:15: error: incompatible types in assignment of 'CRGB' to 'CRGB 'In file included from C:\Users\11916\Documents\Arduino\libraries\FastLED\src/FastLED.h:48:0, from E:\Arduino\小示例\ws2812\ws2812.ino:1:C:\Users\11916\Documents\Arduino\libraries\FastLED\src/fastpin.h: In instantiation of 'class FastPin<13u>':C:\Users\11916\Documents\Arduino\libraries\FastLED\src/platforms/esp/8266/clockless_esp8266.h:21:49: required from 'class ClocklessController<13, 26, 26, 52, (EOrder)66u, 0, false, 50>'C:\Users\11916\Documents\Arduino\libraries\FastLED\src/chipsets.h:570:7: required from 'class WS2811Controller800Khz<13u, (EOrder)66u>'C:\Users\11916\Documents\Arduino\libraries\FastLED\src/FastLED.h:111:52: required from 'class WS2811<13u, (EOrder)66u>'C:\Users\11916\Documents\Arduino\libraries\FastLED\src/FastLED.h:302:39: required from 'static CLEDController& CFastLED::addLeds(CRGB*, int, int) 'E:\Arduino\小示例\ws2812\ws2812.ino:29:62: required from hereC:\Users\11916\Documents\Arduino\libraries\FastLED\src/fastpin.h:210:2: error: static assertion failed: Invalid pin specified static_assert(validpin(), "Invalid pin specified"); ^exit status 1incompatible types in assignment of 'CRGB' to 'CRGB ' 在文件 -> 首选项开启 “编译过程中显示详细输出”选项 这份报告会包含更多信息。 错误信息:赋值中不兼容的类型咋回事?
本帖最后由 810986815 于 2021-2-25 08:43 编辑
开发板选择错误,不能选NodeMCU 1.0 ,要选8266 没有文件目录 chenrubao 发表于 2021-3-27 20:22
没有文件目录
exit status 1
FastLED.h: No such file or directory
810986815 发表于 2021-2-25 08:41
开发板选择错误,不能选NodeMCU 1.0 ,要选8266
楼主你用的是什么版本的,我也一直是不兼容的问题,开发板选8266还是出错
页:
[1]