数码之家

 找回密码
 立即注册
搜索
查看: 2476|回复: 61

[影音] 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏

[复制链接]
发表于 2021-12-12 18:17:35 | 显示全部楼层 |阅读模式
偶然心血来潮,想要做一个声音可视化的系列专题。这个专题的难度有点高,涉及面也比较广泛,相关的FFT和FHT等算法也相当复杂,不过还是打算从最简单的开始,实际动手做做试验,耐心尝试一下各种方案,逐步积累些有用的音乐频谱可视化的资料,也会争取成型一些实用好玩的音乐可视器项目。

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2021-12-13 12:41:52 | 显示全部楼层
这么多灯,不便宜啊,现成的全彩屏好玩一些。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 08:52:10 | 显示全部楼层
flishmen 发表于 2021-12-13 12:41
这么多灯,不便宜啊,现成的全彩屏好玩一些。

什么样的全彩屏啊,有频谱的驱动软件吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 09:16:49 | 显示全部楼层
Arduino 系列传感器和执行器模块实验目录清单:
【花雕动手做】有趣好玩的音乐可视化项目(01)---LED节奏灯
https://www.mydigit.cn/thread-278513-1-1.html
【花雕动手做】有趣好玩的音乐可视化项目(02)---OLED频谱灯
https://www.mydigit.cn/thread-278659-1-1.html
【花雕动手做】有趣好玩的音乐可视化项目(03)---RGB律动灯
https://www.mydigit.cn/thread-278910-1-1.html
【花雕动手做】有趣好玩的音乐可视化项目(04)---WS2812条灯
https://www.mydigit.cn/thread-279138-1-1.html
【花雕动手做】有趣好玩的音乐可视化项目(05)---WS2812柱跳灯
https://www.mydigit.cn/thread-279227-1-1.html
【花雕动手做】有趣好玩的音乐可视化项目(06)---点阵频谱灯
https://www.mydigit.cn/thread-279412-1-1.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(07)---大方格频谱灯
https://www.mydigit.cn/thread-283363-1-1.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(08)---四位32段点阵屏
https://www.mydigit.cn/thread-286035-1-1.html
【花雕动手做】有趣好玩音乐可视化项目(09)--X Music Spectrum
https://www.mydigit.cn/thread-289124-1-1.html
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
https://www.mydigit.cn/thread-289553-1-1.html


【花雕动手做】用快餐盒盖搭建极低成本的多种运动方式实验平台/机器人
https://www.mydigit.cn/thread-227134-1-1.html
【花雕动手做】超低成本,尝试做个五十元的麦克纳姆轮小车!
https://www.mydigit.cn/thread-220131-1-1.html
【花雕动手做】太搞笑啦,一支胶管制成二只蠕动机器人
https://www.mydigit.cn/thread-226476-1-1.html
【花雕动手做】超迷你哦,用徽商香烟盒做个智能小车!
https://www.mydigit.cn/thread-222085-1-1.html
【花雕动手做】五花八门的简易机器人66种
https://www.mydigit.cn/thread-221775-1-1.html
【花雕动手做】特别苗条,使用微波人体感应模块控制的纤细机器人小车
https://www.mydigit.cn/forum.php?mod=viewthread&tid=244463
素人学习Aduino编程的简单办法
https://www.mydigit.cn/forum.php?mod=viewthread&tid=34316
Arduino 语法手册的五个版本
https://www.mydigit.cn/thread-35089-1-1.html
ESP8266 Arduino开发环境搭建的五个备选地址
https://www.mydigit.cn/thread-40541-1-1.html
只用一块扩展板完成Arduino的9类37项实验(代码+图形+仿真)
https://www.mydigit.cn/thread-44343-1-1.html

