1 简示
ws2812注意方向
in
输入与输出
2 接图
内部3 线路图1
11
2
4
5
6
4 代码
#include "FastLED.h" // 此示例程序需要使用FastLED库
#define NUM_LEDS 30 // LED灯珠数量
#define DATA_PIN 9 // Arduino输出控制信号引脚
#define LED_TYPE WS2812 // LED灯带型号
#define COLOR_ORDER GRB // RGB灯珠中红色、绿色、蓝色LED的排列顺序
uint8_t max_bright = 128; // LED亮度控制变量,可使用数值为 0 ~ 255, 数值越大则光带亮度越高
CRGB leds[NUM_LEDS]; // 建立光带leds
void setup() {
Serial.begin(9600); // 启动串行通讯
delay(1000); // 稳定性等待
LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // 初始化光带
FastLED.setBrightness(max_bright); // 设置光带亮度
}
void loop() {
leds[0] = CRGB::Red; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show(); // 更新LED色彩
delay(500); // 等待500毫秒
leds[0] = CRGB::Black; // 设置光带中第一个灯珠颜色为红色,leds[0]为第一个灯珠,leds[1]为第二个灯珠
FastLED.show(); // 更新LED色彩
delay(500); // 等待500毫秒
}
/*
CRGB 颜色名称列表
CRGB::AliceBlue
CRGB::Amethyst
CRGB::AntiqueWhite
CRGB::Aqua
CRGB::Aquamarine
CRGB::Azure
CRGB::Beige
CRGB::Bisque
CRGB::Black
CRGB::BlanchedAlmond
CRGB::Blue
CRGB::BlueViolet
CRGB::Brown
CRGB::BurlyWood
CRGB::CadetBlue
CRGB::Chartreuse
CRGB::Chocolate
CRGB::Coral
CRGB::CornflowerBlue
CRGB::Cornsilk
CRGB::Crimson
CRGB::Cyan
CRGB::DarkBlue
CRGB::DarkCyan
CRGB::DarkGoldenrod
CRGB::DarkGray
CRGB::DarkGreen
CRGB::DarkKhaki
CRGB::DarkMagenta
CRGB::DarkOliveGreen
CRGB::DarkOrange
CRGB::DarkOrchid
CRGB::DarkRed
CRGB::DarkSalmon
CRGB::DarkSeaGreen
CRGB::DarkSlateBlue
CRGB::DarkSlateGray
CRGB::DarkTurquoise
CRGB::DarkViolet
CRGB::DeepPink
CRGB::DeepSkyBlue
CRGB::DimGray
CRGB::DodgerBlue
CRGB::FireBrick
CRGB::FloralWhite
CRGB::ForestGreen
CRGB::Fuchsia
CRGB::Gainsboro
CRGB::GhostWhite
CRGB::Gold
CRGB::Goldenrod
CRGB::Gray
CRGB::Green
CRGB::GreenYellow
CRGB::Honeydew
CRGB::HotPink
CRGB::IndianRed
CRGB::Indigo
CRGB::Ivory
CRGB::Khaki
CRGB::Lavender
CRGB::LavenderBlush
CRGB::LawnGreen
CRGB::LemonChiffon
CRGB::LightBlue
CRGB::LightCoral
CRGB::LightCyan
CRGB::LightGoldenrodYellow
CRGB::LightGreen
CRGB::LightGrey
CRGB::LightPink
CRGB::LightSalmon
CRGB::LightSeaGreen
CRGB::LightSkyBlue
CRGB::LightSlateGray
CRGB::LightSteelBlue
CRGB::LightYellow
CRGB::Lime
CRGB::LimeGreen
CRGB::Linen
CRGB::Magenta
CRGB::Maroon
CRGB::MediumAquamarine
CRGB::MediumBlue
CRGB::MediumOrchid
CRGB::MediumPurple
CRGB::MediumSeaGreen
CRGB::MediumSlateBlue
CRGB::MediumSpringGreen
CRGB::MediumTurquoise
CRGB::MediumVioletRed
CRGB::MidnightBlue
CRGB::MintCream
CRGB::MistyRose
CRGB::Moccasin
CRGB::NavajoWhite
CRGB::Navy
CRGB::OldLace
CRGB::Olive
CRGB::OliveDrab
CRGB::Orange
CRGB::OrangeRed
CRGB::Orchid
CRGB::PaleGoldenrod
CRGB::PaleGreen
CRGB::PaleTurquoise
CRGB::PaleVioletRed
CRGB::PapayaWhip
CRGB::PeachPuff
CRGB::Peru
CRGB::Pink
CRGB::Plaid
CRGB::Plum
CRGB::PowderBlue
CRGB::Purple
CRGB::Red
CRGB::RosyBrown
CRGB::RoyalBlue
CRGB::SaddleBrown
CRGB::Salmon
CRGB::SandyBrown
CRGB::SeaGreen
CRGB::Seashell
CRGB::Sienna
CRGB::Silver
CRGB::SkyBlue
CRGB::SlateBlue
CRGB::SlateGray
CRGB::Snow
CRGB::SpringGreen
CRGB::SteelBlue
CRGB::Tan
CRGB::Teal
CRGB::Thistle
CRGB::Tomato
CRGB::Turquoise
CRGB::Violet
CRGB::Wheat
CRGB::White
CRGB::WhiteSmoke
CRGB::Yellow
CRGB::YellowGreen
*/
代码二
#include "FastLED.h"
#define NUM_LEDS 30
#define DATA_PIN 9
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
uint8_t max_bright =128; // 范围0-255 亮度变量
CRGB leds[NUM_LEDS]; //建立光带对象
void setup(){
Serial.begin(9600);
delay(1000);
LEDS.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds,NUM_LEDS);//初始化
FastLED.setBrightness(max_bright);
}
void loop(){
for(int i=0;i<=NUM_LEDS;i++){
leds[i] = CRGB::Red; //第一个灯珠红色
FastLED.show() ;//更新颜色
delay(500);
leds[i] = CRGB::Black ; //熄灭
FastLED.show() ;
delay(500);
}
}
网友评论