Treasure Chest Sparkle Lights - Stuck

Working on a project to animate a treasure chest for my wife’s tiki room. The first part of animating the chest is to make the treasure within the chest sparkle using neopixels. The code I created randomly assigns each pixel a color from a controlled color pallet when turned on. I then have a loop create a sparkle effect by randomly changing a pixels color to white and then back to it’s original color. The sparkle effect occurs at random times to vary the look consistently. Everything works well as long as the strip brightness remains at or below 125. If I increase the brightness above 125, the sparkle effect changes the color of a pixel white, but then does not change the color back to it’s original color. Eventually, all the pixels are a bright white. On the hardware side, I am running my code thru an Arduino Uno powered either by USB when connected to my computer or from a 5v 10amp power supply which feeds both the neopixels and Arduino. There is a 100uF 25v capacitor between the power connection and lights/Arduino. There is a 330 ohm resistor between the data line and Arduino which connects to a 15 count neopixel strip. Below is my code. I would like to run the LEDs a bit brighter while maintaining my sparkle effect. Any help correcting my issue is appreciated.

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 15

long n;
long color;
int timing;
long red;
long green;
long blue;
long purple;
long yellow;
long cyan;
long lime;
long orange;
long magenta;
long mix;
long set;


// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
 // put your setup code here, to run once:

 Serial.begin(9600); //debug using serial monitor
  
 strip.begin();
 strip.setBrightness(125); //Overall Brightness: 100% is 255, 80% is 204, 60% is 153, 40% is 102, 20% is 51
 
 //defines the desired color pallet
 uint32_t red = strip.Color(255, 0, 0);
 uint32_t green = strip.Color(0, 255, 0);
 uint32_t blue = strip.Color(0, 0, 255);
 uint32_t purple = strip.Color(125, 0, 255);
 uint32_t yellow = strip.Color(255, 255, 0);
 uint32_t cyan = strip.Color(0, 255, 255);
 uint32_t lime = strip.Color(110, 255, 0);
 uint32_t orange = strip.Color(255, 80, 0);
 uint32_t magenta = strip.Color(255, 0, 255);

 randomSeed(analogRead(0));
 strip.show();

 for (int i = 0; i <= LED_COUNT-1; i++) {
   
   mix = random(0,8);  //Generate random number in a range ewual to the number of pallet colors
   Serial.println(mix);  //debugging check

   if (mix == 0) {  //use random number to assign a color from the color pallet
     set = red;
   }
   else if (mix == 1) {
     set = green;
   }
   else if (mix == 2) {
     set = blue;
   }
   else if (mix == 3) {
     set = purple;
   }
   else if (mix == 4) {
     set = yellow;
   }
   else if (mix == 5) {
     set = cyan;
   }
   else if (mix == 6) {
     set = lime;
   }
   else if (mix == 7) {
     set = orange;
   }
   else {
     set == magenta;
   }
   strip.setPixelColor(i, set); //set each pixel to a random color from color pallet
   Serial.println(set); //debugging serial monitor
  
 }
 
 strip.show(); // Initialize all pixels 

}

void loop() {
 // put your main code here, to run repeatedly:
 //Twinkle white
 timing = random(500, 2000); //generate random timing between twinkles
 delay (timing); //wait between twinkles
 n = random(0,LED_COUNT-1); //generate a random pixel number
 uint32_t color = strip.getPixelColor(n); //store random pixel current color data
 strip.setPixelColor(n, 255, 255, 255, 0);  //change the random pixel color data to white
 strip.show(); //display pixel color
 delay (100); //pause before returning to original pixel color
 strip.setPixelColor(n, color); //change the random pixel back to its original color
 strip.show(); //display pixel color
 
}

Get with the program!

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. While you were lucky so far, if you do not post it as “code” it can easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.


