ESP8266 Arduino-控制WS2812霓虹灯
一、下载外设库
从https://github.com/adafruit/Adafruit_NeoPixel下载Adafruit_NeoPixel-master.zip,点击项目->加载库->添加.ZIP文件,为IDE选择并加载下载的外设包。
二、实现代码
/**
* 模块管脚 <--> 开发板管脚 <--> 外设管脚
* GPIO5 D1 WS2811
*/
#include <Adafruit_NeoPixel.h>
#define WS2811_PIN D1
#define MAX_LED 1
uint8 RGB[][3] = {
{ 0xFF, 0x00, 0x00 },
{ 0x00, 0xFF, 0xFF },
{ 0x00, 0x00, 0xFF },
{ 0x00, 0x00, 0xA0 },
{ 0xFF, 0x00, 0x80 },
{ 0x80, 0x00, 0x80 },
{ 0xFF, 0xFF, 0x00 },
{ 0x00, 0xFF, 0x00 },
{ 0xFF, 0x00, 0xFF },
{ 0xFF, 0xFF, 0xFF },
{ 0xC0, 0xC0, 0xC0 },
{ 0x00, 0x00, 0x00 },
{ 0xFF, 0x80, 0x40 },
{ 0x80, 0x40, 0x00 },
{ 0x80, 0x00, 0x00 },
{ 0x80, 0x80, 0x00 },
{ 0x40, 0x80, 0x80 }
};
// Parameter 1 = ws2811级联数量
// Parameter 2 = arduino WS2811_PIN
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, WS2811_PIN, NEO_RGB + NEO_KHZ800 );
void setup()
{
// 初始化Adafruit_NeoPixel库
strip.begin();
// 初始化时关闭所有LED
strip.show();
}
void loop()
{
// 循环输出七彩色
for (uint16_t i = 0; i < sizeof(RGB) / sizeof(RGB[0]); i++)
{
// 设置颜色,参数为 R G B,范围0-255
uint32_t color = strip.Color(RGB[i][0], RGB[i][1], RGB[i][2]);;
// 由于只有1个三色LED,所以编号为0
strip.setPixelColor(0, color);
// 发送数据并显示
strip.show();
// 延时500毫秒
delay(500);
}
}