kitesurfer1404 / WS2812FX

WS2812 FX Library for Arduino and ESP8266
MIT License
1.58k stars 344 forks source link

all lights break down after about one minute #266

Closed IcyFeather233 closed 3 years ago

IcyFeather233 commented 3 years ago

Hello, I really apreciate the ws2812fx, however I meet a problem recently. I want to make a new shop sign and I use ws2812fx to control my ws2812, but I meet 2 problems.

  1. When I set the light to certain static color in segments first, and then I want to set them all in rainbow, I don't know how to make it, it always turned out that only one of the segments is in rainbow status.
  2. When I use mqtt to control the status, the ws2812 will break down after running normally for a minute, first it will stop the animation, then all lights turn down and I lose the control, after a minute I can restart it and continue to control.

include

include

include

include

include

include

include

include "string.h"

const char ssid = "xxx"; const char password = "xxx"; const char* mqtt_server = "xxx";

const byte ledPin = 2;

define LED_COUNT 353

define TIMER_MS 5000

WiFiClient espClient; PubSubClient client(espClient); WS2812FX ws2812fx = WS2812FX(LED_COUNT, ledPin, NEO_RGB + NEO_KHZ800);

int nowLeft = 0; int nowRight = 100; int idx = 0;

void callback(char topic, byte payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); Serial.println();

char receivedChar = (char)payload; String str_r = receivedChar; Serial.println("the str_r before is :" + str_r); str_r = str_r.substring(0,length); Serial.println("the str_r after is :" + str_r); String str_t = topic; Serial.println("the str_t is :" + str_t);

//---------------------------------------------------------------------- //-------------------------segments------------------------------------- if(str_t == "character") { char ch = str_r[0]; if(ch >= 'a' && ch <= 'z') { ch -= ('a' - 'A'); } Serial.println(ch); switch(ch) { case 'S': Serial.println("enter switch"); nowLeft = 0; nowRight = 55; idx = 0; break; case 'C': nowLeft = 56; nowRight = 100; idx = 1; break; case 'U': nowLeft = 101; nowRight = 162; idx = 2; break; case 'M': nowLeft = 163; nowRight = 227; idx = 3; break; case 'A': nowLeft = 228; nowRight = 253; idx = 4; break; case 'K': nowLeft = 254; nowRight = 289; idx = 5; break; case 'E': nowLeft = 290; nowRight = 317; idx = 6; break; case 'R': nowLeft = 318; nowRight = 352; idx = 7; break; } } //---------------------------------------------------------------------- //-------------------------light or not------------------------------------ if(str_t == "ledStatus") { if (receivedChar[0] == '1') {
ws2812fx.setBrightness(100); } if (receivedChar[0] == '0') {
ws2812fx.setBrightness(0); } } //---------------------------------------------------------------------- //-------------------------color------------------------------------- if(str_t == "ledColor") { String str_color = str_r.substring(1,7); // Serial.println(str_color); String str_color_convert = str_color.substring(2,4) + str_color.substring(0,2) + str_color.substring(4,6); // Serial.println(str_color_convert); char offset; uint32_t colorhex = strtol(str_color_convert.c_str(), &offset, 16); // Serial.println(colorhex); // ws2812fx.setColor(colorhex); ws2812fx.setSegment(idx, nowLeft, nowRight, FX_MODE_STATIC, colorhex, 1000, false); } //---------------------------------------------------------------------- //-------------------------brightness------------------------------------ if(str_t == "ledBrt") { char offset; uint32_t b = strtol(str_r.c_str(), &offset, 10) * (255.0/100.0); Serial.println(b); ws2812fx.setBrightness(b); } //---------------------------------------------------------------------- //-------------------------status rotate---------------------------------- if(str_t == "rotate") { if (receivedChar[0] == '1') {
ws2812fx.setMode(FX_MODE_RAINBOW_CYCLE); } if (receivedChar[0] == '0') {
ws2812fx.setMode(FX_MODE_STATIC); } } Serial.println(); }

void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); if (client.connect("ESP8266 Client")) { Serial.println("connected"); client.subscribe("ledStatus");
client.subscribe("ledColor"); client.subscribe("ledBrt"); client.subscribe("character"); //new function client.subscribe("rotate"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } }

void setup() { // put your setup code here, to run once: Serial.begin(9600);

client.setServer(mqtt_server, 1883); client.setCallback(callback);
//---------------------------------------------------------------------------------- ws2812fx.init(); ws2812fx.setBrightness(0); ws2812fx.setColor(0x007BFF); ws2812fx.setMode(FX_MODE_STATIC); ws2812fx.start();

}

void loop() { ws2812fx.service(); //-----------------------------------mqtt client---------------------------------------- if (!client.connected()) { reconnect(); } client.loop(); //--------------------------------------------------------------------------------------

}

moose4lord commented 3 years ago

There are a couple problems with your sketch.

1) you should initialize the segments in the setup() function

  ws2812fx.setSegment(0,   0,  55, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(1,  56, 100, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(2, 101, 162, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(3, 163, 227, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(4, 228, 253, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(5, 254, 289, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(6, 290, 317, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);
  ws2812fx.setSegment(7, 318, 352, FX_MODE_STATIC, 0x007BFF, 1000, NO_OPTIONS);

2) when you change the color or mode of a segment, you should be using the setColor() and setMode() functions that take a segment index as a parameter.

ws2812fx.setColor(idx, colorhex); // idx is the segment index

Otherwise you'll only be changing segment 0.