NEOPixel of C++ Question

I have a question which is about NeoPixel LED string programming, but I’m sure will more likely turn out to be due to my foggy grasp of how C++ pointers and references really work.

I’ve boiled down the code from a larger program. Boiled down, the code is roughly as below ( I may have garbled things a little, but the essentials are there).

The problem I bring here is that the function allOnNow works, but allSoftOnNow crashes the photon. Can anyone explain why? As you can see, allOnNow puts the LEDs on instantly, allSoftOnNow tries to do a short fade up. Yet, they both address the LED string in the same fashion so why does one work and the other not? Thanks for any insights on this…
Alan T.

static Adafruit_NeoPixel porchLites(120, D6, WS2812B); // Declare a 120 LED string fed via D6

void setup() {
    //  Setup the LED string
    porchLites.begin();
    delay(150);
    porchLites.show(); //  Clears the LEDs of any random power up rubbish.
}

void loop() {
    allOnNow(&porchLites);
    allSoftOnNow(&porchLites);
}

   void allOnNow(Adafruit_NeoPixel *leds) {
    uint8_t r,g,b=0;
	//  Do the whole string
	for (int i=0;i<leds->numPixels()-1;i++)
	{
	    r=192; //currentColour.redLevel;
	    g=192; //currentColour.greenLevel;
	    b=192; //currentColour.blueLevel;
	    leds->setPixelColor(i,r,g,b);
	}    
	leds->show();
}

   void allSoftOnNow(Adafruit_NeoPixel *leds) {
        uint8_t r,g,b=0;
	//  Do the whole string
	for (int f=0;f<=leds->numPixels()-1;f++) {
          for (int i=0;i<leds->numPixels()-1;i++) {  	        {
	    r=f; //currentColour.redLevel;
            g=f; //currentColour.greenLevel;
            b=f; //currentColour.blueLevel;
    	    leds->setPixelColor(i,r,g,b);
          }
          leds->show();
    	}    
	
    }

What exactly do you mean by that? What behavior are you seeing. I tried your code, and it doesn’t crash my Photon. It also doesn’t do what you say you want to do ( a slow fade-in). There’s nothing in the code you show for allSoftOnNow() that would cause a fade-in. Something like this would give you a fade-in (but you shouldn’t be calling it over and over in loop),

void allSoftOnNow(Adafruit_NeoPixel *leds) {
    uint8_t r,g,b=0;
 	//  Do the whole string
 	for (int f=0;f<=leds->numPixels()-1;f++) {
       for (int i=0;i<leds->numPixels()-1;i++) {
 	       r=f; //currentColour.redLevel;
           g=f; //currentColour.greenLevel;
           b=f; //currentColour.blueLevel;
     	   leds->setPixelColor(i,r,g,b);
       }
       leds->show();
	   delay(50);
     }
}

Also, since porchLites is a global variable, there’s no need to pass a pointer to it at all. You can replace all the leds-> with porchLites. and have no parameter for allSoftOnNow()

1 Like

@Ric - thanks for the reply.

I apologise, I incorrectly copied my code from the real program, of course in the real softOn function in my full program the r, g, and b values are derived from the loop variable f (have corrected that here now). Also, I’m not calling it over and over from loop() in reality, just put that in here so as to - not all that successfully :slight_smile: - make a complete example.

The functions do need to take a reference to the LED string, because in the real application, this Photon is actually controlling three separate LED strings and I don’t want to have three sets of functions for controlling them.

Looking at your corrected version of the code which doesnt crash your photon, I’m wondering if the delay in your loop makes the difference? In my case, the crash manifests as the Photon stops normal processing and goes from “breathing blue” mode into rapid blue flash mode until you reset it. Perhaps there is a race condition somewhere in the library code that can’t cope with repeat calls at that rate? I did at one time have a delay in the real code, but only 2ms - so I will try a larger value and report back.

Many thanks for your reply. Will update soon.

Best
Alan T.

Ric. That does seem to be it - a delay of 7ms in that loop makes the difference. Makes the fade up a little slower than I wanted, but not a real problem. Many thanks for your help with this.

Best
Alan T.

hmm… I can’t duplicate that - even without that delay, my Photon doesn’t crash.

That’s odd. If I remove the delay mine still crashes. Are you using the version of the NeoPixel libary offered on the Web IDE (V0.0.13)? My Photon firmware rev is 0.6.2 Unless either of those is different I cant imagine why the difference :confused: Might be some wacky issue with my photon I suppose, but ufortunately I dont have a spare Photon to try as a tie breaker, but perhaps next time I get a new one, I’ll do a quick test on that.

I guess the main thing is I have a workaround, but…never comfy not to know the root cause :slight_smile: Thanks again.
Best
Alan T

I am using that same library, and my Photon is running 0.6.3. This is the code I tested,

#include "neopixel.h"

 static Adafruit_NeoPixel porchLites(120, D6, WS2812B); // Declare a 120 LED string fed via D6

 void setup() {
     //  Setup the LED string
     porchLites.begin();
     delay(150);
     porchLites.show(); //  Clears the LEDs of any random power up rubbish.
	 delay(2000);
 }

 void loop() {
     allOnNow(&porchLites);
     allSoftOnNow(&porchLites);
 }

    void allOnNow(Adafruit_NeoPixel *leds) {
     uint8_t r,g,b=0;
 	//  Do the whole string
 	for (int i=0;i<leds->numPixels()-1;i++)
 	{
 	    r=192; //currentColour.redLevel;
 	    g=192; //currentColour.greenLevel;
 	    b=192; //currentColour.blueLevel;
 	    leds->setPixelColor(i,r,g,b);
 	}
 	leds->show();
 }

    void allSoftOnNow(Adafruit_NeoPixel *leds) {
    	uint8_t r,g,b=0;
 		//  Do the whole string
 		for (int f=0;f<=leds->numPixels()-1;f++) {
    		for (int i=0;i<leds->numPixels()-1;i++) {
 				r=f; //currentColour.redLevel;
    			g=f; //currentColour.greenLevel;
    			b=f; //currentColour.blueLevel;
    			leds->setPixelColor(i,r,g,b);
    		}
    	leds->show();
     	}
    }