Blinker项目实例:Nodemcu驱动ws2812(附项目代码)-Arduino中文社区 - Powered by Discuz! Archiver

涟水河 发表于 2020-6-1 00:28

本帖最后由 涟水河 于 2020-6-2 11:15 编辑

搞定了,

这个可以编译通过的,还是楼主的,只是重新排了下

#include <ESP8266LLMNR.h>

#define BLINKER_WIFI
#include <Blinker.h>
#include <math.h>
char auth[] = "********";//IoT秘钥
char ssid[] = "***";//wifi名称
char pswd[] = "*******"; //wifi秘钥

bool tab = { false };
bool modestate = { false }; //工作模式选项卡
bool AutoBit = false; //判断手动模式是否有效
bool SingleAdjust = false; //判断逐个调节是否有效
int LEDserial = 0; //
int Offset = 30; //灯珠整体移动若干单位
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
#define PIN            5
#define NUMPIXELS      30
#define RGB_1 "RGBKey"
#define Tab_1 "tab-mode"
#define Number_1 "LEDNum"
#define Number_2 "LED_R"
#define Number_3 "LED_G"
#define Number_4 "LED_B"
BlinkerRGB WS2812(RGB_1);
BlinkerTab Tab1(Tab_1);//定义数据结构
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
BlinkerNumber Number1 (Number_1);
BlinkerNumber Number2 (Number_2);
BlinkerNumber Number3 (Number_3);
BlinkerNumber Number4 (Number_4); //*************************************************************//选项卡触发函数
void tab1_callback(uint8_t tab_set) {   //BLINKER_LOG("get tab set: ", tab_set);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
switch (tab_set)    {
    case BLINKER_CMD_TAB_0 :
      tab = true;            //BLINKER_LOG("tab 0 set");
      AutoBit = !AutoBit; //手动位取反
      break;
    case BLINKER_CMD_TAB_1 :
      tab = true;            //BLINKER_LOG("tab 1 set");
      Mode1();//开启Mode1,灯光模式1
      break;
    case BLINKER_CMD_TAB_2 :
      tab = true;            //BLINKER_LOG("tab 2 set");
      SingleAdjust = !SingleAdjust; //开启Mode2,灯光模式2            break;
    case BLINKER_CMD_TAB_3 :
      tab = true;            //BLINKER_LOG("tab 3 set");
      Mode3();//开启Mode3,流水灯模式
      break;
    case BLINKER_CMD_TAB_4 :
      tab = true;            //BLINKER_LOG("tab 4 set");
      break;
    default:
      break;
}
}
void tab1_feedback() {

for (uint8_t num = 0; num < 5; num++)    {
    if (tab)      {
      Tab1.tab(num);
      tab = false;
    }
}
Tab1.print();
}//*******************************************************************//自由模式整体调节/逐个调节服务函数
void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{   
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));   
if (AutoBit==true)
   {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));         
   Mode0(r_value,g_value,b_value,bright_value);
   //return NULL;
   }   
else if (SingleAdjust == true)
{
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    Mode2(r_value, g_value, b_value, bright_value);
}
}
//********************************************************************//自由控制(整体调节)Mode0
void Mode0(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) {
pixels.setBrightness(bright_value);
for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, r_value, g_value, b_value);
}
pixels.show();
Number2.print(r_value);
Number3.print(g_value);
Number4.print(b_value);
}
//********************************************************************//七色模式Mode1
void Mode1() {
int r = 0;    int g = 0;    int b = 0;    int i = 0;    int full = NUMPIXELS;
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
for (int i = 0; i < NUMPIXELS; i++) {
    if (i < full / 3) {
      r = 255;             //********************************************************************//逐个调节Mode2
      g = ceil(255 * 3 * i / full);
      b = 0;
    } else if (i < full / 2) {
      r = ceil(750 - i * (250 * 6 / full));
      g = 255;
      b = 0;
    } else if (i < full * 2 / 3) {
      r = 0;
      g = 255;
      b = ceil(i * (250 * 6 / full) - 750);
    } else if (i < full * 5 / 6) {
      r = 0;
      g = ceil(1250 - i * (250 * 6 / full));
      b = 255;
    } else {
      r = ceil(150 * i * (6 / full) - 750);
      g = 0;
      b = 255;
    }             pixels.setPixelColor(i, pixels.Color(r, g, b));
}    pixels.show();
}
void Mode2(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) {
pixels.setBrightness(bright_value);   //*******************************************************************//护眼照明模式 黄光Mode3
pixels.setPixelColor(LEDserial, r_value, g_value, b_value);
pixels.show();
LEDserial = LEDserial + 1;
LEDserial = LEDserial % NUMPIXELS;
Number2.print(r_value);
Number3.print(g_value);
Number4.print(b_value);
}
void Mode3() {
int bright_value = 200;         //*******************************************************************//显示数值
int r_value = 255;
int g_value = 215;
int b_value = 0;
pixels.setBrightness(bright_value);
for (int i = 0; i < NUMPIXELS; i++) {
    pixels.setPixelColor(i, r_value, g_value, b_value);
}               pixels.show();
Number2.print(r_value);
Number3.print(g_value);
Number4.print(b_value);
}
void dataRead(const String & data) {
uint32_t BlinkerTime = millis();      //*********************************************************************
Number1.print(BlinkerTime / 1000);
}
void setup() {
Serial.begin(115200);    BLINKER_DEBUG.stream(Serial);      Blinker.attachData(dataRead);   pinMode(LED_BUILTIN, OUTPUT);//引脚设置为输出模式
digitalWrite(LED_BUILTIN, LOW);   Blinker.begin(auth, ssid, pswd);       pixels.begin();//开启pixel对ws2812输出
WS2812.attach(ws2812_callback);
Tab1.attach(tab1_callback,
            tab1_feedback);
}
void loop() {
Blinker.run();
}





