灯带代码实现

#include "FastLED.h"            // FastLED库
 
#define NUM_LEDS 60             // LED灯珠数量
#define DATA_PIN 3              // Arduino输出控制信号引脚
#define LED_TYPE WS2812         // LED灯带型号
#define COLOR_ORDER GRB         // RGB灯珠中红色、绿色、蓝色LED的排列顺序
 
uint8_t max_bright = 100;       // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
 
CRGB leds[NUM_LEDS];            // 建立光带leds

void led_rainbow_loop(void){
  FastLED.clear();
  FastLED.show();
  delay(500);
  
  fill_solid(leds, 60, CRGB::Red);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Orange);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Yellow);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Green);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Cyan);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Blue);
  FastLED.show();
  delay(1000);                     // 等待1秒
  fill_solid(leds, 60, CRGB::Purple);
  FastLED.show();
  delay(1000);                     // 等待1秒
}

void led_rainbow_flowing(void)
{
  FastLED.clear();
  FastLED.show();
  delay(500);
  int i = 0,loop_count = 0;
  if(NUM_LEDS%7 == 0)
  {
    loop_count = NUM_LEDS/7;
  }
  else
  {
    loop_count = NUM_LEDS/7+1;
  }
  
  
  for(i=0; i<loop_count; i++)
  {
    leds[i*7] = CRGB::Red;
    FastLED.show();
    delay(200);
    if(i*7+1>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+1] = CRGB::Orange;
    FastLED.show();
    delay(200);
    if(i*7+2>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+2] = CRGB::Yellow;
    FastLED.show();
    delay(200);
    if(i*7+3>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+3] = CRGB::Green;
    FastLED.show();
    delay(200);
    if(i*7+4>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+4] = CRGB::Cyan;
    FastLED.show();
    delay(200);
    if(i*7+5>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+5] = CRGB::Blue;
    FastLED.show();
    delay(200);
    if(i*7+6>=NUM_LEDS) 
    {
      break;
    }
    
    leds[i*7+6] = CRGB::Purple;
    FastLED.show();
    delay(200);
    if(i*7+7>=NUM_LEDS) 
    {
      break;
    }
  }
  
}


void led_rainbow_static(void){
  FastLED.clear();
  FastLED.show();
  delay(500);
  fill_rainbow(leds, 60, 0, 15);
  FastLED.show();
  delay(1000);
}

void setup() { 
  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // 初始化光带 
  FastLED.setBrightness(max_bright);                            // 设置光带亮度
}
 
void loop() { 
  while(1){
    led_rainbow_flowing();
    led_rainbow_static();
    led_rainbow_loop();
  }
}

#include //灯带库函数
#include
#define PIN D1//定义引脚

#define NUMPIXELS 29//定义led灯数量

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);//定义灯带(确定灯长,输出口,输出规则)

int delayval2 = 40; 
void setup(){
  srand((unsigned)time(NULL));	//设置随机数
  pixels.setBrightness(25);               //设置灯带亮度
  pixels.begin();                        //NeoPixel输出准备数据引脚
}
void loop() {
  uint32_t light2c = pixels.Color(rand()%255,rand()%255,rand()%255);//设置灯带RGB颜色
  int light2bright = 10;
  for(int i=0;i<NUMPIXELS;i++){
    pixels.setPixelColor(i,light2c);//设置第i盏灯为light2c色)
    pixels.show();//更新灯带
  }
  while(true){
    light2bright+=10;
    pixels.setBrightness(light2bright);//设置灯带亮度
    pixels.show();
    delay(delayval2);
    if(light2bright>=200)
      break;
  }
  while(true){
    light2bright-=10;
    pixels.setBrightness(light2bright);
    pixels.show();
    delay(delayval2);
    if(light2bright<=10)
      break;
  }
}

你可能感兴趣的:(嵌入式硬件,arduino)