要过年了,给大家一个好玩的,声音控制ws2812声音频谱-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3950|回复: 8

[分享] 要过年了,给大家一个好玩的,声音控制ws2812声音频谱

[复制链接]
发表于 2021-2-10 11:25 | 显示全部楼层 |阅读模式
本帖最后由 810986815 于 2021-2-10 11:33 编辑

要过年了,给大家一个好玩的,声音控制ws2812声音频谱,老外那里扒来的,得来不易,话不多说,直接上代码.

#include<FastLED.h>
#include<MegunoLink.h>
#include<Filter.h>

// define necessary parameters
#define N_PIXELS  16  //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_TYPE  WS2811
#define BRIGHTNESS  24     // a little dim for recording purposes
#define COLOR_ORDER GRB

// declare the LED array
CRGB leds[N_PIXELS];

// 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上增加声音和震动反馈。(如果有人感兴趣的话)希望版主给增加两个设备,有点不够用

发表于 2021-2-10 13:31 | 显示全部楼层
我比较好奇,他用的音频接口是放大之后的信号吗?我看他直接 analogRead了
 楼主| 发表于 2021-2-10 14:49 来自手机 | 显示全部楼层
用麦克风模块,接AO
发表于 2021-2-24 21:38 | 显示全部楼层
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 [16]'  ws2812:59:15: error: incompatible types in assignment of 'CRGB' to 'CRGB [16]'  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) [with CHIPSET = WS2811; unsigned char DATA_PIN = 13u; EOrder RGB_ORDER = (EOrder)66u]'  E:\Arduino\小示例\ws2812\ws2812.ino:29:62:   required from here  C:\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 1  incompatible types in assignment of 'CRGB' to 'CRGB [16]'    在文件 -> 首选项开启 “编译过程中显示详细输出”选项 这份报告会包含更多信息。
发表于 2021-2-24 21:40 | 显示全部楼层
错误信息:赋值中不兼容的类型咋回事?
 楼主| 发表于 2021-2-25 08:41 | 显示全部楼层
本帖最后由 810986815 于 2021-2-25 08:43 编辑

开发板选择错误,不能选NodeMCU 1.0 ,要选8266
发表于 2021-3-27 20:22 | 显示全部楼层
没有文件目录
发表于 2021-3-27 20:22 | 显示全部楼层

exit status 1
FastLED.h: No such file or directory
发表于 2021-5-12 23:18 | 显示全部楼层
810986815 发表于 2021-2-25 08:41
开发板选择错误,不能选NodeMCU 1.0 ,要选8266

楼主你用的是什么版本的,我也一直是不兼容的问题,开发板选8266还是出错
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-28 13:46 , Processed in 0.070355 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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