爆改车间主任github_blinker控制RGB LED示例

  • Post author:
  • Post category:其他


这是一个RGB控制示例,帮助大家更好的编写灯控程序。

注意:只有changeLight()函数在改变灯的状态,如果你是用的其他灯,只用在setup中进行初始化,在changeLight中改变灯的状态即可。

[mw_shl_code=arduino,true]

#define BLINKER_WIFI

#include

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();

}[/mw_shl_code]



版权声明:本文为weixin_29479561原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。