先加载ws2812库文件https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>
 
#define PIN 6
#define MAX_LED 8
 
#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();           //初始化Adafruit_NeoPixel;
  strip.show();           //显示所有LED为关状态;
}

void loop()
{
  uint8_t i,a=0;                                        
  uint32_t color = strip.Color(0, 100, 255);         //选择所显示的颜色
  while(a<10)
  {
      for(i=0;i<9;i++)
      {
        if(i==a) strip.setPixelColor(i, color);     //第几个LED点亮;
        else strip.setPixelColor(i, 0);             //使其他LED全灭;
      }
       strip.show();                                //是LED显示所选的颜色;
       delay(20);                                   //延时20ms;
       a++;                                         
  }
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