37种传感器实验(Arduino)记录每一次小小的进步
https://www.mydigit.cn/thread-35315-1-1.html
实验一百一十五: 震动马达模块 高低电平振动小电机 智能可穿戴式产品震动提醒模块
https://www.mydigit.cn/thread-76804-1-1.html
实验一百一十六:NRF24L01+ 无线模块 功率加强版 2.4G无线收发通信模块 黑金刚
https://www.mydigit.cn/thread-76843-1-1.html
实验一百一十七:蓝色诺基亚5110屏 LCD液晶屏模块 兼容3310 LCD
https://www.mydigit.cn/thread-77142-1-1.html
实验一百一十八:US-015 高分辨超声波测距模块 超声波传感器 US-020升级版
https://www.mydigit.cn/thread-77594-1-1.html
实验一百一十九:HB100微波雷达感应模块 10.525GHz多普勒探测器探头传感器
https://www.mydigit.cn/thread-78082-1-1.html
实验一百二十: SIM800L GPRS 转接板GSM模块 microSIM卡带天线4频通讯模块
https://www.mydigit.cn/thread-75498-1-1.html
实验一百二十一:五向导航按键模块 5D摇杆 单片机 独立键盘开关 按钮 Your Cee
https://www.mydigit.cn/thread-78274-1-1.html
实验一百二十二: BBC microbit micro:bit,bit go,bit club python图形编程开发板
https://www.mydigit.cn/thread-76145-1-1.html
实验一百二十三: P10单红V706模组  32*16单元板LED显示屏
https://www.mydigit.cn/thread-78460-1-1.html
实验一百二十四: PS2鼠标 键盘插座接口 PS2模块
https://www.mydigit.cn/thread-78589-1-1.html
实验一百二十五: 升级版 WeMos D1 R2 WiFi UNO 开发板 基于ESP8266
https://www.mydigit.cn/thread-78843-1-1.html
实验一百二十六:JQ6500语音模块 MP3模块 MCU串口控制播报 一对一5路控制 音乐IC
https://www.mydigit.cn/thread-79043-1-1.html
实验一百二十七:JoyStick Shield PS2游戏摇杆扩展板 模拟键盘鼠标功能 大摇杆
https://www.mydigit.cn/thread-79290-1-1.html
实验一百二十八:日本夏普SHARP PM2.5 灰尘/粉尘传感器 GP2Y1014AU0F 带线
https://www.mydigit.cn/thread-79385-1-1.html
实验一百二十九:GY-302 数字量光强度检测 光照传感器 BH1750FVI 光线检测模块
https://www.mydigit.cn/thread-79661-1-1.html
实验一百三十:XD-203多功能扩展板Multi-function Shield模块(12合1版)
https://www.mydigit.cn/thread-79777-1-1.html
实验一百三十一:24位 WS2812 5050 RGB LED 内置全彩驱动彩灯 圆形开发板
https://www.mydigit.cn/thread-80667-1-1.html
实验一百三十二:WT588D语音模块 语音芯片 可反复擦写高音质语音下载16P-16M 蓝板HW-708
https://www.mydigit.cn/thread-81018-1-1.html
实验一百三十三:夏普GP2Y0A02YK0F 红外测距传感器模块 20-150cm 距离传感器
https://www.mydigit.cn/thread-81360-1-1.html
实验一百三十四:2004A液晶屏 20X4 J204A字符显示液晶模块 204A LCD/LCM 蓝屏5V(带背光 IIC/I2C)
https://www.mydigit.cn/thread-81914-1-1.html
实验一百三十五:ESP8266机智云开发板 ESP12F wifi模块(支持云端的智能硬件开发套件)
https://www.mydigit.cn/thread-82229-1-1.html
实验一百三十六:基于STM32F103RCT6及多种传感器的UFUN2.0升级开发板
https://www.mydigit.cn/thread-100457-1-1.html
实验一百三十七:IIC I2C电平转换模块 5-3v系统兼容 2路
https://www.mydigit.cn/thread-121468-1-1.html
实验一百三十八:中文TTS文本转语音合成模块 替代SYN6288和XFS5152
https://www.mydigit.cn/thread-121781-1-1.html
实验一百三十九:电流互感器模块 5A量程 单相 交流电流传感器模块
https://www.mydigit.cn/thread-122217-1-1.html
实验一百四十:YL-004老款20按键独立键盘 跑马灯矩阵键盘模块
(4*4矩阵键盘+4位独立按键+8位跑马灯组合模块)
https://www.mydigit.cn/thread-122307-1-1.html
实验一百四十一:KY-027 魔术光杯 for UNO 魔术光环模块
https://www.mydigit.cn/thread-122579-1-1.html
实验一百四十二:LCD1602 字符液晶屏 输入输出扩展板 LCD Keypad Shield
https://www.mydigit.cn/thread-123105-1-1.html
实验一百四十三:I2C接口I/O扩展模块 支持级联PCF8574T模块
https://www.mydigit.cn/thread-127953-1-1.html
实验一百四十四:Ethernet W5100S 网络扩展板 SD卡扩展模块 支持MEGA
https://www.mydigit.cn/thread-130504-1-1.html
实验一百四十五:0.91寸OLED液晶屏显示模块 IIC 12832液晶屏 兼容3.3v-5V
https://www.mydigit.cn/thread-124905-1-1.html
实验一百四十六:MT8870 DTMF语音解码板模块 电话拨号控制音频解码处理电路
https://www.mydigit.cn/thread-128475-1-1.html
实验一百四十七:64位 WS2812B 8x8 RGB 5050 LED模块 ws2812s像素点阵屏
https://www.mydigit.cn/thread-127681-1-1.html
实验一百四十八:E18-D80NK 漫反射式红外光电开关避障传感器模块
https://www.mydigit.cn/thread-126831-1-1.html
实验一百四十九:MAX3232电平转TTL电平转换板 mini RS232 MCU串口转换模块
https://www.mydigit.cn/thread-126424-1-1.html
实验一百五十:AS608光学指纹识别模块 智能锁/考勤门禁开发/指纹采集模块
https://www.mydigit.cn/thread-124272-1-1.html
实验一百五十一:ESP-32掌控板 盛思 MicroPython微控制器开发板
https://www.mydigit.cn/thread-136590-1-1.html
实验一百五十二:GY-25 串口直接输出角度数据 倾斜度角度传感器模块 MPU-6050
https://www.mydigit.cn/thread-218333-1-1.html
实验一百五十三:Maixduino AI开发板 k210 RISC-V AI+lOT ESP32视觉听觉开发套装
https://www.mydigit.cn/thread-129551-1-1.html
实验一百五十四:ML8511紫外线传感器模块  模拟量输出UV Sensor Breakou
https://www.mydigit.cn/thread-143285-1-1.html
实验一百五十五:KA2284 电平指示模块 5点LED电量/音频音乐旋律显示器
https://www.mydigit.cn/thread-223567-1-1.html
实验一百五十六:恒压型LED无级触摸调光PWM控制板 电容式触摸开关器模块
https://www.mydigit.cn/thread-223681-1-1.html
实验一百五十七:SG3525 PWM控制器模块 频率可调 占空比可调 波形发生器
https://www.mydigit.cn/thread-129257-1-1.html
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
https://www.mydigit.cn/thread-230781-1-1.html
实验一百五十九:MAX9814麦克风放大器模块 MIC话筒声音放大/咪头传感器 CMA-4544PF-W
https://www.mydigit.cn/thread-129060-1-1.html
实验一百六十二:S12SD紫外线传感器模块太阳光强度检测传感器模块高灵敏LM358放大
https://www.mydigit.cn/thread-143194-1-1.html
实验一百六十四:Seeeduino Lotus开发板 传感器9件套 主板+扩展板二合一https://www.mydigit.cn/thread-141403-1-1.html
实验一百七十:L293D四路电机驱动模块 扩展板 motor control shield 马达板
https://www.mydigit.cn/thread-205585-1-1.html
实验一百七十一:HLK-V20 AI智能离线语音识别模块 150条本地指令离线识别
https://www.mydigit.cn/thread-206726-1-1.html
实验一百七十二:MX1508 四通道双路有刷直流马达驱动模块 2路直流电机驱动模块 双H桥步进电机 正反转PWM调速
https://www.mydigit.cn/thread-216710-1-1.html
实验一百七十三:2262/2272四路无线遥控套件 M4非锁接收板 四键无线遥控器发射模块
https://www.mydigit.cn/thread-224877-1-1.html
实验一百七十四:DRV8833 电机驱动板模块 小体积高性能 2路直流电机驱动板 自平衡小车 代替TB6612FNG
https://www.mydigit.cn/thread-220662-1-1.html
实验一百七十五:LD3320 语音识别模块 非特定人声 语音控制 智能家居开发板
https://www.mydigit.cn/thread-225247-1-1.html
实验一百七十九:0.66英寸OLED显示模块 液晶屏模块IIC/I2C接口 64*48像素 SSD1306驱动芯片
https://www.mydigit.cn/thread-278724-1-1.html
实验二百零一:OPT101模拟光照传感器 TEMT6000光强度模块 单片光电二极管 YourCee
https://www.mydigit.cn/thread-278450-1-1.html
实验二百零七:I2C红色8*8LED点阵模块ht16k33驱动1088BS树莓派物联网可扩展编程
https://www.mydigit.cn/thread-274979-1-1.html
实验二百一十七:EPD Eink 电子墨水显示器 SUNY 2.9 英寸 EPD 屏幕无线数字电子纸显示器模块  DEPG0290RWS800F6黑白红三色墨水屏(珠海三一科技)
https://www.mydigit.cn/thread-282165-1-1.html

