02- 8-Bit WS2812 RGB LED

參考資料:http://pizgchen.blogspot.tw/2017/04/ws2812-8-bit-2812-rgb-led.html

  • 傳統方式要控制多顆 RGB LED 在電路接線和程式控制方面是非常煩雜的,然而使用內建 WS2812 晶片的 RGB LED 卻是簡單又方便,不管你要控制幾顆 RGB LED,都只要使用 Arduino 3 支腳位就足夠了。

  • 8位 WS2812 5050 RGB LED 內置全彩驅動彩燈開發板

  • 淘寶:https://world.taobao.com/item/541980677823.htm?fromSite=main&spm=a1z09.2.0.0.A2AvmF&_u=41lsjr25ba78

  • 電路接線

  • 用杜邦線依下表將 Arduino 與燈條連接起來:

  • Arduino 燈條

  • 5V VDC

  • Gnd Gnd

  • D6 DI

// uint32_t color = strip.Color(val, val, val); // 设置颜色,参数为 R G B,范围0-255

// strip.setPixelColor(0, color); // 设置某个点的颜色,若只有一个,所以只控制第0个点

  • 下載函式庫

  • 1. 點選右側網址下載函式庫 https://github.com/adafruit/Adafruit_NeoPixel

  • 2. 將下載的檔案解壓縮,複製到 Arduino IDE 的 libraries 資料夾裏。

  • 3. 更改名稱為「Adafruit_NeoPixel」。

  • 執行範例程式

  • 1. 啟動 Arduino IDE。

  • 2. 點擊「檔案」>「範例」>「Adafruit_NeoPixel」>「strandtest」。

  • 3. 找到這一行

  • Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

  • 修改為

  • Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800); 看有幾顆LED燈?

  • Demo Code(取自https://learn.adafruit.com/getting-started-with-flora/blink-onboard-neopixel )

  1. #include <Adafruit_NeoPixel.h>

  2. #define PIN 6 //接腳

  3. #define MAX_LED 1 //LED的數量

  4. //Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

  5. Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_GRB + NEO_KHZ800 ); //NEO_GRB(顯示顏色是RGB);NEO_RGB(顯示顏色是GRB)

  6. //Adafruit_NeoPixel就是類別,其建構子有3個參數,分別為LED的數量、硬體連接的腳位、以及LED的型別。

  7. //以上例來說,只有1個LED,連接在第6支腳,採用GRB,800KHZ通訊訊號的速率。

  8. //為何LED跟通訊訊號的速率有關,因為它採用串接方式,當送出10顆LED命令時,接收到的第一顆LED會取下其命令,然後把9個命令再往下送,以此類推。

    1. // Parameter 1 = number of pixels in strip

    2. // Parameter 2 = Arduino pin number (most are valid)

    3. // Parameter 3 = pixel type flags, add together as needed:

    4. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)

    5. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)

    6. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)

    7. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

    8. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

  9. void setup() {

  10. strip.begin(); // 初始化库

  11. strip.show(); // Initialize all pixels to 'off' // 发送数据,默认每个点的颜色为0,所以初始的每个点都是不亮的

  12. }

  13. void loop() {

  14. // Some example procedures showing how to display to the pixels:

    1. colorWipe(strip.Color(255, 0, 0), 500); // Red (紅) 顯示颜色,参数为 R G B,范围0-255(亮度),後方參數為顯示時間。colorWipe第一個參數為顏色值,第二個為延遲的時間。

  1. colorWipe(strip.Color(0, 255, 0), 500); // Green(綠)

  2. colorWipe(strip.Color(0, 0, 255), 500); // Blue(藍)

  3. rainbowCycle(20);

  4. }

  5. // Fill the dots one after the other with a color

    1. void colorWipe(uint32_t c, uint8_t wait) {

  1. for(uint16_t i=0; i<strip.numPixels(); i++) { //strip.numPixels() LED個數-1

  2. strip.setPixelColor(i, c);

  3. strip.show();

  4. delay(wait);

  5. }

  6. }

  7. //由上可知,如果要在第一顆LED燈顯示紅燈,程式如下

  8. // strip.setPixelColor(0, strip.Color(50, 0, 0)); // strip.show(); // delay(5000);

  9. // Slightly different, this makes the rainbow equally distributed throughout

  10. void rainbowCycle(uint8_t wait) {

  11. uint16_t i, j;

  12. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel

  13. for(i=0; i< strip.numPixels(); i++) {

  14. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));

  15. }

  16. strip.show();

  17. delay(wait);

  18. }

  19. }

  20. // Input a value 0 to 255 to get a color value.

  21. // The colours are a transition r - g - b - back to r.

  22. uint32_t Wheel(byte WheelPos) {

  23. WheelPos = 255 - WheelPos;

  24. if(WheelPos < 85) {

  25. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

  26. } else if(WheelPos < 170) {

  27. WheelPos -= 85;

  28. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

  29. } else {

  30. WheelPos -= 170;

  31. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

  32. }

  33. }