韵野 发表于 2021-1-25 17:44

涟水河 发表于 2020-6-1 00:28
搞定了,

这个可以编译通过的,还是楼主的,只是重新排了下

能帮我看下,问题出在哪吗?
提示Arduino:1.8.13 (Windows 7), 开发板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"





















E:\单片机\氛围灯\1.25\1.25\1.251\1.251.ino: In function 'void tab1_callback(uint8_t)':

1.251:45:13: error: 'Mode1' was not declared in this scope

1.251:53:13: error: 'Mode3' was not declared in this scope

E:\单片机\氛围灯\1.25\1.25\1.251\1.251.ino: In function 'void button1_callback(const String&)':

1.251:138:50: error: 'SET_RGB' was not declared in this scope

1.251:139:27: error: 'SET_RGB' was not declared in this scope

E:\单片机\氛围灯\1.25\1.25\1.251\1.251.ino: In function 'void Mode0(uint32_t, int)':

1.251:143:18: error: 'strip' was not declared in this scope

1.251:149:37: error: a function-definition is not allowed here before '{' token

1.251:355:1: error: expected '}' at end of input

exit status 1

'Mode1' was not declared in this scope



在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。

#include <Adafruit_NeoPixel.h>

#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#include <Blinker.h>
#include <Adafruit_NeoPixel.h>

char auth[] = "64e4f6335177";
char ssid[] = "LYH";
char pswd[] = "lyh788799";

bool tab = { false };
bool modestate = { false }; //工作模式选项卡
bool AutoBit = false; //判断手动模式是否有效
bool SingleAdjust = false; //判断逐个调节是否有效
int LEDserial = 15; //
int Offset = 60; //灯珠整体移动若干单位
#define PIN 15//DIN PIN (GPIO15, D8)
#define NUMPIXELS 60// Number of you led
#define RGB_1 "RGBKey"
#define Tab_1 "tab-mode"
#define Number_1 "LEDNum"
#define Number_2 "LED_R"
#define Number_3 "LED_G"
#define Number_4 "LED_B"
BlinkerRGB WS2812(RGB_1);
BlinkerButton Button1("key");
BlinkerTab Tab1(Tab_1);//定义数据结构
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
BlinkerNumber Number1 (Number_1);
BlinkerNumber Number2 (Number_2);
BlinkerNumber Number3 (Number_3);
BlinkerNumber Number4 (Number_4); //*************************************************************//选项卡触发函数
void tab1_callback(uint8_t tab_set) {   //BLINKER_LOG("get tab set: ", tab_set);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
switch (tab_set)    {
    case BLINKER_CMD_TAB_0 :
      tab = true;            //BLINKER_LOG("tab 0 set");
      AutoBit = !AutoBit; //手动位取反
      break;
    case BLINKER_CMD_TAB_1 :
      tab = true;            //BLINKER_LOG("tab 1 set");
      Mode1();//开启Mode1,
      break;
    case BLINKER_CMD_TAB_2 :
      tab = true;            //BLINKER_LOG("tab 2 set");
      SingleAdjust = !SingleAdjust; //开启Mode2,         
      break;
    case BLINKER_CMD_TAB_3 :
      tab = true;            //BLINKER_LOG("tab 3 set");
      Mode3();//开启Mode3,
      break;
    case BLINKER_CMD_TAB_4 :
      tab = true;            //BLINKER_LOG("tab 4 set");
      break;
    default:
      break;
}
}
void tab1_feedback() {

for (uint8_t num = 0; num < 5; num++)    {
    if (tab)      {
      Tab1.tab(num);
      tab = false;
    }
}
Tab1.print();
}