You could tidy up your “else if (mix ==” with a “case” statement or even better, an array.

Don’t know the library, so am puzzled by the fifth argument in "strip.setPixelColor(n, 255, 255, 255, 0); "

Yes, will look at cleaning this code up and simplifying. Will need to learn about arrays. I structured the code with what I know today. The fifth argument is for the white pixel as I am using GRBW LEDs, which can be set 0-255. Modified my post per your suggestion. Missed the copy to forum, will be sure to use that in the future.

Well I am wondering whether is is significant that you are using "strip.setPixelColor" with five arguments in some places and four in others?

Ensured all setPixelColor commands are consistently 5 arguments. No change to the issue. I can interestingly set the brightness above 125 and the base lights brighten normally. It is only when the sparkle loop occurs, that the lights turn white according to the sparkle command, but then are stuck on white. Going to try to serial print the color variable to see if I am properly receiving the color when called. If it does, that would isolate the issue to the code returning the led to the proper color. Could insufficient delay be creating an issue? Will try upping the delay time as well and see what happens.

Ok, I am seeing strange output from the color set to the color call. Below is a portion of the serial monitor output at brightness levels of 125 and 155 respectively. At a brightness of 125, the LEDs return to their original color visually. At 155, the LEDs get stuck in a bright white.

Interestingly, the set color and the color call values do not match. After every sparkle event, the color shifts. I would imagine this would change the color each subsequent sparkle event eventually resulting in a very different color. I will need to fix this somehow.

The higher brightness really messes up the set color from the reported color value. At brightness of 125, the set color and reported color are not the same, but closer to the set value. At brightness of 155, the set color and reported color are extremely different. At 155 brightness, there is consistency between LEDs set to the same color.

Easiest to follow LED 13 at both brightness levels. At 125 brightness, the color read is closer together after 3 sparkle events. At 155 brightness, the color is completely different after the first sparkle event. Both show color values lowering after each sparkle event.

Would love to hear suggestions on how to resolve this.

Serial monitor output at 125 LED brightness:

Set LED 0 to color: 16776960
LED number 0 read color: 16645376
Set LED 1 to color: 16776960
LED number 1 read color: 16645376
Set LED 2 to color: 16732160
LED number 2 read color: 16600832
Set LED 3 to color: 16776960
LED number 3 read color: 16645376
Set LED 4 to color: 16776960
LED number 4 read color: 16645376
Set LED 5 to color: 65535
LED number 5 read color: 65021
Set LED 6 to color: 16776960
LED number 6 read color: 16645376
Set LED 7 to color: 255
LED number 7 read color: 253
Set LED 8 to color: 7274240
LED number 8 read color: 7208192
Set LED 9 to color: 16776960
LED number 9 read color: 16645376
Set LED 10 to color: 16776960
LED number 10 read color: 16645376
Set LED 11 to color: 255
LED number 11 read color: 253
Set LED 12 to color: 8192255
LED number 12 read color: 8061181
Set LED 13 to color: 7274240
LED number 13 read color: 7208192
Set LED 14 to color: 255
LED number 14 read color: 253
LED number 9 is currently set to this color: 16645376
LED number 8 is currently set to this color: 7208192
LED number 8 is currently set to this color: 7076608
LED number 8 is currently set to this color: 6945024
LED number 10 is currently set to this color: 16645376
LED number 9 is currently set to this color: 16513792
LED number 4 is currently set to this color: 16645376
LED number 7 is currently set to this color: 253
LED number 0 is currently set to this color: 16645376
LED number 1 is currently set to this color: 16645376
LED number 10 is currently set to this color: 16513792
LED number 10 is currently set to this color: 16382208
LED number 13 is currently set to this color: 7208192
LED number 13 is currently set to this color: 7076608
LED number 4 is currently set to this color: 16513792
LED number 0 is currently set to this color: 16513792
LED number 2 is currently set to this color: 16600832
LED number 10 is currently set to this color: 16250624
LED number 11 is currently set to this color: 253
LED number 1 is currently set to this color: 16513792
LED number 4 is currently set to this color: 16382208
LED number 9 is currently set to this color: 16382208
LED number 3 is currently set to this color: 16645376
LED number 2 is currently set to this color: 16469248
LED number 6 is currently set to this color: 16645376
LED number 13 is currently set to this color: 6945024
LED number 3 is currently set to this color: 16513792
LED number 10 is currently set to this color: 16119040
LED number 6 is currently set to this color: 16513792

Serial monitor output at 155 LED brightness:

Set LED 0 to color: 255
LED number 0 read color: 27531675
Set LED 1 to color: 7274240
LED number 1 read color: 2759695104
Set LED 2 to color: 255
LED number 2 read color: 27531675
Set LED 3 to color: 65280
LED number 3 read color: 2753141504
Set LED 4 to color: 65280
LED number 4 read color: 2753141504
Set LED 5 to color: 7274240
LED number 5 read color: 2759695104
Set LED 6 to color: 16776960
LED number 6 read color: 3181091584
Set LED 7 to color: 16732160
LED number 7 read color: 429608448
Set LED 8 to color: 7274240
LED number 8 read color: 2759695104
Set LED 9 to color: 16732160
LED number 9 read color: 429608448
Set LED 10 to color: 16776960
LED number 10 read color: 3181091584
Set LED 11 to color: 65535
LED number 11 read color: 2780666779
Set LED 12 to color: 65535
LED number 12 read color: 2780666779
Set LED 13 to color: 255
LED number 13 read color: 27531675
Set LED 14 to color: 16711680
LED number 14 read color: 429588480
LED number 12 is currently set to this color: 2780666779
LED number 2 is currently set to this color: 27531675
LED number 1 is currently set to this color: 2759695104
LED number 9 is currently set to this color: 429608448
LED number 11 is currently set to this color: 2780666779
LED number 7 is currently set to this color: 429608448
LED number 4 is currently set to this color: 2753141504
LED number 13 is currently set to this color: 27531675
LED number 0 is currently set to this color: 27531675
LED number 2 is currently set to this color: 10623130
LED number 8 is currently set to this color: 2759695104
LED number 8 is currently set to this color: 2726074880
LED number 10 is currently set to this color: 3181091584
LED number 12 is currently set to this color: 2763823770
LED number 6 is currently set to this color: 3181091584
LED number 7 is currently set to this color: 412765440
LED number 13 is currently set to this color: 10623130
LED number 13 is currently set to this color: 10491544
LED number 10 is currently set to this color: 3164248576
LED number 0 is currently set to this color: 10623130

I let the lights run for about 15 minutes at a brightness level of 105. The color values shifted a lot initially and then stabilized over time. It is still running and currently the color values are identical after each subsequent sparkle event for a given LED. The visual appearance of each LED looks identical to it's starting color.

For example, the set color value is stated as 255. Initially, the read color value is 253. Once stabilized, the read color value is 128. The stable value appears to be half the set value.

 Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);

