現在,我們編寫一個程式來點亮LED燈串。下面的示例代碼基於
Adafruit_NeoPixel 代碼示例。
LED燈條的示例程式
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 <Adafruit_NeoPixel.h> #define PIN 6 //pin number of the signal terminal #define LED_NUM 50 //number of LEDs Adafruit_NeoPixel ledtape = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800); //if the control IC of the LED tape used is WS2812, the third parameter is NEO_KHZ800 and if WS2811 then NEO_KHZ400 void setup() { ledtape.begin(); ledtape.show(); //reflects once in all OFF state } void loop() { uint16_t i, j; for(j=0; j <256; j++) { for(i=0; i < ledtape.numPixels(); i++) { ledtape.setPixelColor(i, rotateColor((((i) * 256 / ledtape.numPixels()) + j) & 255)); } ledtape.show(); delay(20); } } //RGB color transition function uint32_t rotateColor(byte WheelPos) { if(WheelPos < 85) { return ledtape.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return ledtape.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return ledtape.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } |
如果已經連接LED燈帶並且程式運行正常,您將會看到LED燈點亮。
而且,如果稍微改變一下程式,我們也可以一個接一個地點亮LED燈。
一次點亮一個LED燈的示例程式
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 |
#include <Adafruit_NeoPixel.h> #define PIN 6 //pin number of the signal terminal #define LED_NUM 50 //number of LEDs Adafruit_NeoPixel ledtape = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800); //if the control IC of the LED tape used is WS2812, the third parameter is NEO_KHZ800 and if WS2811 then NEO_KHZ400 void setup() { ledtape.begin(); ledtape.show(); //reflects once in all OFF state } void loop() { uint16_t i, j; //Set the state of all colors of the LED in minutes and light in show for(i=0; i < ledtape.numPixels(); i++) { ledtape.setPixelColor(i, Wheel((((i) * 256 / ledtape.numPixels()) + j) & 255)); ledtape.show(); ledtape.setPixelColor(i, ledtape.Color(0,0,0)); delay(20); ledtape.show(); } } //RGB color transition function uint32_t rotateColor(byte WheelPos) { if(WheelPos < 85) { return ledtape.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return ledtape.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return ledtape.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } |
現在您可以使用剛完成的LED燈串來裝飾您的室內或室外空間了!點燃萬聖節南瓜燈後,您會覺得萬聖節又再度造訪!
今天,我們學習了如何使用LED燈條/燈串的基本知識。下次,我們將使用ROHM感測器評估套件的感測器構建一個根據感測器輸入值控制LED燈串的設備。假日已經提前來臨了!讓我們用這些酷炫的LED燈條/燈串,為家裡增添一些節日氣氛吧!