/***************配网函数**************/
bool WIFI_Status = true;
void smartConfig()//配网函数
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();//等待手机端发出的用户名与密码
while (1)
{
    Serial.print(".");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);                     
    digitalWrite(LED_BUILTIN, LOW);   
    delay(1000);                     
    if (WiFi.smartConfigDone())//退出等待
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;
    }
}
}
void WIFI_Set()//
{
    //Serial.println("\r\n正在连接");
    int count = 0;
    while(WiFi.status()!=WL_CONNECTED)
    {
      if(WIFI_Status)
      {
            Serial.print(".");
            digitalWrite(LED_BUILTIN, HIGH);
            delay(500);                     
            digitalWrite(LED_BUILTIN, LOW);   
            delay(500);               
            count++;
            if(count>=5)//5s
            {
                WIFI_Status = false;
                Serial.println("WiFi连接失败,请用手机进行配网");
            }
      }
      else
      {
            smartConfig();//微信智能配网
      }
   }
    /* Serial.println("连接成功");
   Serial.print("IP:");
   Serial.println(WiFi.localIP());*/
}



int counter = 0;
int LED_R=0,LED_G=0,LED_B=0,LED_Bright=255;// RGB和亮度
bool LED_Flag = false;

void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    Blinker.print(state);
    //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      LED_Flag = !LED_Flag;
      if(LED_Flag) SET_RGB(255,255,255,LED_Bright);
      else SET_RGB(0,0,0,0);
}

void Mode0(uint32_t color, int wait){
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //Set pixel's color (in RAM)
    strip.show();                        //Update strip to match
    delay(wait);                           //Pause for a moment
}

void Mode1(uint32_t color, int wait){
for(int a=0; a<10; a++) {// Repeat 10 times...
    for(int b=0; b<3; b++) { //'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
      strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show(); // Update strip with new contents
      delay(wait);// Pause for a moment
    }
}
}

void Mode2(int wait){
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      // Offset pixel hue by an amount to make one full revolution of the
      // color wheel (range of 65536) along the length of the strip
      // (strip.numPixels() steps):
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
      // optionally add saturation and value (brightness) (each 0 to 255).
      // Here we're using just the single-argument hue variant. The result
      // is passed through strip.gamma32() to provide 'truer' colors
      // before assigning to each pixel:
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
    delay(wait);// Pause for a moment
}
}

void Mode3(int wait){
int firstPixelHue = 0;   // First pixel starts at red (hue 0)
for(int a=0; a<30; a++) {// Repeat 30 times...
    for(int b=0; b<3; b++) { //'b' counts from 0 to 2...
      strip.clear();         //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for(int c=b; c<strip.numPixels(); c += 3) {
      // hue of pixel 'c' is offset by an amount to make one full
      // revolution of the color wheel (range 65536) along the length
      // of the strip (strip.numPixels() steps):
      int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
      uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
      strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
      }
      strip.show();                // Update strip with new contents
      delay(wait);               // Pause for a moment
      firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
    }
}
}
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
    counter=counter;
    Number1.print(counter);
}


void SET_RGB(int R,int G,int B,int bright)
{
    for (uint16_t i = 0; i < NUMPIXELS; i++) //把灯条变色
    {
      pixels.setPixelColor(i,R,G,B);
    }
    pixels.setBrightness(bright);//亮度
    pixels.show();    //送出显示
}
//APP RGB颜色设置回调
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);
    LED_Bright = bright_value;
    SET_RGB(r_value,g_value,b_value,LED_Bright);
}