So are you using GRBW led strips then?
I ask because lines like

 uint32_t green = strip.Color(0, 255, 0);

Is only getting three parameters, it needs to have four for GRBW LEDs. So you are leaving the white component out completely. Maybe this is messing up the colour returned.

It would help if you printer the set and returned colour in hex so we could see what is going on with the bit pattern. A long decimal number is just useless to see the bit pattern.

 strip.setBrightness(125);

Set this to 255 and see if the shifting goes away.

Corrected the code to pass 4 attributes to each LED when setting the color pallet.

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4
 
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 15

long n;
long color;
int timing;
long red;
long green;
long blue;
long purple;
long yellow;
long cyan;
long lime;
long orange;
long magenta;
long mix;
long set;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //debug using serial monitor
   
  strip.begin();
  strip.setBrightness(155); //Overall Brightness: 100% is 255, 80% is 204, 60% is 153, 40% is 102, 20% is 51
  
  //defines the desired color pallet
  uint32_t red=strip.Color(255, 0, 0,0);
  uint32_t green=strip.Color(0, 255, 0,0);
  uint32_t blue=strip.Color(0, 0, 255, 0);
  uint32_t purple=strip.Color(125, 0, 255, 0);
  uint32_t yellow=strip.Color(255, 255, 0, 0);
  uint32_t cyan=strip.Color(0, 255, 255, 0);
  uint32_t lime=strip.Color(110, 255, 0, 0);
  uint32_t orange=strip.Color(255, 80, 0, 0);
  uint32_t magenta=strip.Color(255, 0, 255, 0);

  randomSeed(analogRead(0)); //necessary to get true randomization
  strip.show();

  for (int i=0; i <= LED_COUNT-1; i++) {
    
    mix = random(0,8);  //Generate random number in a range ewual to the number of pallet colors
    
 
    if (mix==0) {  //use random number to assign a color from the color pallet
      set=red;
    }
    else if (mix==1) {
      set=green;
    }
    else if (mix==2) {
      set=blue;
    }
    else if (mix==3) {
      set=purple;
    }
    else if (mix==4) {
      set=yellow;
    }
    else if (mix==5) {
      set=cyan;
    }
    else if (mix==6) {
      set=lime;
    }
    else if (mix==7) {
      set=orange;
    }
    else {
      set==magenta;
    }
    strip.setPixelColor(i, set); //set each pixel to a random color from color pallet
    
    uint32_t color=strip.getPixelColor(i); //store random pixel current color data
    
    Serial.print("Set LED ");
    Serial.print(i);  //debugging check
    Serial.print(" to color: ");
    Serial.println(set); //debugging serial monitor
    Serial.print("LED number ");
    Serial.print(i);
    Serial.print(" read color: ");
    Serial.println(color);
  }
  
  strip.show(); // Initialize all pixels 
}

