108个传感器之-ws2812LED(38)

108个传感器之-ws2812LED(38)

介绍

一个基于 WS2812 驱动的 LED 灯带模块。

引脚连接

pin 引脚连接开发板的 gpio 接口即可:

DEV BOARD Sensor
Pin 2 D1
5 V 4-7VDC
GND GND

注意:如果是串联的话就是灯带 1 的 D1接灯带 2 的 D0,因为 D1 是输入口,D0 是输出口。

代码示例

通过修改 NUMPIXELS 的数量,理论可以控制无数个灯的显示状态

普通动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

// 控制 WS2812 灯条的引脚编号
#define PIN 2

// 定义控制的 LED 数量
#define NUMPIXELS 8

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

// 相邻 LED 之间的延迟,单位毫秒
#define DELAYVAL 500

void setup()
{
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.

pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
// 在这里编写你的代码,控制LED灯条的亮灭和颜色
void loop()
{
pixels.clear(); // Set all pixel colors to 'off'

// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for (int i = 0; i < NUMPIXELS; i++)
{
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels.setPixelColor(i, pixels.Color(150, 150, 20));
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Pause before next pass through loop
}
}

结果

彩虹条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

// 控制 WS2812 灯条的引脚编号
#define PIN 2
// 定义控制的 LED 数量
#define NUMPIXELS 8

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.setBrightness(10); // Set BRIGHTNESS to about 4% (max = 255)
pixels.show(); // Initialize all pixels to 'off'
}
// 在这里编写你的代码,控制LED灯条的亮灭和颜色
void loop()
{
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
pixels.rainbow(firstPixelHue);
pixels.show();
delay(10);
}
}

ws2812b rainbow cycle

跑马灯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

// 控制 WS2812 灯条的引脚编号
#define PIN 2
// 定义控制的 LED 数量
#define NUMPIXELS 8

Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.setBrightness(10); // Set BRIGHTNESS to about 4% (max = 255)
strip.show(); // Initialize all pixels to 'off'
}

void theaterChase(uint32_t color, int wait) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear();
for(int c=b; c<strip.numPixels(); c += 3) {
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}

void loop()
{
theaterChase(strip.Color(255, 255, 255), 50); // White
theaterChase(strip.Color(255, 0, 0), 50); // Red
theaterChase(strip.Color( 0, 0, 255), 50); // Blue
}

ws2812b theater marquee chasing lights

雪花

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

// 控制 WS2812 灯条的引脚编号
#define PIN 2
// 定义控制的 LED 数量
#define NUMPIXELS 8

Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.setBrightness(10); // Set BRIGHTNESS to about 4% (max = 255)
strip.show(); // Initialize all pixels to 'off'
}

void loop()
{
int pixel[60];
for(int p=0; p<60; p++){
pixel[p] = random(0,255);
}

// Run some snowflake cycles
for (int j=0; j<200; j++) {
// Every five cycles, light a new pixel
if((j%5)==0){
strip.setPixelColor(random(0,60), 255,255,255);
}

// Dim all pixels by 10
for(int p=0; p<60; p++){
strip.setPixelColor(p, pixel[p],pixel[p],pixel[p] );
pixel[p] = pixel[p] - 10;
}
strip.show();
delay(100);
}
}

ws2812b snowflakes

小结

待完善…

作者

dev

发布于

2025-01-10

更新于

2025-01-10

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×