NeoPixel Coding Help

Hey there! So I'm pretty new to Arduino and coding, and have a cosplay project with NeoPixels I'm working with. I'm working with the code below that I found to get a twinkling effect, but I'd like to change it up to only flash in a few specific colors, rather than the fully random colors I'm getting now. Can anyone show me where I need to change or add to the code to specify the colors?

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define Pixels 16

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixels, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.

float redStates[Pixels];
float blueStates[Pixels];
float greenStates[Pixels];
float fadeRate = 0.96;

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'

for(uint16_t l = 0; l < Pixels; l++) {
redStates[l] = 0;
greenStates[l] = 0;
blueStates[l] = 0;
}
}

void loop () {
if (random(20) == 1) {
uint16_t i = random(Pixels);
if (redStates[i] < 1 && greenStates[i] < 1 && blueStates[i] < 1) {
redStates[i] = random(256);
greenStates[i] = random(256);
blueStates[i] = random(256);
}
}

for(uint16_t l = 0; l < Pixels; l++) {
  if (redStates[l] > 1 || greenStates[l] > 1 || blueStates[l] > 1) {
    strip.setPixelColor(l, redStates[l], greenStates[l], blueStates[l]);

    if (redStates[l] > 1) {
      redStates[l] = redStates[l] * fadeRate;
    } else {
      redStates[l] = 0;
    }

    if (greenStates[l] > 1) {
      greenStates[l] = greenStates[l] * fadeRate;
    } else {
      greenStates[l] = 0;
    }

    if (blueStates[l] > 1) {
      blueStates[l] = blueStates[l] * fadeRate;
    } else {
      blueStates[l] = 0;
    }

  } else {
    strip.setPixelColor(l, 0, 0, 0);
  }
}
strip.show();
delay(10);

}

You can limit the numbers here:

redStates[i]   = random(256);
greenStates[i] = random(256);
blueStates[i]  = random(256);

See:
random(min, max)

Or you can use a much smaller range of random numbers, say 8, and then get them to the full range by multiplying by 32.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.