回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 10:30:32 | 显示全部楼层


WS2812B主要特点
智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电复位和掉电复位电路。
每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串行级联接口,能通过一根信号线完成数据的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
数据发送速度可达800Kbps。
光的颜色高度一致,性价比高。

主要应用领域
LED全彩发光字灯串,LED全彩模组, LED全彩软灯条硬灯条,LED护栏管。
LED点光源,LED像素屏,LED异形屏,各种电子产品,电器设备跑马灯。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 10:36:08 | 显示全部楼层
WS2812模块电原理图



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 10:46:21 | 显示全部楼层
MAX9814
是一款低成本高性能麦克风放大器,具有自动增益控制(AGC)和低噪声麦克风偏置。器件具有低噪声前端放大器、可变增益放大(VGA)、输出放大器、麦克风偏置电压发生器和AGC控制电路。
●自动增益控制(AGC)
●3种增益设置(40dB、50dB、60dB)
●可编程动作时间
●可编程动作和释放时间比
●电源电压范围2.7V~5.5V  
●低THD:0.04% (典型值)
●低功耗关断模式
●内置2V低噪声麦克风偏置



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 20:33:31 | 显示全部楼层
搜索并安装Adafruit_NeoPixel库:

https://github.com/adafruit/Adafruit_NeoPixel



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-14 20:40:19 | 显示全部楼层
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之一:使用Adafruit_NeoPixel库的音乐可视化多彩节奏灯

  实验开源代码

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之一:使用Adafruit_NeoPixel库的音乐可视化多彩节奏灯
  4. */

  5. #include <Adafruit_NeoPixel.h>
  6. #define MIC A0 // 麦克风与A0相连接
  7. #define LED_PIN 6 // LED are connected to D6
  8. #define N_PIXELS 16 // Number of LED
  9. #define N 100 //样本数
  10. #define fadeDelay 10 // 淡出量
  11. #define noiseLevel 40 // 降噪下限

  12. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

  13. int samples[N]; // 存储样本
  14. int periodFactor = 0; // 用于周期计算
  15. int t1 = -1;
  16. int T;
  17. int slope;
  18. byte periodChanged = 0;

  19. void setup() {
  20.   // Serial.begin(9600);
  21.   strip.begin();
  22.   ledsOff();
  23.   delay(500);
  24.   displayColor(Wheel(100));
  25.   strip.show();
  26.   delay(500);
  27. }

  28. void loop() {
  29.   Samples();
  30. }

  31. void Samples() {
  32.   for (int i = 0; i < N; i++) {
  33.     samples[i] = analogRead(0);
  34.     if (i > 0) {
  35.       slope = samples[i] - samples[i - 1];
  36.     }
  37.     else {
  38.       slope = samples[i] - samples[N - 1];
  39.     }

  40.     if (abs(slope) > noiseLevel) {
  41.       if (slope < 0) {
  42.         calculatePeriod(i);
  43.         if (periodChanged == 1) {
  44.           displayColor(getColor(T));
  45.         }
  46.       }
  47.     }
  48.     else {
  49.       ledsOff();
  50.     }
  51.     periodFactor += 1;
  52.     delay(1);
  53.   }
  54. }

  55. void calculatePeriod(int i) {
  56.   if (t1 == -1) {

  57.     t1 = i;
  58.   }
  59.   else {

  60.     int period = periodFactor * (i - t1);
  61.     periodChanged = T == period ? 0 : 1;
  62.     T = period;
  63.     // Serial.println(T);

  64.     t1 = i;
  65.     periodFactor = 0;
  66.   }
  67. }

  68. uint32_t getColor(int period) {
  69.   if (period == -1)
  70.     return Wheel(0);
  71.   else if (period > 400)
  72.     return Wheel(5);
  73.   else
  74.     return Wheel(map(-1 * period, -400, -1, 50, 255));
  75. }

  76. void fadeOut()
  77. {
  78.   for (int i = 0; i < 5; i++) {
  79.     strip.setBrightness(110 - i * 20);
  80.     strip.show(); // Update strip
  81.     delay(fadeDelay);
  82.     periodFactor += fadeDelay;
  83.   }
  84. }

  85. void fadeIn() {
  86.   strip.setBrightness(100);
  87.   strip.show();

  88.   for (int i = 0; i < 5; i++) {
  89.     //strip.setBrightness(20*i + 30);
  90.     //strip.show();
  91.     delay(fadeDelay);
  92.     periodFactor += fadeDelay;
  93.   }
  94. }

  95. void ledsOff() {
  96.   fadeOut();
  97.   for (int i = 0; i < N_PIXELS; i++) {
  98.     strip.setPixelColor(i, 0, 0, 0);
  99.   }
  100. }

  101. void displayColor(uint32_t color) {
  102.   for (int i = 0; i < N_PIXELS; i++) {
  103.     strip.setPixelColor(i, color);
  104.   }
  105.   fadeIn();
  106. }

  107. uint32_t Wheel(byte WheelPos) {
  108.   // Serial.println(WheelPos);
  109.   if (WheelPos < 85) {

  110.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  111.   }
  112.   else if (WheelPos < 170) {
  113.     WheelPos -= 85;
  114.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  115.   }
  116.   else {
  117.     WheelPos -= 170;
  118.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  119.   }
  120. }
