#include <Blinker.h>
#include <C:\Users\宁爸爸\Documents\Arduino\libraries\Adafruit_NeoPixel.h>
#define PIN D6
#define BRIGHTNESS 50
#define BLINKER_WIFI
char auth[] = "6abe9fd75ce2";
char ssid[] = "Mr_Ning";
char pswd[] = "bcptdtptp";
int states = 0, r,g,b,l,Direction = -10;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
// 新建组件对象
BlinkerButton Button1 ("Power");
BlinkerButton Button2 ("Breathing_light");
BlinkerButton Button3 ("rainbow");
BlinkerButton Button4 ("rainbowcycle");
BlinkerRGB RGB1 ("RGBKey");
int counter = 0;
// 按下按键1即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
if ( states = 0 )
{
states = 1;
colorWipe(strip.Color(250, 250, 250), 150);
r = 250;
g = 250;
b = 250;
l = 150;
}
else
{
states = 0;
colorWipe(strip.Color(0, 0, 0), 0);
}
}
// 按下按键2即会执行该函数
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
BLINKER_LOG("Breathing_light!!");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
states = 2;
}
// 按下按键3即会执行该函数
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
BLINKER_LOG("rainbow");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
states = 3;
}
// 按下按键4即会执行该函数
void button4_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
BLINKER_LOG("rainbowcycle");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
states = 4;
}
// 颜色拾取模块数据发生变化即会执行该函数
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);
colorWipe(strip.Color(r_value, g_value, b_value), 20);
r = r_value;
g = g_value;
b = b_value;
l = int(bright_value*0.58);
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
//LED初始化
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show();
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
RGB1.attach(rgb1_callback);
}
void loop()
{
Blinker.run();
switch (state)
{
case 2 :
{
if ( l <= 10 )
Direction = 10;
if ( l >= 140 )
Direction = -10;
l += Direction;
colorWipe(strip.Color(r*(l/250), g*(l/250), b*(l/250)), 20);
}
case 3 : rainbow(10);
case 4 : rainbowCycle(10);
}
}
//LED
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255 ));
}
strip.show();
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
#error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
|