彩灯的多种玩法
1196357542(讨论 | 贡献)2017年7月14日 (五) 07:10的版本
呼吸、流水、闪烁、彩虹、炫彩
硬件清单
电路搭建 将彩灯的IN接到Hub的6/7引脚,实际使用的是6号引脚。
代码
#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);
}
相关案例
|