//小爱电源类回调
void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
      //digitalWrite(LED_BUILTIN, LOW);
      SET_RGB(255,255,255,255);      
      BlinkerMIOT.powerState("on");
      BlinkerMIOT.print();
    }
    else if (state == BLINKER_CMD_OFF) {
      //digitalWrite(LED_BUILTIN, HIGH);
      SET_RGB(255,255,255,0);
      BlinkerMIOT.powerState("off");
      BlinkerMIOT.print();
    }
}
//小爱设置颜色回调
void miotColor(int32_t color)
{
    BLINKER_LOG("need set color: ", color);
    int colorR,colorG,colorB;
    colorR = color >> 16 & 0xFF;
    colorG = color >>8 & 0xFF;
    colorB = color       & 0xFF;

    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
    SET_RGB(colorR,colorG,colorB,LED_Bright);
    //pixelShow();

    BlinkerMIOT.color(color);//反馈小爱控制状态
    BlinkerMIOT.print();
}
//小爱调亮度回调
void miotBright(const String & bright)
{
    BLINKER_LOG("need set brightness: ", bright);

    int colorW = bright.toInt();

    BLINKER_LOG("now set brightness: ", colorW);
    LED_Bright = colorW;
    SET_RGB(LED_R,LED_G,LED_B,LED_Bright);
    Serial.printf("亮度调节中...%d",colorW);
   
    BlinkerMIOT.brightness(colorW);//反馈小爱控制状态
    BlinkerMIOT.print();
}












void setup() {
    // 初始化串口
    Serial.begin(115200);
    pixels.begin();//WS2812初始化
    pixels.show();
    pinMode(LED_BUILTIN, OUTPUT);
    #if defined(BLINKER_PRINT)
    BLINKER_DEBUG.stream(BLINKER_PRINT);
    #endif
    Serial.begin(115200);    BLINKER_DEBUG.stream(Serial);      Blinker.attachData(dataRead);   pinMode(LED_BUILTIN, OUTPUT);//引脚设置为输出模式
digitalWrite(LED_BUILTIN, LOW);   Blinker.begin(auth, ssid, pswd);       pixels.begin();//开启pixel对ws2812输出
WS2812.attach(ws2812_callback);
Tab1.attach(tab1_callback,
            tab1_feedback);
   }
    // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
    // Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.

strip.begin();         // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show();            // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)         
   
    WIFI_Set();
    // 初始化blinker
    Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());

   
    Blinker.attachData(dataRead);//如果没有被绑定的组件被触发就执行这个回调
    Button1.attach(button1_callback);//Button1这个组件被触发就执行这个回调
    RGB1.attach(rgb1_callback);//注册调节颜色的回调函数
    BlinkerMIOT.attachPowerState(miotPowerState);
    BlinkerMIOT.attachColor(miotColor);//小爱调节颜色
    BlinkerMIOT.attachBrightness(miotBright);//小爱调节RGB亮度
}

void loop()
{
// Fill along the length of the strip in various colors...
colorWipe(strip.Color(255,   0,   0), 50); // Red
colorWipe(strip.Color(0, 255,   0), 50); // Green
colorWipe(strip.Color(0,   0, 255), 50); // Blue

// Do a theater marquee effect in various colors...
theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
theaterChase(strip.Color(0,   0, 127), 50); // Blue, half brightness

rainbow(10);             // Flowing rainbow cycle along the whole strip
theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
   }
   Blinker.run();

}

chenhao189 发表于 2022-5-13 07:50

像优秀的人学习

chenhao189 发表于 2022-5-17 21:49

优秀的人在哪里都是闪闪发光

chenhao189 发表于 2022-5-17 21:55

大神点灯科技的界面配置代码有吗

chenhao189 发表于 2022-5-17 22:04

涟水河 发表于 2020-6-1 00:28
搞定了,

这个可以编译通过的,还是楼主的,只是重新排了下

点灯科技手机端界面配置代码呢有吗?

chenhao189 发表于 2022-5-18 07:06

MUHEI 发表于 2020-3-17 00:23
上面那个库文件的问题解决了,可是还是有好多错误,貌似上面的程序的格式有错,请问可以给我源程序吗? ...

手机端界面配置代码有吗

chenhao189 发表于 2022-5-18 07:07

YLGZS 发表于 2020-3-11 08:36
再来个小爱同学就完美了

手机端界面配置代码有吗

chenhao189 发表于 2022-5-18 22:20

MUHEI 发表于 2020-3-17 00:23
上面那个库文件的问题解决了,可是还是有好多错误,貌似上面的程序的格式有错,请问可以给我源程序吗? ...

点灯科技界面配置代码有吗

chenhao189 发表于 2022-5-19 22:10

按键的组件名都是啥呀,方便说一下嘛
页: 1 [2] 3
查看完整版本: Blinker项目实例:Nodemcu驱动ws2812(附项目代码)