ws2812 氛围灯问题
本帖最后由 ly4661870 于 2022-5-25 15:18 编辑#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
//#define RGB_1 "col-k5h"
#define BUTTON_1 "ButtonKey"
#include "FastLED.h" // 此示例程序需要使用FastLED库
#define TEXTE_1 "tex-c99"
#define NUM_LEDS 60 // LED灯珠数量
#define LED_DT 5 // Arduino输出控制信号引脚
#define LED_TYPE WS2812 // LED灯带型号
#define COLOR_ORDER RGB // RGB灯珠中红色、绿色、蓝色LED的排列顺序
uint8_t set=1 , set1;
uint8_t r1 ,g1 ,b1 ,L1=255;
uint8_t fromcolor=0 , tocolor=255 , h=20;
CRGB leds; // 建立光带leds
uint8_t colorIndex , jiezou=10;
int a ;
char auth[] = "a8ec5d1ee633";
int data ={ 0 , 127 , 213, 40 , 200 , 80, 160, 220 , 100 , 20};
BlinkerButton Button1("guan");
BlinkerButton Button2("kai");
BlinkerRGB RGB1("col_k5h");
BlinkerButton Button3("ok");
BlinkerSlider Slider1("from");
BlinkerSlider Slider3("set");
BlinkerSlider Slider2("to");
BlinkerSlider Slider4("jiezou");
BlinkerText Text1(TEXTE_1);
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "tap")
{set=0;
Button1.color("#000000");
Button1.text("关灯");
Button2.text("开灯");
Button2.print("off");
Button1.print("off");
Text1.print("", "");
Blinker.print("off","off") ;}
}
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "tap")
{set=7;
Button2.text("白光");
Button2.print("on");}
}
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "tap")
{set=set1;
Button3.text("已确定");
Button3.print("on");
}
}
void slider1_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
fromcolor= value;
}
void slider2_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
tocolor= value;
}
void slider4_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
jiezou= value;
}
void slider3_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
set1= value;
Button3.text("点我确定");
Button3.print("on");
switch(set1){
case 1 :Text1.print("单色模式", "可通过色盘选择指定颜色,同时可调节对应亮度");
Blinker.print("on","ok") ;break;
case 2 :Text1.print("彩色模式", "可通过两个HSV色调滑块改变颜色,亮度通过色盘调节");
Blinker.print("on","ok") ;break;
case 3 :Text1.print("单色呼吸灯", "能够自动改变呼吸灯的颜色,灯光节奏滑块调节速度");
Blinker.print("on","ok") ;break;
case 4 :Text1.print("单色呼吸灯", "可以通过调色盘改变呼吸灯颜色 灯光节奏滑块调节速度");
Blinker.print("on","ok") ;break;
case 5 :Text1.print("流水灯", "可自动改变流水灯颜色 灯光节奏滑块调节速度");
Blinker.print("on","ok") ;break;
case 6 :Text1.print("跑马灯", "HSV色调滑块调节颜色,色盘调节亮度 灯光节奏滑块调节速度");
Blinker.print("on","ok") ;break;
}
}
void rgb1_callback(uint8_t r_value, uint8_t g_value,
uint8_t b_value, uint8_t bright_value)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
r1 = r_value ;
g1 = g_value ;
b1 = b_value ;
L1 = bright_value;
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
Blinker.print("millis", BlinkerTime);
}
void setup() {
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化ws2812B灯带
LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(255);
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,HIGH);
// 初始化blinker
Blinker.begin(auth);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
RGB1.attach(rgb1_callback);
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
Slider3.attach(slider3_callback);
Slider4.attach(slider4_callback);
}
void loop() {
Blinker.run();
switch(set){
case 0 :
FastLED.clear();
FastLED.show();
break;
case 7 :
fill_solid(leds , NUM_LEDS , CRGB( 245,245,245));
FastLED.setBrightness(L1);
FastLED.show();
Blinker.delay(jiezou);
break;
case 1 :
fill_solid(leds , NUM_LEDS , CRGB( r1,g1,b1));
FastLED.setBrightness(L1);
FastLED.show();
Blinker.delay(jiezou);
break;
case 4 :
for ( int i=30; i <= 255; i++)
{
fill_solid(leds , NUM_LEDS , CRGB( r1 ,g1 ,b1));
FastLED.show();
FastLED.setBrightness(i);
Blinker.delay(jiezou);
Blinker.run();
}
for( int m=255 ;m >= 30; m--)
{
fill_solid(leds , NUM_LEDS , CRGB( r1 ,g1 ,b1));
FastLED.show();
FastLED.setBrightness(m);
Blinker.delay(jiezou);
Blinker.run();
}break;
case 3 :
h=h+31;
for (uint8_t i=90; i <= 254 ; i++ ){
fill_gradient(leds ,0 ,CHSV(h,255,i) ,NUM_LEDS-1, CHSV(h,255,i) , LONGEST_HUES);
Blinker.run();
FastLED.show();
Blinker.delay(jiezou);
}
for(uint8_t m=254;m >= 90 ; m-- ){
fill_gradient(leds ,0 ,CHSV(h,255,m) , NUM_LEDS-1 , CHSV(h,255,m) , LONGEST_HUES);
Blinker.run();
FastLED.show();
Blinker.delay(jiezou);
}break;
case 2 :
fill_gradient(leds ,0 ,CHSV(fromcolor,255,L1) ,NUM_LEDS-1 , CHSV(tocolor,255,L1) , LONGEST_HUES);
FastLED.show();
Blinker.run();
break;
case 5 :
int n;
uint8_t v ;
h=h+40;
for(n=0 ; n<=(NUM_LEDS-1) ;n++ ){
fill_gradient(leds ,n ,CHSV(h,255,L1) ,n, CHSV(h,255,L1) , LONGEST_HUES);
FastLED.show();
Blinker.delay(jiezou);
Blinker.run();
}
for(n=NUM_LEDS-1 ; n>=0;n-- ){
fill_gradient(leds ,n ,CHSV(h+100,255,L1) ,n, CHSV(h+100,255,L1) , LONGEST_HUES);
FastLED.show();
Blinker.delay(jiezou);
Blinker.run();
}
break;
case 6 :
for( uint8_t n=0 ; n <= NUM_LEDS-10 ; n++){
fill_gradient(leds ,n ,CHSV(fromcolor,255,L1) , n+9 , CHSV(tocolor,255,L1) , SHORTEST_HUES);
FastLED.show();
Blinker.delay(jiezou);
Blinker.run();
FastLED.clear();
}
for( uint8_th=NUM_LEDS-1 ;h >= 9 ;h--){
fill_gradient(leds ,h ,CHSV(fromcolor,255,L1) , h-9 , CHSV(tocolor,255,L1) , SHORTEST_HUES);
FastLED.show();
Blinker.delay(jiezou);
Blinker.run();
FastLED.clear();
}
break;
}
}
这段代码有些问题 功能基本都能实现 就是用调色盘控制颜色的时候有问题 ,麻烦大家帮看一看 哪里有问题
改了一下调色盘的key就可以修改了不知道为什么
页:
[1]