blinker控制RGB LED示例
这是一个RGB控制示例,帮助大家更好的编写灯控程序。注意:只有changeLight()函数在改变灯的状态,如果你是用的其他灯,只用在setup中进行初始化,在changeLight中改变灯的状态即可。
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "xxxxxxxxxxx";
char ssid[] = "xxxxx";
char pswd[] = "xxxxxxxxxx";
#define RGB_1 "RGBKey"
BlinkerRGB RGB1(RGB_1);
bool switch_state = false;
int red=0;
int green=0;
int blue=0;
int bright=0;
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));
red=r_value;
green=g_value;
blue=b_value;
bright=bright_value;
changeLight();
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
RGB1.brightness(bright);
RGB1.print(red, green, blue);
}
void switch_callback(const String & state)
{
BLINKER_LOG("get switch state: ", state);
if (state == BLINKER_CMD_ON) {
switch_state = true;
bright=255;
changeLight();
BUILTIN_SWITCH.print("on");
}
else if (state == BLINKER_CMD_OFF) {
switch_state = false;
bright=0;
changeLight();
digitalWrite(LED_BUILTIN, LOW);
BUILTIN_SWITCH.print("off");
}
}
void heartbeat()
{
if (switch_state) BUILTIN_SWITCH.print("on");
else BUILTIN_SWITCH.print("off");
RGB1.brightness(bright);
RGB1.print(red, green, blue);
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void changeLight(){
int red_temp=map(red*bright/255,0,255,1023,0);
int green_temp= map(green*bright/255,0,255,1023,0);
int blue_temp= map(blue*bright/255,0,255,1023,0);
BLINKER_LOG("red_temp: ", red_temp);
BLINKER_LOG("green_temp: ", green_temp);
BLINKER_LOG("blue_temp: ", blue_temp);
analogWrite(D8,red_temp);
analogWrite(D9,green_temp);
analogWrite(D10,blue_temp);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
BUILTIN_SWITCH.attach(switch_callback);
RGB1.attach(rgb1_callback);
}
void loop()
{
Blinker.run();
}
可以分享一下blinker软件中界面的设置吗?谢谢啦
寝室咯 发表于 2019-9-12 09:58
可以分享一下blinker软件中界面的设置吗?谢谢啦
就是一个颜色拾取组件,没了,不过这个程序是2.2.7使用的,不知道2.2.6正常不 本帖最后由 carlbeven 于 2019-9-18 20:03 编辑
coloz老师,能否写一个教程?开关按键:RGB循环变颜色的;颜色拾取:使用拾色按键选取颜色,调节亮度并且保持该颜色、亮度。折腾了许久不懂如何中断RGB循环颜色和解决blinker app端接受数据延迟的问题。谢谢~ carlbeven 发表于 2019-9-18 19:55
coloz老师,能否写一个教程?开关按键:RGB循环变颜色的;颜色拾取:使用拾色按键选取颜色,调节亮度并且保 ...
https://github.com/blinker-iot/Blinker_PRO_SMART_LAMP
页:
[1]