Microduino-Color led是彩色LED灯,内置IC控制芯片,单总线控制,可任意级联,只需要一个I/O口就可以控制所有的灯,每个彩灯都可以单独控制。
#include <Microduino_ColorLED.h> //引用彩灯库
#define PIN 6 //led灯控制引脚
#define NUMPIXELS 2 //级联彩灯数量
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
void setup() {
strip.begin();
strip.setBrightness(60); //设置彩灯亮度
strip.show();
}
void loop() {
strip.setPixelColor(0, strip.Color(255, 0, 0));//红
strip.show();
delay(1000);
strip.setPixelColor(0, strip.Color(0, 255, 0));//绿
strip.show();
delay(1000);
strip.setPixelColor(0, strip.Color(0, 0, 255));//蓝
strip.show();
delay(1000);
strip.setPixelColor(0, strip.Color(0, 0, 0));//灭
strip.show();
delay(1000);
}
#include <Microduino_ColorLED.h> //引用彩灯库
#define PIN 6 //led灯控制引脚
#define NUMPIXELS 2 //级联彩灯数量
#define val_max 255
#define val_min 0
ColorLED strip = ColorLED(NUMPIXELS, PIN); //将ColorLED类命名为strip,并定义彩灯数量和彩灯引脚号
void setup() {
strip.begin();
strip.setBrightness(60); //设置彩灯亮度
strip.show();
}
void loop() {
rainbowCycle( 255, 0, 0, 10);//红色呼吸
rainbowCycle( 0, 255, 0, 10);//绿色呼吸
rainbowCycle( 0, 0, 255, 10);//蓝色呼吸
}
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);
}
}