OnChange(handler) with NeoPixels

I am trying to view the status LED on the exterior of an enclosed case with sensors externally connected. I lifted this partially from someone’s project here, and docs.

I have a rather large project going and this is the OnChange(handler) Neopixel pertinent code, everything compiles and works except this only follows the on status change flashing green to cyan, then stays static. I am assuming there is a better way to mirror system to neopixels.

#define PIXEL_PIN D6
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);



void ledChangeHandler(uint8_t r, uint8_t g, uint8_t b) {

    // Initial boot, control all teh LEDs
    if(!Particle.connected() && !has_booted) {
        for(uint8_t i=0; i<PIXEL_COUNT; i++)
            strip.setPixelColor(i, strip.Color(r, g, b));

        strip.show();

    }
}

void setup(){
    strip.begin(); // initialize the strip
    strip.show(); // initialize all pixels to 'off'
    RGB.onChange(ledChangeHandler);

    WiFiSetup();
    
    Particle.connect();
	while(!Particle.connected()) {
	 	Particle.process();
	 	delay(1);
	}

You may want to do the hookup (and prerequisites) in STARTUP() instead of setup()

And why are you explicitly stopping the update once connected?

  if(!Particle.connected() && !has_booted) {
    // only do this while NOT connected AND also NOT having booted (???)
    ...
  }

That way, you do ask for exactly the behaviour you don’t seem to like :wink:

1 Like

Are you overcomplicating things?

Did you try this very simple approach:

void ledChangeHandler(byte green, byte red, byte blue)
{
  strip.setPixelColor(0, red, green, blue);
  strip.show();
}
1 Like

God I wish it was this @BulldogLowell

So I tried that, and @ScruffR 's suggestion. First of which, the bool has_booted = false; is a global, then I call has_booted = true; at the end of setup I don’t see why that would be an issue, its just confirming it’s made it through setup without errors on any other sensors.

The best I can get it to do is follow the rapid boot flash from green to cyan, then it stays static.

Once you are connected to the cloud !Particle.connected() will become false and hence the if() condition will never ever become true again and never ever update the strip again - no matter what has_booted will be.

Because you only update the strip when both conditions (Particle.connected() and has_booted) are false. As soon one becomes true you stop updating the strip!

1 Like