void loop() {
  // put your main code here, to run repeatedly:
  //Twinkle white
  timing = random(500, 2000); //generate random timing between twinkles
  delay (timing); //wait between twinkles
  n = random(0,LED_COUNT-1); //generate a random pixel number
  uint32_t color=strip.getPixelColor(n); //store random pixel current color data
  
  Serial.print("LED number ");
  Serial.print(n);
  Serial.print(" is currently set to this color: ");
  Serial.println(color); //debugging serial monitor
  strip.setPixelColor(n, 255, 255, 255, 0);  //change the random pixel color data to white
  strip.show(); //display pixel color
  delay (100); //pause before returning to original pixel color
  strip.setPixelColor(n, color); //change the random pixel back to its original color
  strip.show(); //display pixel color
}

Converted to hex output.

Brightness at 125:

Set LED 0 to color: FFFF
LED number 0 read color: FDFD
Set LED 1 to color: FF00
LED number 1 read color: FD00
Set LED 2 to color: FF00
LED number 2 read color: FD00
Set LED 3 to color: FF
LED number 3 read color: FD
Set LED 4 to color: FF0000
LED number 4 read color: FD0000
Set LED 5 to color: FF00
LED number 5 read color: FD00
Set LED 6 to color: 7D00FF
LED number 6 read color: 7B00FD
Set LED 7 to color: FF00
LED number 7 read color: FD00
Set LED 8 to color: FF0000
LED number 8 read color: FD0000
Set LED 9 to color: FF5000
LED number 9 read color: FD4F00
Set LED 10 to color: FFFF
LED number 10 read color: FDFD
Set LED 11 to color: FFFF00
LED number 11 read color: FDFD00
Set LED 12 to color: FF00
LED number 12 read color: FD00
Set LED 13 to color: FFFF00
LED number 13 read color: FDFD00
Set LED 14 to color: 6EFF00
LED number 14 read color: 6DFD00
LED number 8 is currently set to this color: FD0000
LED number 3 is currently set to this color: FD
LED number 10 is currently set to this color: FDFD
LED number 1 is currently set to this color: FD00
LED number 1 is currently set to this color: FB00
LED number 0 is currently set to this color: FDFD
LED number 12 is currently set to this color: FD00
LED number 3 is currently set to this color: FB
LED number 5 is currently set to this color: FD00
LED number 4 is currently set to this color: FD0000
LED number 13 is currently set to this color: FDFD00
LED number 6 is currently set to this color: 7B00FD
LED number 6 is currently set to this color: 7900FB
LED number 4 is currently set to this color: FB0000
LED number 2 is currently set to this color: FD00
LED number 13 is currently set to this color: FBFB00
LED number 2 is currently set to this color: FB00
LED number 10 is currently set to this color: FBFB
LED number 1 is currently set to this color: F900
LED number 7 is currently set to this color: FD00
LED number 7 is currently set to this color: FB00
LED number 7 is currently set to this color: F900
LED number 11 is currently set to this color: FDFD00
LED number 9 is currently set to this color: FD4F00
LED number 6 is currently set to this color: 7700F9
LED number 10 is currently set to this color: F9F9
LED number 13 is currently set to this color: F9F900
LED number 5 is currently set to this color: FB00
LED number 10 is currently set to this color: F7F7
LED number 0 is currently set to this color: FBFB
LED number 13 is currently set to this color: F7F700
LED number 5 is currently set to this color: F900
LED number 5 is currently set to this color: F700
LED number 1 is currently set to this color: F700
LED number 13 is currently set to this color: F5F500
LED number 5 is currently set to this color: F500
LED number 11 is currently set to this color: FBFB00
LED number 9 is currently set to this color: FB4D00
LED number 9 is currently set to this color: F94B00
LED number 0 is currently set to this color: F9F9
LED number 1 is currently set to this color: F500
LED number 8 is currently set to this color: FB0000
LED number 1 is currently set to this color: F300
LED number 7 is currently set to this color: F700
LED number 2 is currently set to this color: F900

Brightness at 155 in another post.

Set the brightness to 255, and the issue did go away! Very interesting suggestion. Why is peak brightness a way to resolve the issue? It is extremely bright for my project, but could live with it if I must. Understanding the why here would certainly help.

Brightness at 155, HEX data:

