引用: 火辣西米秀 发表于 2021-10-8 21:18 板子确实小,指甲盖大小不为过
性能好,价格也不错
WS2812B
是一个集控制电路与发光电路于一体的智能外控LED光源。其外型与一个5050LED灯珠相同,每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路,还包含有高精度的内部振荡器和12V高压可编程定电流控制部分,有效保证了像素点光的颜色高度一致。数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。
WS2812主要特点
1、智能反接保护,电源反接不会损坏IC。
2、IC控制电路与LED点光源公用一个电源。
3、控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
4、内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
5、内置上电复位和掉电复位电路。
6、每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
7、串行级联接口,能通过一根信号线完成数据的接收与解码。
8、任意两点传传输距离在不超过5米时无需增加任何电路。
9、当刷新速率30帧/秒时,级联数不小于1024点。
10、数据发送速度可达800Kbps。
11、光的颜色高度一致,性价比高。
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之一:使用MAX9814的音乐反应简单柱跳灯
实验开源代码
/*
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之一:使用MAX9814的音乐反应简单柱跳灯
*/
#include <Adafruit_NeoPixel.h>
#include <math.h>
#define N_PIXELS 8
#define MIC_PIN A0
#define LED_PIN 6
#define SAMPLE_WINDOW 5
#define PEAK_HANG 24
#define PEAK_FALL 4
#define INPUT_FLOOR 10
#define INPUT_CEILING 50
byte peak = 16;
unsigned int sample;
byte Count = 0;
byte HangCount = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
analogReference(EXTERNAL);
strip.setBrightness(22);
strip.show();
strip.begin();
}
float fscale( float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ;
curve = pow(10, curve);
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
Serial.print(OriginalRange, DEC);
Serial.print(" ");
Serial.print(NewRange, DEC);
Serial.print(" ");
Serial.println(zeroRefCurVal, DEC);
Serial.println();
delay(10);
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0) {
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
}
void loop() {
unsigned long startMillis = millis();
float peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1023;
unsigned int c, y;
while (millis() - startMillis < SAMPLE_WINDOW)
{
sample = analogRead(MIC_PIN);
if (sample < 1024)
{
if (sample > signalMax)
{
signalMax = sample;
}
else if (sample < signalMin)
{
signalMin = sample;
}
}
}
peakToPeak = signalMax - signalMin;
for (int i = 0; i <= strip.numPixels() - 1; i++) {
strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
}
c = fscale(INPUT_FLOOR, INPUT_CEILING, strip.numPixels(), 0, peakToPeak, 2);
if (c < peak) {
peak = c;
HangCount = 0;
}
if (c <= strip.numPixels()) {
drawLine(strip.numPixels(), strip.numPixels() - c, strip.Color(0, 0, 0));
}
y = strip.numPixels() - peak;
strip.setPixelColor(y - 1, Wheel(map(y, 0, strip.numPixels() - 1, 30, 150)));
strip.show();
if (HangCount > PEAK_HANG) {
if (++Count >= PEAK_FALL) {
peak++;
Count = 0;
}
}
else {
HangCount++;
}
}
void drawLine(uint8_t from, uint8_t to, uint32_t c) {
uint8_t fromTemp;
if (from > to) {
fromTemp = from;
from = to;
to = fromTemp;
}
for (int i = from; i <= to; i++) {
strip.setPixelColor(i, c);
}
}
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
本帖最后由 eagler8 于 2021-10-9 07:07 编辑
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之一:使用MAX9814的音乐反应简单柱跳灯
实验视频剪辑
https://v.youku.com/v_show/id_XNTgwODQ3NzI5Mg==.html?spm=a2hcb.playlsit.page.1
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之一:使用MAX9814的音乐反应12位环形灯
实验视频剪辑
https://v.youku.com/v_show/id_XNTgxMTk5MTM4OA==.html?firsttime=5
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之二:Adafruit_NeoPixel库的音乐可视化环灯
实验开源代码
/*
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之二:Adafruit_NeoPixel库的音乐可视化环灯
*/
#include <Adafruit_NeoPixel.h>
#define MIC A0 // 麦克风与A0相连接
#define LED_PIN 6 // LED are connected to D6
#define N_PIXELS 12 // Number of LED
#define N 100 //样本数
#define fadeDelay 10 // 淡出量
#define noiseLevel 15 // 降噪下限
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
int samples[N]; // 存储样本
int periodFactor = 0; // 用于周期计算
int t1 = -1;
int T;
int slope;
byte periodChanged = 0;
void setup() {
// Serial.begin(9600);
strip.begin();
ledsOff();
delay(500);
displayColor(Wheel(100));
strip.show();
delay(500);
}
void loop() {
Samples();
}
void Samples() {
for (int i = 0; i < N; i++) {
samples[i] = analogRead(0);
if (i > 0) {
slope = samples[i] - samples[i - 1];
}
else {
slope = samples[i] - samples[N - 1];
}
if (abs(slope) > noiseLevel) {
if (slope < 0) {
calculatePeriod(i);
if (periodChanged == 1) {
displayColor(getColor(T));
}
}
}
else {
ledsOff();
}
periodFactor += 1;
delay(1);
}
}
void calculatePeriod(int i) {
if (t1 == -1) {
t1 = i;
}
else {
int period = periodFactor * (i - t1);
periodChanged = T == period ? 0 : 1;
T = period;
// Serial.println(T);
t1 = i;
periodFactor = 0;
}
}
uint32_t getColor(int period) {
if (period == -1)
return Wheel(0);
else if (period > 400)
return Wheel(5);
else
return Wheel(map(-1 * period, -400, -1, 50, 255));
}
void fadeOut()
{
for (int i = 0; i < 5; i++) {
strip.setBrightness(110 - i * 20);
strip.show(); // Update strip
delay(fadeDelay);
periodFactor += fadeDelay;
}
}
void fadeIn() {
strip.setBrightness(100);
strip.show();
for (int i = 0; i < 5; i++) {
//strip.setBrightness(20*i + 30);
//strip.show();
delay(fadeDelay);
periodFactor += fadeDelay;
}
}
void ledsOff() {
fadeOut();
for (int i = 0; i < N_PIXELS; i++) {
strip.setPixelColor(i, 0, 0, 0);
}
}
void displayColor(uint32_t color) {
for (int i = 0; i < N_PIXELS; i++) {
strip.setPixelColor(i, color);
}
fadeIn();
}
uint32_t Wheel(byte WheelPos) {
// Serial.println(WheelPos);
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之二:Adafruit_NeoPixel库的音乐可视化环灯
实验视频剪辑
https://v.youku.com/v_show/id_XNTgxMTg4NTQzMg==.html?spm=a2hcb.playlsit.page.1
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之三:十六位直条WS2812音乐可视化柱灯
实验开源代码
/*
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之三:十六位直条WS2812音乐可视化柱灯
*/
#include <Adafruit_NeoPixel.h>
#define N_PIXELS 16 // Number of pixels in strand
#define MIC_PIN A0 // Microphone is attached to this analog pin
#define LED_PIN 6 // NeoPixel LED strand is connected to this pin
#define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
#define NOISE 300 // Noise/hum/interference in mic signal
#define SAMPLES 50 // Length of buffer for dynamic level adjustment
#define TOP (N_PIXELS + 2) // Allow dot to go slightly off scale
#define PEAK_FALL 60 // Rate of peak falling dot
byte
peak = 0, // Used for falling dot
dotCount = 0, // Frame counter for delaying dot-falling speed
volCount = 0; // Frame counter for storing past volume data
int
vol[SAMPLES], // Collection of prior volume samples
lvl = 100, // Current "dampened" audio level
minLvlAvg = 0, // For dynamic adjustment of graph low & high
maxLvlAvg = 512;
Adafruit_NeoPixel
strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
// Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
// line. Audio samples are 'cleaner' at 3.3V.
// COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
analogReference(EXTERNAL);
memset(vol, 0, sizeof(vol));
strip.setBrightness(66);
strip.begin();
}
void loop() {
uint8_t i;
uint16_t minLvl, maxLvl;
int n, height;
n = analogRead(MIC_PIN); // Raw reading from mic
n = abs(n - 512 - DC_OFFSET); // Center on zero
n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading (else looks twitchy)
// Calculate bar height based on dynamic min/max levels (fixed point):
height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
if (height < 0L) height = 0; // Clip output
else if (height > TOP) height = TOP;
if (height > peak) peak = height; // Keep 'peak' dot at top
// Color pixels based on rainbow gradient
for (i = 0; i < N_PIXELS; i++) {
if (i >= height) strip.setPixelColor(i, 0, 0, 0);
else strip.setPixelColor(i, Wheel(map(i, 0, strip.numPixels() - 1, 30, 150)));
}
// Draw peak dot
if (peak > 0 && peak <= N_PIXELS - 1) strip.setPixelColor(peak, Wheel(map(peak, 0, strip.numPixels() - 1, 30, 150)));
strip.show(); // Update strip
// Every few frames, make the peak pixel drop by 1:
if (++dotCount >= PEAK_FALL) { //fall rate
if (peak > 0) peak--;
dotCount = 0;
}
vol[volCount] = n; // Save sample for dynamic leveling
if (++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
// Get volume range of prior frames
minLvl = maxLvl = vol[0];
for (i = 1; i < SAMPLES; i++) {
if (vol[i] < minLvl) minLvl = vol[i];
else if (vol[i] > maxLvl) maxLvl = vol[i];
}
// minLvl and maxLvl indicate the volume range over prior frames, used
// for vertically scaling the output graph (so it looks interesting
// regardless of volume level). If they're too close together though
// (e.g. at very low volume levels) the graph becomes super coarse
// and 'jumpy'...so keep some minimum distance between them (this
// also lets the graph go to zero when no sound is playing):
if ((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; // Dampen min/max levels
maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; // (fake rolling average)
}
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
【花雕动手做】有趣好玩的音乐可视化系列小项目(05)---WS2812柱跳灯
项目之三:十六位直条WS2812音乐可视化柱灯
实验视频剪辑
https://v.youku.com/v_show/id_XNTgxMTkwNjMwNA==.html?spm=a2hcb.playlsit.page.1