“彩灯的多种玩法”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Yanglibin@microduino.cc移动页面彩灯呼吸闪烁效果彩灯的多种玩法
第3行: 第3行:
 
|
 
|
 
<br>
 
<br>
<big>此示例给出了控制彩灯的几种显示效果。</big>
+
<big>此示例给出了控制彩灯的几种显示效果。
 +
 
 +
'''呼吸、流水、闪烁、彩虹、炫彩'''</big>
 
<br>
 
<br>
 
<br>
 
<br>
第22行: 第24行:
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:135%">代码</p>
 
<p style="color: #E87E05;font-size:135%">代码</p>
*呼吸灯
+
*<big>呼吸灯</big>
 
<source lang="cpp">
 
<source lang="cpp">
 
#include <Microduino_ColorLED.h> //引用彩灯库
 
#include <Microduino_ColorLED.h> //引用彩灯库
第66行: 第68行:
 
</source>
 
</source>
  
*闪烁灯
+
<br>
 +
<br>
 +
 
 +
*<big>流水灯</big>
 
<source lang="cpp">
 
<source lang="cpp">
 +
 +
#include <Microduino_ColorLED.h> //引用彩灯库
 +
 +
#define PIN            6        //彩灯引脚
 +
#define NUMPIXELS      7        //级联彩灯数量
 +
 +
#define val_max 255
 +
#define val_min 0
 +
 +
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
 +
 +
void setup() {
 +
  strip.begin();                //彩灯初始化
 +
  strip.show();
 +
}
 +
 +
/*流水灯效果*/
 +
void loop() {
 +
  theaterChase(COLOR_RED,    2, 450); //红色流水灯效果,循环2次,两灯之间延时450ms
 +
  theaterChase(COLOR_BLUE,  4, 400); //蓝色流水灯效果,循环4次,两灯之间延时400ms
 +
}
 +
 +
//Theatre-style crawling lights.
 +
void theaterChase(uint32_t c, uint8_t n, uint8_t wait) {
 +
  for (int j = 0; j < n; j++) { //do 10 cycles of chasing
 +
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
 +
      strip.setPixelColor(i, c);  //turn every third pixel on
 +
      strip.show();
 +
      delay(wait);
 +
      strip.setPixelColor(i, 0); //turn every third pixel on
 +
      strip.show();
 +
    }
 +
  }
 +
}
  
 
</source>
 
</source>
  
*彩虹灯
+
<br>
 +
<br>
 +
 
 +
*<big>闪烁灯</big>
 +
<source lang="cpp">
 +
 
 +
#include <Microduino_ColorLED.h> //引用彩灯库
 +
 
 +
#define PIN            6        //彩灯引脚
 +
#define NUMPIXELS      7        //级联彩灯数量
 +
 
 +
#define val_max 255
 +
#define val_min 0
 +
 
 +
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
 +
 
 +
void setup() {
 +
  strip.begin();                //彩灯初始化
 +
  strip.show();
 +
}
 +
 
 +
void loop() {
 +
  ledBlinkALL(COLOR_BLUE, 1000);
 +
}
 +
 
 +
//第n号灯,颜色c,闪烁时间wait
 +
void ledBlink(uint32_t c, uint8_t n, uint8_t wait) {
 +
  strip.setPixelColor(n, c);  //turn every third pixel on
 +
  strip.show();
 +
  delay(wait);
 +
  strip.setPixelColor(n, 0); //turn every third pixel on
 +
  strip.show();
 +
  delay(wait);
 +
}
 +
 
 +
//全部灯,颜色c,闪烁时间wait
 +
void ledBlinkALL(uint32_t c, uint8_t wait) {
 +
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
 +
    strip.setPixelColor(i, c); 
 +
    strip.show();
 +
  }
 +
  delay(wait);
 +
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
 +
    strip.setPixelColor(i, 0);
 +
    strip.show();
 +
  }
 +
  delay(wait);
 +
}
 +
 
 +
</source>
 +
 
 +
<br>
 +
<br>
 +
 
 +
*<big>彩虹灯</big>
 
<source lang="cpp">
 
<source lang="cpp">
 +
 +
#include <Microduino_ColorLED.h> //引用彩灯库
 +
 +
#define PIN            6        //彩灯引脚
 +
#define NUMPIXELS      7        //级联彩灯数量
 +
 +
#define val_max 255
 +
#define val_min 0
 +
 +
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
 +
 +
void setup() {
 +
  strip.begin();                //彩灯初始化
 +
  strip.show();
 +
}
 +
 +
/*彩虹灯效*/
 +
void loop() {
 +
  rainbow(20);
 +
}
 +
 +
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);
 +
  }
 +
}
 +
 +
// 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);
 +
}
  
 
</source>
 
</source>
  
 +
<br>
 +
<br>
 +
 +
*<big>炫彩灯</big>
 +
<source lang="cpp">
 +
 +
#include <Microduino_ColorLED.h> //引用彩灯库
 +
 +