Set LED 0 to color: FFFF
LED number 0 read color: A5BD9B9B
Set LED 1 to color: 7D00FF
LED number 1 read color: 1FC199B
Set LED 2 to color: FFFF
LED number 2 read color: A5BD9B9B
Set LED 3 to color: FF00
LED number 3 read color: A4199B00
Set LED 4 to color: FF00
LED number 4 read color: A4199B00
Set LED 5 to color: FFFF
LED number 5 read color: A5BD9B9B
Set LED 6 to color: FFFF00
LED number 6 read color: BD9B9B00
Set LED 7 to color: 7D00FF
LED number 7 read color: 1FC199B
Set LED 8 to color: 7D00FF
LED number 8 read color: 1FC199B
Set LED 9 to color: 6EFF00
LED number 9 read color: A47D9B00
Set LED 10 to color: 7D00FF
LED number 10 read color: 1FC199B
Set LED 11 to color: FFFF00
LED number 11 read color: BD9B9B00
Set LED 12 to color: 6EFF00
LED number 12 read color: A47D9B00
Set LED 13 to color: FFFF00
LED number 13 read color: BD9B9B00
Set LED 14 to color: FF5000
LED number 14 read color: 199B4E00
LED number 1 is currently set to this color: 1FC199B
LED number 5 is currently set to this color: A5BD9B9B
LED number 11 is currently set to this color: BD9B9B00
LED number 8 is currently set to this color: 1FC199B
LED number 3 is currently set to this color: A4199B00
LED number 10 is currently set to this color: 1FC199B
LED number 5 is currently set to this color: A4BC9A9A
LED number 11 is currently set to this color: BC9A9A00
LED number 0 is currently set to this color: A5BD9B9B
LED number 8 is currently set to this color: 1998189A
LED number 3 is currently set to this color: A2189A00
LED number 4 is currently set to this color: A4199B00
LED number 0 is currently set to this color: A4BC9A9A
LED number 7 is currently set to this color: 1FC199B
LED number 9 is currently set to this color: A47D9B00
LED number 11 is currently set to this color: BB989800
LED number 10 is currently set to this color: 1998189A
LED number 2 is currently set to this color: A5BD9B9B
LED number 11 is currently set to this color: B9969600
LED number 1 is currently set to this color: 1998189A
LED number 3 is currently set to this color: A0169800
LED number 4 is currently set to this color: A2189A00
LED number 6 is currently set to this color: BD9B9B00
LED number 11 is currently set to this color: B7959500
LED number 6 is currently set to this color: BC9A9A00
LED number 6 is currently set to this color: BB989800
LED number 2 is currently set to this color: A4BC9A9A
LED number 11 is currently set to this color: B6939300
LED number 1 is currently set to this color: 18961698
LED number 5 is currently set to this color: A2BB9898
LED number 12 is currently set to this color: A47D9B00
LED number 4 is currently set to this color: A0169800
LED number 4 is currently set to this color: 9F159600
LED number 1 is currently set to this color: 16951596
LED number 1 is currently set to this color: 15931395
LED number 1 is currently set to this color: 13921293
LED number 1 is currently set to this color: 12901092
LED number 13 is currently set to this color: BD9B9B00
LED number 12 is currently set to this color: A27C9A00
LED number 0 is currently set to this color: A2BB9898
LED number 7 is currently set to this color: 1998189A
LED number 12 is currently set to this color: A07B9800

Grumpy_Mike, converted to HEX but not sure what you meant by "printer the set"? Is this a format of some type?

Ugly solution to the issue. Storing the set value of the LED once assigned and then recalling the set value to restore the color resolves the LED color read issue. The issue experienced is with the color read becoming inaccurate at higher brightness levels, causing the LEDs to turn white with the independent white channel stuck on. The color read approach earlier seemed to work ok at lower than 125 brightness and at peak brightness of 255, but would fail between those values.

This solution is ugly in that adding additional LEDs would require additional variables to store the preset color data, hence this is not good for large strings of LEDs.

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4
 
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 15