复制代码


回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 08:06:10 | 显示全部楼层
实验场景图


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 08:46:04 | 显示全部楼层

【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之一:使用Adafruit_NeoPixel库的多彩节奏灯

   实验视频剪辑

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

回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 08:49:01 | 显示全部楼层
  实验场景动态图



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

发表于 2021-12-15 10:54:07 | 显示全部楼层

动次打次动次打次 哈哈哈
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 11:56:49 | 显示全部楼层
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之二:音乐反应式 LED 灯板(4x4位)

  实验开源代码

  1. /*
  2. 【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之二:音乐反应式 LED 灯板
  4. */

  5. #include <Adafruit_NeoPixel.h>
  6. #include <math.h>
  7. #define N_PIXELS  16
  8. #define MIC_PIN   A0
  9. #define LED_PIN    6
  10. #define SAMPLE_WINDOW  4
  11. #define PEAK_HANG 24
  12. #define PEAK_FALL 4
  13. #define INPUT_FLOOR 10
  14. #define INPUT_CEILING 50
  15. byte peak = 16;
  16. unsigned int sample;

  17. byte Count = 0;
  18. byte HangCount = 0;

  19. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);

  20. void setup() {
  21.   Serial.begin(9600);
  22.   analogReference(EXTERNAL);
  23.   strip.setBrightness(22);
  24.   strip.show();
  25.   strip.begin();
  26. }

  27. float fscale( float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {

  28.   float OriginalRange = 0;
  29.   float NewRange = 0;
  30.   float zeroRefCurVal = 0;
  31.   float normalizedCurVal = 0;
  32.   float rangedValue = 0;
  33.   boolean invFlag = 0;

  34.   if (curve > 10) curve = 10;
  35.   if (curve < -10) curve = -10;

  36.   curve = (curve * -.1) ;
  37.   curve = pow(10, curve);

  38.   if (inputValue < originalMin) {
  39.     inputValue = originalMin;
  40.   }
  41.   if (inputValue > originalMax) {
  42.     inputValue = originalMax;
  43.   }

  44.   OriginalRange = originalMax - originalMin;

  45.   if (newEnd > newBegin) {
  46.     NewRange = newEnd - newBegin;
  47.   }
  48.   else
  49.   {
  50.     NewRange = newBegin - newEnd;
  51.     invFlag = 1;
  52.   }

  53.   zeroRefCurVal = inputValue - originalMin;
  54.   normalizedCurVal  =  zeroRefCurVal / OriginalRange;   // normalize to 0 - 1 float

  55.   Serial.print(OriginalRange, DEC);
  56.   Serial.print("   ");
  57.   Serial.print(NewRange, DEC);
  58.   Serial.print("   ");
  59.   Serial.println(zeroRefCurVal, DEC);
  60.   Serial.println();
  61.   delay(10);

  62.   if (originalMin > originalMax ) {
  63.     return 0;
  64.   }

  65.   if (invFlag == 0) {
  66.     rangedValue =  (pow(normalizedCurVal, curve) * NewRange) + newBegin;
  67.   }
  68.   else
  69.   {
  70.     rangedValue =  newBegin - (pow(normalizedCurVal, curve) * NewRange);
  71.   }
  72.   return rangedValue;
  73. }

  74. void loop() {
  75.   unsigned long startMillis = millis();
  76.   float peakToPeak = 0;

  77.   unsigned int signalMax = 0;
  78.   unsigned int signalMin = 1023;
  79.   unsigned int c, y;

  80.   while (millis() - startMillis < SAMPLE_WINDOW)
  81.   {
  82.     sample = analogRead(MIC_PIN);
  83.     if (sample < 1024)
  84.     {
  85.       if (sample > signalMax)
  86.       {
  87.         signalMax = sample;
  88.       }
  89.       else if (sample < signalMin)
  90.       {
  91.         signalMin = sample;
  92.       }
  93.     }
  94.   }
  95.   peakToPeak = signalMax - signalMin;

  96.   for (int i = 0; i <= strip.numPixels() - 1; i++) {
  97.     strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
  98.   }

  99.   c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);

  100.   if (c < peak) {
  101.     peak = c;
  102.     HangCount = 0;
  103.   }
  104.   if (c <= strip.numPixels()) {
  105.     drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));
  106.   }

  107.   y = strip.numPixels() - peak;
  108.   strip.setPixelColor(y - 1, Wheel(map(y, 0, strip.numPixels() - 1, 30, 150)));
  109.   strip.show();

  110.   if (HangCount > PEAK_HANG) {
  111.     if (++Count >= PEAK_FALL) {
  112.       peak++;
  113.       Count = 0;
  114.     }
  115.   }
  116.   else {
  117.     HangCount++;
  118.   }
  119. }

  120. void drawLine(uint8_t from, uint8_t to, uint32_t c) {
  121.   uint8_t fromTemp;
  122.   if (from > to) {
  123.     fromTemp = from;
  124.     from = to;
  125.     to = fromTemp;
  126.   }
  127.   for (int i = from; i <= to; i++) {
  128.     strip.setPixelColor(i, c);
  129.   }
  130. }

  131. uint32_t Wheel(byte WheelPos) {
  132.   if (WheelPos < 85) {
  133.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  134.   }
  135.   else if (WheelPos < 170) {
  136.     WheelPos -= 85;
  137.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  138.   }
  139.   else {
  140.     WheelPos -= 170;
  141.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  142.   }
  143. }