上例中有用到下面呈現亮燈的函式,前者是固定顏色的顯示,後者則是以顏色變化來顯示。

  • colorWipe(strip.Color(255, 0, 0), 500);

  • rainbowCycle(20);

下例為「檔案」>「範例」>「Adafruit_NeoPixel」>「strandtest」的程式

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

#include <avr/power.h>

#endif

#define PIN 6

// Parameter 1 = number of pixels in strip

// Parameter 2 = Arduino pin number (most are valid)

// 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)

// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across

// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input

// and minimize distance between Arduino and first pixel. Avoid connecting

// on a live circuit...if you must, connect GND first.

void setup() {

// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket

#if defined (__AVR_ATtiny85__)

if (F_CPU == 16000000) clock_prescale_set(clock_div_1);

#endif

// End of trinket special code

strip.begin();

strip.show(); // Initialize all pixels to 'off'

}

void loop() {

// Some example procedures showing how to display to the pixels:

colorWipe(strip.Color(255, 0, 0), 50); // Red

colorWipe(strip.Color(0, 255, 0), 50); // Green

colorWipe(strip.Color(0, 0, 255), 50); // Blue

//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW

// Send a theater pixel chase in...

theaterChase(strip.Color(127, 127, 127), 50); // White 閃爍

theaterChase(strip.Color(127, 0, 0), 50); // Red

theaterChase(strip.Color(0, 0, 127), 50); // Blue

rainbow(20);

rainbowCycle(20);

theaterChaseRainbow(50);

}

// Fill the dots one after the other with a color

void colorWipe(uint32_t c, uint8_t wait) {

for(uint16_t i=0; i<strip.numPixels(); i++) {

strip.setPixelColor(i, c);

strip.show();

delay(wait);

}

}

void rainbow(uint8_t wait) {

uint16_t i, j;

for(j=0; j<256; j++) {

for(i=0; i<strip.numPixels(); i++) {

strip.setPixelColor(i, Wheel((i+j) & 255));

}

strip.show();

delay(wait);

}

}

// Slightly different, this makes the rainbow equally distributed throughout

void rainbowCycle(uint8_t wait) {

uint16_t i, j;

for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel

for(i=0; i< strip.numPixels(); i++) {

strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));

}

strip.show();

delay(wait);

}

}

//Theatre-style crawling lights.

void theaterChase(uint32_t c, uint8_t wait) {

for (int j=0; j<10; j++) { //do 10 cycles of chasing

for (int q=0; q < 3; q++) {

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

strip.setPixelColor(i+q, c); //turn every third pixel on

}

strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

strip.setPixelColor(i+q, 0); //turn every third pixel off

}

}

}

}

//Theatre-style crawling lights with rainbow effect

void theaterChaseRainbow(uint8_t wait) {

for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel

for (int q=0; q < 3; q++) {

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on

}

strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

strip.setPixelColor(i+q, 0); //turn every third pixel off

}

}

}

}

// Input a value 0 to 255 to get a color value.

// The colours are a transition r - g - b - back to r.

uint32_t Wheel(byte WheelPos) {

WheelPos = 255 - WheelPos;

if(WheelPos < 85) {

return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

}

if(WheelPos < 170) {

WheelPos -= 85;

return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

}

WheelPos -= 170;

return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

}

上例中有用到下面呈現亮燈的函數

  • colorWipe(strip.Color(255, 0, 0), 500);亮燈

  • theaterChase(strip.Color(127, 127, 127), 50); // White 閃爍

  • rainbow(20);

  • rainbowCycle(20);

  • theaterChaseRainbow(50);

下例為呼吸燈製作(http://www.arduino.cn/thread-6115-1-1.html)

#include <Adafruit_NeoPixel.h>

#define PIN 6

#define MAX_LED 1

#define ADD true

#define SUB false

int val = 0;

boolean stat = ADD;

// Parameter 1 = ws2811级联数量

// Parameter 2 = arduino 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, PIN, NEO_RGB + NEO_KHZ800 );

void setup()

{

// 初始化库

strip.begin();

// 发送数据,默认每个点的颜色为0,所以初始的每个点都是不亮的

strip.show();

}

void loop()

{

// 设置颜色,参数为 R G B,范围0-255

uint32_t color = strip.Color(val, val, val);

// 设置某个点的颜色,实验中只有一个,所以只控制第0个点

strip.setPixelColor(0, color);

// 下面是产生呼吸灯效果

if(val>=255)

stat = SUB;

if(val<=0)

stat = ADD;

strip.show();

delay(5);

if(stat==SUB) val --;

else if(stat==ADD) val++;

}