long n;
long color;
int timing;
long red;
long green;
long blue;
long purple;
long yellow;
long cyan;
long lime;
long orange;
long magenta;
long mix;
long set;
long led0;
long led1;
long led2;
long led3;
long led4;
long led5;
long led6;
long led7;
long led8;
long led9;
long led10;
long led11;
long led12;
long led13;
long led14;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //debug using serial monitor
   
  strip.begin();
  strip.setBrightness(185); //Overall Brightness: 100% is 255, 80% is 204, 60% is 153, 40% is 102, 20% is 51
  
  //defines the desired color pallet
  uint32_t red=strip.Color(255, 0, 0,0);
  uint32_t green=strip.Color(0, 255, 0,0);
  uint32_t blue=strip.Color(0, 0, 255, 0);
  uint32_t purple=strip.Color(125, 0, 255, 0);
  uint32_t yellow=strip.Color(255, 255, 0, 0);
  uint32_t cyan=strip.Color(0, 255, 255, 0);
  uint32_t lime=strip.Color(110, 255, 0, 0);
  uint32_t orange=strip.Color(255, 80, 0, 0);
  uint32_t magenta=strip.Color(255, 0, 255, 0);

  randomSeed(analogRead(0)); //necessary to get true randomization
  strip.show();

  for (int i=0; i <= LED_COUNT-1; i++) {
    
    mix = random(0,8);  //Generate random number in a range ewual to the number of pallet colors
    
    if (mix==0) {  //use random number to assign a color from the color pallet
      set=red;
    }
    else if (mix==1) {
      set=green;
    }
    else if (mix==2) {
      set=blue;
    }
    else if (mix==3) {
      set=purple;
    }
    else if (mix==4) {
      set=yellow;
    }
    else if (mix==5) {
      set=cyan;
    }
    else if (mix==6) {
      set=lime;
    }
    else if (mix==7) {
      set=orange;
    }
    else {
      set==magenta;
    }
    strip.setPixelColor(i, set); //set each pixel to a random color from color pallet

    if (i==0) {
      led0=set;
    }
    else if (i==1) {
      led1=set;
    }
    else if (i==2) {
      led2=set;
    }
    else if (i==3) {
      led3=set;
    }
    else if (i==4) {
      led4=set;
    }
    else if (i==5) {
      led5=set;
    }
    else if (i==6) {
      led6=set;
    }
    else if (i==7) {
      led7=set;
    }
    else if (i==8) {
      led8=set;
    }
    else if (i==9) {
      led9=set;
    }
    else if (i==10) {
      led10=set;
    }
    else if (i==11) {
      led11=set;
    }
    else if (i==12) {
      led12=set;
    }
    else if (i==13) {
      led13=set;
    }
    else {
      led14=set;
    }
      
    Serial.print("Set LED ");
    Serial.print(i);  //debugging check
    Serial.print(" to color: ");
    Serial.println(set, HEX); //debugging serial monitor
  }
  
  strip.show(); // Initialize all pixels 
}

void loop () {
  //enter repeated code here
  //Twinkle white
  timing = random(500, 2000); //generate random timing between twinkles
  delay (timing); //wait between twinkles
  n = random(0,LED_COUNT-1); //generate a random pixel number
 
  strip.setPixelColor(n, 255, 255, 255, 0);  //change the random pixel color data to white
  strip.show(); //display pixel color

  delay (100); //pause before returning to original pixel color
  
  if (n==0) {
    strip.setPixelColor(n, led0); //change the random pixel back to its original color
  }
  else if (n==1) {
    strip.setPixelColor(n, led1); //change the random pixel back to its original color
  }
  else if (n==2) {
    strip.setPixelColor(n, led2); //change the random pixel back to its original color
  }
  else if (n==3) {
    strip.setPixelColor(n, led3); //change the random pixel back to its original color
  }
  else if (n==4) {
    strip.setPixelColor(n, led4); //change the random pixel back to its original color
  }
  else if (n==5) {
    strip.setPixelColor(n, led5); //change the random pixel back to its original color
  }
  else if (n==6) {
    strip.setPixelColor(n, led6); //change the random pixel back to its original color
  }
  else if (n==7) {
    strip.setPixelColor(n, led7); //change the random pixel back to its original color
  }
  else if (n==8) {
    strip.setPixelColor(n, led8); //change the random pixel back to its original color
  }
  else if (n==9) {
    strip.setPixelColor(n, led9); //change the random pixel back to its original color
  }
  else if (n==10) {
    strip.setPixelColor(n, led10); //change the random pixel back to its original color
  }
  else if (n==11) {
    strip.setPixelColor(n, led11); //change the random pixel back to its original color
  }
  else if (n==12) {
    strip.setPixelColor(n, led12); //change the random pixel back to its original color
  }
  else if (n==13) {
    strip.setPixelColor(n, led13); //change the random pixel back to its original color
  }
  else {
    strip.setPixelColor(n, led14); //change the random pixel back to its original color
  }
  
  uint32_t color=strip.getPixelColor(n); //store random pixel current color data
  Serial.print("LED number ");
  Serial.print(n);
  Serial.print(" is reset to this color: ");
  Serial.println(color, HEX); //debugging serial monitor  
  strip.show(); //display pixel color
}

By print the set, I meant print the values you set the pixels to, which you have done.

The reason why it only works with a full brightness is that the brightness scales the colour number during the setting process, then it fills the buffer and you send out the scaled value so you never get back what you put in.
Adafruit have a version of this library for the Raspberry Pi, in python that maintains a separate buffer for the set and scaled values so this does not happen. But there you have the space for a double buffer.