复制代码


回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 11:57:42 | 显示全部楼层
aladdincp 发表于 2021-12-15 10:54
动次打次动次打次 哈哈哈

谢谢鼓励哦
回复 支持 反对

使用道具 举报

发表于 2021-12-15 12:33:36 来自手机浏览器 | 显示全部楼层
那种现成的比较好吧? 就是街边商铺,上面那种红色LED屏,也有彩色的,那种现成的比较合适
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 13:02:00 | 显示全部楼层
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之二:音乐反应式 LED 灯板(4x4位)

  实验视频剪辑

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



回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 13:03:42 | 显示全部楼层
  实验场动态图



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 13:05:47 | 显示全部楼层
360安仔 发表于 2021-12-15 12:33
那种现成的比较好吧? 就是街边商铺,上面那种红色LED屏,也有彩色的,那种现成的比较合适 ...

那种可以做频谱显示吗,没有试过
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-15 15:21:45 | 显示全部楼层
【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  项目之三:六十四位闪动音乐频谱灯(8x8WS2812硬屏)

  实验开源代码

  1. /*
  2.   【花雕动手做】有趣好玩的音乐可视化系列小项目(10)---WS2812硬板屏
  3.   项目之三:六十四位闪动音乐频谱灯(8x8WS2812硬屏)
  4. */

  5. #include "FastLED.h"
  6. #define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
  7. #define OCT_NORM 0 // Don't normalise octave intensities by number of bins
  8. #define FHT_N 256 // set to 256 point fht
  9. #include <FHT.h> // include the library
  10. //int noise[] = {204,188,68,73,150,98,88,68}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}


  11. // int noise[] = {204,190,108,85,65,65,55,60}; // noise for mega adk
  12. int noise[] = {204, 195, 100, 90, 85, 80, 75, 75}; // noise for NANO
  13. //int noise[] = {204,198,100,85,85,80,80,80};
  14. float noise_fact[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}
  15. float noise_fact_adj[] = {15, 7, 1.5, 1, 1.2, 1.4, 1.7, 3}; // noise level determined by playing pink noise and seeing levels [trial and error]{204,188,68,73,150,98,88,68}

  16. #define LED_PIN     6
  17. #define LED_TYPE    WS2812
  18. #define COLOR_ORDER GRB

  19. // Params for width and height
  20. const uint8_t kMatrixWidth = 8;
  21. const uint8_t kMatrixHeight = 8;//----------was 27
  22. //#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  23. #define NUM_LEDS    64

  24. CRGB leds[NUM_LEDS];

  25. int counter2 = 0;



  26. void setup() {
  27.   Serial.begin(9600);
  28.   delay(1000);
  29.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  30.   FastLED.setBrightness (22);
  31.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  32.   FastLED.show();
  33.   // TIMSK0 = 0; // turn off timer0 for lower jitter
  34.   ADCSRA = 0xe5; // set the adc to free running mode
  35.   ADMUX = 0x40; // use adc0
  36.   DIDR0 = 0x01; // turn off the digital input for adc0
  37. }

  38. void loop() {
  39.   int prev_j[8];
  40.   int beat = 0;
  41.   int prev_oct_j;
  42.   int counter = 0;
  43.   int prev_beat = 0;
  44.   int led_index = 0;
  45.   int saturation = 0;
  46.   int saturation_prev = 0;
  47.   int brightness = 0;
  48.   int brightness_prev = 0;

  49.   while (1) { // reduces jitter
  50.     cli();  // UDRE interrupt slows this way down on arduino1.0
  51.     for (int i = 0 ; i < FHT_N ; i++) { // save 256 samples
  52.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  53.       ADCSRA = 0xf5; // restart adc
  54.       byte m = ADCL; // fetch adc data
  55.       byte j = ADCH;
  56.       int k = (j << 8) | m; // form into an int
  57.       k -= 0x0200; // form into a signed int
  58.       k <<= 6; // form into a 16b signed int
  59.       fht_input[i] = k; // put real data into bins
  60.     }
  61.     fht_window(); // window the data for better frequency response
  62.     fht_reorder(); // reorder the data before doing the fht
  63.     fht_run(); // process the data in the fht
  64.     fht_mag_octave(); // take the output of the fht  fht_mag_log()

  65.     // every 50th loop, adjust the volume accourding to the value on A2 (Pot)
  66.     if (counter >= 50) {
  67.       ADMUX = 0x40 | (1 & 0x07); // set admux to look at Analogpin A1 - Master Volume

  68.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  69.       ADCSRA = 0xf5; // restart adc
  70.       delay(10);
  71.       while (!(ADCSRA & 0x10)); // wait for adc to be ready
  72.       ADCSRA = 0xf5; // restart adc
  73.       byte m = ADCL; // fetch adc data
  74.       byte j = ADCH;
  75.       int k = (j << 8) | m; // form into an int
  76.       float master_volume = (k + 0.1) / 1000 + .75; // so the valu will be between ~0.5 and 1.---------------------+.75 was .5
  77.       Serial.println (master_volume);

  78.       for (int i = 1; i < 8; i++) {
  79.         noise_fact_adj[i] = noise_fact[i] * master_volume;
  80.       }

  81.       ADMUX = 0x40 | (0 & 0x07); // set admux back to look at A0 analog pin (to read the microphone input
  82.       counter = 0;
  83.     }

  84.     sei();
  85.     counter++;
  86.     // End of Fourier Transform code - output is stored in fht_oct_out[i].
  87.     // i=0-7 frequency (octave) bins (don't use 0 or 1), fht_oct_out[1]= amplitude of frequency for bin 1
  88.     // for loop a) removes background noise average and takes absolute value b) low / high pass filter as still very noisy
  89.     // c) maps amplitude of octave to a colour between blue and red d) sets pixel colour to amplitude of each frequency (octave)

  90.     for (int i = 1; i < 8; i++) {  // goes through each octave. skip the first 1, which is not useful

  91.       int j;
  92.       j = (fht_oct_out[i] - noise[i]); // take the pink noise average level out, take the asbolute value to avoid negative numbers
  93.       if (j < 10) {
  94.         j = 0;
  95.       }
  96.       j = j * noise_fact_adj[i];

  97.       if (j < 10) {
  98.         j = 0;
  99.       }
  100.       else {
  101.         j = j * noise_fact_adj[i];
  102.         if (j > 180) {
  103.           if (i >= 7) {
  104.             beat += 2;
  105.           }
  106.           else {
  107.             beat += 1;
  108.           }
  109.         }
  110.         j = j / 30;
  111.         j = j * 30; // (force it to more discrete values)
  112.       }

  113.       prev_j[i] = j;

  114.       //     Serial.print(j);
  115.       //     Serial.print(" ");


  116.       // this fills in 11 LED's with interpolated values between each of the 8 OCT values
  117.       if (i >= 2) {
  118.         led_index = 2 * i - 3;
  119.         prev_oct_j = (j + prev_j[i - 1]) / 2;

  120.         saturation = constrain(j + 50, 0, 255); //-----------50 was 30
  121.         saturation_prev = constrain(prev_oct_j + 50, 0, 255);
  122.         brightness = constrain(j, 0, 255);
  123.         brightness_prev = constrain(prev_oct_j, 0, 255);
  124.         if (brightness == 255) {
  125.           saturation = 50;
  126.           brightness = 200;
  127.         }
  128.         if (brightness_prev == 255) {
  129.           saturation_prev = 50;
  130.           brightness_prev = 200;
  131.         }


  132.         for (uint8_t y = 0; y < kMatrixHeight; y++) {
  133.           leds[XY(led_index - 1, y)] = CHSV(j + y * 30, saturation, brightness);
  134.           if (i > 2) {
  135.             prev_oct_j = (j + prev_j[i - 1]) / 2;
  136.             leds[ XY(led_index - 2, y)] = CHSV(prev_oct_j + y * 30, saturation_prev, brightness_prev);
  137.           }
  138.         }
  139.       }
  140.     }

  141.     if (beat >= 7) {
  142.       fill_solid(leds, NUM_LEDS, CRGB::Gray);
  143.       FastLED.setBrightness(200);

  144.     }
  145.     else {
  146.       if (prev_beat != beat) {
  147.         FastLED.setBrightness(40 + beat * beat * 5);
  148.         prev_beat = beat;
  149.       }
  150.     }

  151.     FastLED.show();
  152.     if (beat) {
  153.       counter2 += ((beat + 4) / 2 - 2);
  154.       if (counter2 < 0) {
  155.         counter2 = 1000;
  156.       }
  157.       if (beat > 3 && beat < 7) {
  158.         FastLED.delay (20);
  159.       }
  160.       beat = 0;
  161.     }
  162.     // Serial.println();
  163.   }
  164. }

  165. // Param for different pixel layouts
  166. const bool    kMatrixSerpentineLayout = false;
  167. // Set 'kMatrixSerpentineLayout' to false if your pixels are
  168. // laid out all running the same way, like this:
  169. // Set 'kMatrixSerpentineLayout' to true if your pixels are
  170. // laid out back-and-forth, like this:

  171. uint16_t XY( uint8_t x, uint8_t y)
  172. {
  173.   uint16_t i;

  174.   if ( kMatrixSerpentineLayout == false) {
  175.     i = (y * kMatrixWidth) + x;
  176.   }

  177.   if ( kMatrixSerpentineLayout == true) {
  178.     if ( y & 0x01) {
  179.       // Odd rows run backwards
  180.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  181.       i = (y * kMatrixWidth) + reverseX;
  182.     } else {
  183.       // Even rows run forwards
  184.       i = (y * kMatrixWidth) + x;
  185.     }
  186.   }

  187.   i = (i + counter2) % NUM_LEDS;
  188.   return i;
  189. }
复制代码


回复 支持 反对

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2025-5-3 01:12 , Processed in 0.514801 second(s), 11 queries , Redis On.

Powered by Discuz!

© 2006-2025 MyDigit.Net

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