#define PIN            6        //彩灯引脚
 +
#define NUMPIXELS      7        //级联彩灯数量
 +
 +
#define val_max 255
 +
#define val_min 0
 +
 +
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
 +
 +
void setup() {
 +
  strip.begin();                //彩灯初始化
 +
  strip.show();
 +
}
 +
 +
/*炫彩灯效*/
 +
void loop() {
 +
  rainbowCycle(20);
 +
}
 +
 +
// 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);
 +
  }
 +
}
 +
 +
// 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);
 +
}
 +
 +
</source>
  
 
<br>
 
<br>

2017年7月7日 (五) 08:56的版本


此示例给出了控制彩灯的几种显示效果。

呼吸、流水、闪烁、彩虹、炫彩

硬件清单



电路搭建

将彩灯的IN接到Hub的6/7引脚,实际使用的是6号引脚。
将Battery、Core、Hub堆叠在一起,通过MicroUSB数据线接入电脑。



代码

  • 呼吸灯
#include <Microduino_ColorLED.h> //引用彩灯库

#define PIN            6         //彩灯引脚
#define NUMPIXELS      16        //级联彩灯数量

#define val_max 255
#define val_min 0

ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号

void setup() {
  strip.begin();                 //彩灯初始化
  strip.show();
}

void loop() {
  rainbowCycle( 255, 0, 0, 5);//红色呼吸
  rainbowCycle( 0, 255, 0, 5);//绿色呼吸
  rainbowCycle( 0, 0, 255, 5);//蓝色呼吸
}

void colorSet(uint32_t c) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
  }
  strip.show();
}

void rainbowCycle( int r, int g, int b, uint8_t wait) {
  for (int val = 0; val < 255; val++)
  {
    colorSet(strip.Color(map(val, val_min, val_max, 0, r), map(val, val_min, val_max, 0, g), map(val, val_min, val_max, 0, b)));
    delay(wait);
  }
  for (int val = 255; val >= 0; val--)
  {
    colorSet(strip.Color(map(val, val_min, val_max, 0, r), map(val, val_min, val_max, 0, g), map(val, val_min, val_max, 0, b)));
    delay(wait);
  }
}



  • 流水灯
#include <Microduino_ColorLED.h> //引用彩灯库

#define PIN            6         //彩灯引脚
#define NUMPIXELS      7        //级联彩灯数量

#define val_max 255
#define val_min 0

ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号

void setup() {
  strip.begin();                 //彩灯初始化
  strip.show();
}

/*流水灯效果*/
void loop() {
  theaterChase(COLOR_RED,    2, 450); //红色流水灯效果,循环2次,两灯之间延时450ms
  theaterChase(COLOR_BLUE,   4, 400); //蓝色流水灯效果,循环4次,两灯之间延时400ms
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t n, uint8_t wait) {
  for (int j = 0; j < n; j++) { //do 10 cycles of chasing
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);  //turn every third pixel on
      strip.show();
      delay(wait);
      strip.setPixelColor(i, 0); //turn every third pixel on
      strip.show();
    }
  }
}



  • 闪烁灯
#include <Microduino_ColorLED.h> //引用彩灯库

#define PIN            6         //彩灯引脚
#define NUMPIXELS      7         //级联彩灯数量

#define val_max 255
#define val_min 0

ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号

void setup() {
  strip.begin();                 //彩灯初始化
  strip.show();
}

void loop() {
  ledBlinkALL(COLOR_BLUE, 1000);
}

//第n号灯,颜色c,闪烁时间wait
void ledBlink(uint32_t c, uint8_t n, uint8_t wait) {
  strip.setPixelColor(n, c);  //turn every third pixel on
  strip.show();
  delay(wait);
  strip.setPixelColor(n, 0); //turn every third pixel on
  strip.show();
  delay(wait);
}

//全部灯,颜色c,闪烁时间wait
void ledBlinkALL(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);  
    strip.show();
  }
  delay(wait);
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0); 
    strip.show();
  }
  delay(wait);
}



  • 彩虹灯
#include <Microduino_ColorLED.h> //引用彩灯库

#define PIN            6         //彩灯引脚
#define NUMPIXELS      7        //级联彩灯数量

#define val_max 255
#define val_min 0

ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号

void setup() {
  strip.begin();                 //彩灯初始化
  strip.show();
}

/*彩虹灯效*/
void loop() {
  rainbow(20);
}

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);
  }
}

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



  • 炫彩灯
#include <Microduino_ColorLED.h> //引用彩灯库

#define PIN            6         //彩灯引脚
#define NUMPIXELS      7        //级联彩灯数量

#define val_max 255
#define val_min 0

ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号

void setup() {
  strip.begin();                 //彩灯初始化
  strip.show();
}

/*炫彩灯效*/
void loop() {
  rainbowCycle(20);
}

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

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


相关案例




返回Sensor-ColorLED_Reference界面

返回Sensor-Color_LED界面