The real solution is to set the brightness to full but apply the brightness scaling to the colours you use. That is instead of full red 255, 0, 0 you would use say 64, 0, 0 so it is not as bright.

Thanks for the clarification and help Grumpy_Mike.

New code below is working perfectly with only a little more effort to adjust each colors brightness in the color pallet. This is a better solution than my previous fix to enable expansion of the number of LEDs being used.

Fun first attempt using neopixels. Happy with the result of this light effect, making my gems sparkle!

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4
 
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 15

long n;
long color;
int timing;
long red;
long green;
long blue;
long purple;
long yellow;
long cyan;
long lime;
long orange;
long magenta;
long mix;
long set;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //debug using serial monitor
   
  strip.begin();
  strip.setBrightness(255); //Overall Brightness must be kept at 255 for reading the color of LED
  
  //set brightness by adjusting the color pallet
  //Brightness of the colors can be set using the following scale: 1005 is 255, 80% is 204, 60% is 153, 40% is 102, 20% is 51
  //defines the desired color pallet
  uint32_t red=strip.Color(153, 0, 0,0);
  uint32_t green=strip.Color(0, 153, 0,0);
  uint32_t blue=strip.Color(0, 0, 153, 0);
  uint32_t purple=strip.Color(75, 0, 153, 0);
  uint32_t yellow=strip.Color(153, 153, 0, 0);
  uint32_t cyan=strip.Color(0, 153, 153, 0);
  uint32_t lime=strip.Color(66, 153, 0, 0);
  uint32_t orange=strip.Color(153, 48, 0, 0);
  uint32_t magenta=strip.Color(153, 0, 153, 0);

  randomSeed(analogRead(0)); //necessary to get true randomization
  strip.show();

  for (int i=0; i <= LED_COUNT-1; i++) {
    
    mix = random(0,8);  //Generate random number in a range ewual to the number of pallet colors
    
 
    if (mix==0) {  //use random number to assign a color from the color pallet
      set=red;
    }
    else if (mix==1) {
      set=green;
    }
    else if (mix==2) {
      set=blue;
    }
    else if (mix==3) {
      set=purple;
    }
    else if (mix==4) {
      set=yellow;
    }
    else if (mix==5) {
      set=cyan;
    }
    else if (mix==6) {
      set=lime;
    }
    else if (mix==7) {
      set=orange;
    }
    else {
      set==magenta;
    }
    strip.setPixelColor(i, set); //set each pixel to a random color from color pallet
    
    uint32_t color=strip.getPixelColor(i); //store random pixel current color data
    
    Serial.print("Set LED ");
    Serial.print(i);  //debugging check
    Serial.print(" to color: ");
    Serial.println(set, HEX); //debugging serial monitor
    Serial.print("LED number ");
    Serial.print(i);
    Serial.print(" read color: ");
    Serial.println(color, HEX);
  }
  
  strip.show(); // Initialize all pixels 
}

void loop() {
  // put your main code here, to run repeatedly:
  //Twinkle white
  timing = random(500, 2000); //generate random timing between twinkles
  delay (timing); //wait between twinkles
  n = random(0,LED_COUNT-1); //generate a random pixel number
  uint32_t color=strip.getPixelColor(n); //store random pixel current color data
  
  Serial.print("LED number ");
  Serial.print(n);
  Serial.print(" is currently set to this color: ");
  Serial.println(color, HEX); //debugging serial monitor
  strip.setPixelColor(n, 255, 255, 255, 0);  //change the random pixel color data to white, adjust brightness to desired
  strip.show(); //display pixel color
  delay (100); //pause before returning to original pixel color
  strip.setPixelColor(n, color); //change the random pixel back to its original color
  strip.show(); //display pixel color
}

Thanks for getting back and posting your full solution.

Are you aware that this deceleration in the setup function

uint32_t red=strip.Color(153, 0, 0,0);
  uint32_t green=strip.Color(0, 153, 0,0);
  uint32_t blue=strip.Color(0, 0, 153, 0);
  uint32_t purple=strip.Color(75, 0, 153, 0);
  uint32_t yellow=strip.Color(153, 153, 0, 0);
  uint32_t cyan=strip.Color(0, 153, 153, 0);
  uint32_t lime=strip.Color(66, 153, 0, 0);
  uint32_t orange=strip.Color(153, 48, 0, 0);
  uint32_t magenta=strip.Color(153, 0, 153, 0);

Can only be used inside the setup function and that the other variables you declared as global:-

