2018年8月1日 星期三

Arduino RGB 全彩LED WS2812 5050,跑馬燈

本章要跟大家介紹WS2812這顆RGB LED來製作最簡易的跑馬燈,其實WS2812 RGB LED製作跑馬燈效果相當好,控制電路又簡單,只需要運用到一個數位腳就可以實現控制一整排LED,今天展示的是最簡單的跑馬燈形式



實際實驗環境



參考資料
函式庫網址如下

請將網址資料打包放至如下資料夾
"C:\Users\___XXXX___\Documents\Arduino\libraries"

接線方式

今天採用的是Arduino NANO + WS2812 RGB LED燈條
燈條上的DOUT是用來串接後面的燈條用的
******************接線方式******************
NANO                   WS2812燈條

+5V------------------VCC
D6------------------DIN
GND------------------GND
********************************************

範例程式

如果想要更多的顏色變化,可以自行修改
由於我實驗的LED燈條有兩顆燒壞了,所以有修改過LED的總數

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      6

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 1000; // delay for half a second

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

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

mode_1();
mode_2();
mode_3();

mode_4();



}

void mode_1(){
  int mode_i=0;
  
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(20,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }
    for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,20,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }
    for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,20)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).
  }

}

void mode_2(){
  int mode_i=0;
  
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(20,0,0)); // Moderately bright green color.
    //pixels.setPixelColor((mode_i+NUMPIXELS-1)%NUMPIXELS, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).    
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
  }
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,20,0)); // Moderately bright green color.
    //pixels.setPixelColor((mode_i+NUMPIXELS-1)%NUMPIXELS, pixels.Color(0,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).    
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
  }
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,20)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds).    
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.
  }
  
}
void mode_3(){
  int mode_i=0;
  
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(20,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }
    for(mode_i=NUMPIXELS-1;mode_i>0;mode_i--){
    pixels.setPixelColor(mode_i, pixels.Color(20,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }

  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,20,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }
    for(mode_i=NUMPIXELS-1;mode_i>0;mode_i--){
    pixels.setPixelColor(mode_i, pixels.Color(0,20,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }
  
  for(mode_i=0;mode_i<NUMPIXELS;mode_i++){
    pixels.setPixelColor(mode_i, pixels.Color(0,0,20)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }
    for(mode_i=NUMPIXELS-1;mode_i>0;mode_i--){
    pixels.setPixelColor(mode_i, pixels.Color(0,20,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(200); // Delay for a period of time (in milliseconds). 
    pixels.setPixelColor(mode_i, pixels.Color(0,0,0)); // Moderately bright green color.  
  }
  
}

void mode_4(){
  int mode_i=0,LED_R=0,LED_G=0,LED_B=0;
  
    for(mode_i=0;mode_i<50;mode_i++){
    pixels.setPixelColor(0, pixels.Color(mode_i,0,0)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }
    for(mode_i=50;mode_i>0;mode_i--){
    pixels.setPixelColor(0, pixels.Color(mode_i,0,0)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }
    
    for(mode_i=0;mode_i<50;mode_i++){
    pixels.setPixelColor(0, pixels.Color(0,mode_i,0)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }
    for(mode_i=50;mode_i>0;mode_i--){
    pixels.setPixelColor(0, pixels.Color(0,mode_i,0)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }
    
    for(mode_i=0;mode_i<50;mode_i++){
    pixels.setPixelColor(0, pixels.Color(0,0,mode_i)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }
    for(mode_i=50;mode_i>0;mode_i--){
    pixels.setPixelColor(0, pixels.Color(0,0,mode_i)); // Moderately bright green color.    
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(40); // Delay for a period of time (in milliseconds). 
    }

}

實際測試影片




未來可行的應用

  • 居家的氣氛燈設計
  • 汽車、機車的可動車尾燈
  • 環境中各種可以透過LED變化的應用
  • 生活裝置藝術






沒有留言:

張貼留言

專題許願池,請不要吝嗇將你想製作的專題用回覆的方式寫在下方

專題許願池 開這個板是為了幫助一些人,有專題製作需求,但是又不知道要如何實現 如果您有想製作的專題,請您將您的題目回覆在下方,先提出您的需求,進而討論可行性或製做的方向,或是您進行到什麼階段,有遇到什麼樣的問題也可以提出來討論,如果我知道會盡量幫助您順利完成。 不限...