Trying to check if a Neopixel is on or off

Hi there. I’m working on a project involving some Adafruit Neopixel LEDs and the Neopixel library. In the code, I have a section that I would like to run only if the a pixel is turned off. If the pixel is on, I would like that particular code to be skipped. This should be easy with a simple if statement, but it’s not working as I thought it would. The relevant section of code is as follows:

       int randVar = rand() % 30 + 3;    //A number of pixels from 3-33 will light up 
       for (int j=0; j< randVar; j++)
       {
         int randPos = rand() % 120;
         if (getPixelColor(randPos) == 0x000000)
         {     
             setPixelColor(randPos, Wheel(rand() % 255));
             show();
         }
       }

To my knowledge, getPixelColor(i) outputs a combined hex value of each RGB value of pixel i. In other words, if the pixel is off, the RGB components would be (0x00, 0x00, 0x00). getPixelColor would then return 0x000000. The if statement should check for this and then only run the setPixelColor on pixels that are off. But it doesn’t seem to differentiate, and will change the color of pixels that are already on. Can someone tell me what’s wrong here, or what I can do differently? Thank you!

1 Like
        for (int j=0; j< 120; j++)
        {
          int randPos = rand() % 120;
          if (getPixelColor(randPos) == 0x000000)
          {     
              setPixelColor(randPos, Wheel(rand() % 255));
              show();
          }
        }

Why the for loop? You never use ‘j’.

(BTW, did you see what I did with the code tags there? Why didn’t you do that? )

Sorry, I’m a noob here. Edited the original to fix it. The for loop was for a piece of code I neglected to put in (wasn’t really relevant to the question at hand, I don’t have the exact code with me at work… whoops I should be working…). Fixed that too.

Do you have a strip object properly initialized/

Do you mean strip.getPixelColor() and strip.setPixelColor()?

Have you tried doing uint32_t black = strip.Color(0, 0, 0); and comparing your getPixelColor() with black rather than 0x0? (Not sure if brightness is not coded into the 4 bytes)

J-M-L:
Do you have a strip object properly initialized/

Do you mean strip.getPixelColor() and strip.setPixelColor()?

Have you tried doing uint32_t black = strip.Color(0, 0, 0); and comparing your getPixelColor() with black rather than 0x0? (Not sure if brightness is not coded into the 4 bytes)

The strip object is properly initialized. This is part of a much larger working set of code, most of which is lifted from the adafruit guide: Put it all together... | Multi-tasking the Arduino - Part 3 | Adafruit Learning System.

As for strip.getPixelColor(), I'm actually a little confused about all these classes and whatnot. I thought I needed a strip. in front of these kind of calls, but apparently this works without it? setPixelColor works in this context without "strip." at least, but I could try putting it in front of getPixelColor too. I don't think one would work and the other wouldn't though.

I will try what you've suggested though. Part of the issue is that I haven't been able to find anywhere the exact syntax of the output of the getPixelColor command, so this may help. Thanks.

As for strip.getPixelColor(), I’m actually a little confused about all these classes and whatnot. I thought I needed a strip. in front of these kind of calls, but apparently this works without it?

Well, presumably, you’ve got a function called “setPixelColor”, that isn’t a method of the object “strip”.
But we can’t see your code, so we’re just guessing.

Good luck.

Here's the full code then. I didn't post it because I didn't have it at the time and the other 500 lines didn't seem relevant, but take a look if it'll help. It's apparently too big to post here but I put it on Github: LED_Strip/LED_Code_1 at master · dlovinger/LED_Strip · GitHub. The relevant bits are at line 311.

AWOL:
Well, presumably, you've got a function called "setPixelColor", that isn't a method of the object "strip".

The bit of code in question is part of a class definition, not part of an instantiated object, so you use the bare function names without an object name as prefix. At least I think that's how it works (I'm a bit new to this).

I've no idea why it's not working though.

How many pixels are we talking about here ?

Presumably if a pixel is on then you turned it on. If you save the state of each pixel using a bit in a variable then you can determine whether the pixel is on or off without reading from it.

Looking at the code, this is a subclass so that’s why the call works without the strip. thingy

Now if you go look at source code for getPixelColor() in the adafruit library you’ll see they try to do some smart stuff depending on the type of the strip and if brightness has been applied or not, so sometimes they return 3 bytes packed into 4, but sometimes 4 bytes… so you are not really guaranteed that you’ll always get back the exact 3 byte value you expect.

I’d suggest you print the color for a led you know is off and find out what the value is.