long n;
long color;
int timing;
long red;
long green;
long blue;
long purple;
long yellow;
long cyan;
long lime;
long orange;
long magenta;
long mix;
long set;

Are always defined as zero.
Fortunately you don't use them so your code could do with a bit of trimming to remove the things that are doing nothing for you. I would recommend you do this to get a bit more insight into coding.

Anyway well done in getting it going.

It is a good idea to post diagnostic output using the "code" listing as well. :grinning:

Thanks for the further input Grumpy_Mike and Paul__B. I have updated the code to remove unnecessary variable declarations and to replace the if else statements with a simple array.

Grumpy_Mike, I appreciate pointing out the variable declarations. I had moved the uint32_t variables before the void setup, and kept receiving an error. I did not understand that global variables had to be declared 0.

I will not be using the color pallet variables in the Loop, so I think it is ok not to set the global variables for each color. I assume that if I changed my mind on that point, I would simply code it as “uint32_t red;” for example. And then assign the color value as “red=strip.Color(153, 0, 0, 0);”, which could now be done inside setup or loop. Just checking if that is correct. Should I be declaring global variables always or just when I think I need them (is global variable declaration the best practice regardless of how I use them)?

Any further suggestions on simplifying and cleaning my code up is appreciated.

#include <Adafruit_NeoPixel.h>

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4
 
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 15

long n;
long timing;
long mix;
uint32_t set;
int i;
long twinkle;

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //debug using serial monitor
   
  strip.begin();
  strip.setBrightness(255); //Overall Brightness must be kept at 255 for reading the color of LED
  
  //set brightness by adjusting the color pallet
  //Brightness of the colors can be set using the following scale: 1005 is 255, 80% is 204, 60% is 153, 40% is 102, 20% is 51
  //defines the desired color pallet
  uint32_t red=strip.Color(153, 0, 0, 0);
  uint32_t green=strip.Color(0, 153, 0, 0);
  uint32_t blue=strip.Color(0, 0, 153, 0);
  uint32_t purple=strip.Color(75, 0, 153, 0);
  uint32_t yellow=strip.Color(153, 153, 0, 0);
  uint32_t cyan=strip.Color(0, 153, 153, 0);
  uint32_t lime=strip.Color(66, 153, 0, 0);
  uint32_t orange=strip.Color(153, 48, 0, 0);
  uint32_t magenta=strip.Color(153, 0, 153, 0);
  uint32_t myArray[9]={red, green, blue, purple, yellow, cyan, lime, orange, magenta};

  randomSeed(analogRead(0)); //necessary to get true randomization
  strip.show();

  for (i=0; i <= LED_COUNT-1; i++) {
    
    mix = random(0,8);  //Generate random number in a range ewual to the number of pallet colors
    set = myArray[mix];
    
    strip.setPixelColor(i, set); //set each pixel to a random color from color pallet
    
    uint32_t color=strip.getPixelColor(i); //store random pixel current color data
    
    Serial.print("Set LED ");
    Serial.print(i);  //debugging check
    Serial.print(" to color: ");
    Serial.println(set, HEX); //debugging serial monitor
    Serial.print("LED number ");
    Serial.print(i);
    Serial.print(" read color: ");
    Serial.println(color, HEX);
  }
  
  strip.show(); // Initialize all pixels 
}

void loop() {
  // put your main code here, to run repeatedly:
  //Twinkle white
  timing=random(500, 2000); //generate random timing between twinkles
  delay (timing); //wait between twinkles
  n=random(0,LED_COUNT-1); //generate a random pixel number
  uint32_t color=strip.getPixelColor(n); //store random pixel current color data
  
  Serial.print("LED number ");
  Serial.print(n);
  Serial.print(" is currently set to this color: ");
  Serial.println(color, HEX); //debugging serial monitor
  strip.setPixelColor(n, 255, 255, 255, 0);  //change the random pixel color data to white, adjust brightness to desired
  strip.show(); //display pixel color
  twinkle=random(40,120);
  delay (twinkle); //pause before returning to original pixel color
  strip.setPixelColor(n, color); //change the random pixel back to its original color
  strip.show(); //display pixel color
}

I did not understand that global variables had to be declared 0.

They don’t, they are often declared for any value. Zero is just the default option.
Can you post the example where this produced an error as you must be doing something else that is wrong to cause the error.

will not be using the color pallet variables in the Loop, so I think it is ok not to set the global variables for each color.

Correct.

Making a variable global uses memory permanently, where as declaring it inside a function, once the function completes the memory they use is reclaimed.