SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By StealthRT
#181564
Hey all I have been trying to figure out a way to random set patterns for my fade in/ out code below:
Code: Select all
int PIN = 3;
int totalLEDs = 11;
int ledFadeTime = 5;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(totalLEDs, PIN, NEO_GRB + NEO_KHZ800);

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

void loop() {
  rgbFadeInAndOut(0, 0, 255, ledFadeTime); // Blue
}

void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
  for(uint8_t b = 0; b <255; b++) {
     for(uint8_t i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
     }

     strip.show();
     delay(wait);
  };

  for(uint8_t b=255; b > 0; b--) {
     for(uint8_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
     }
     strip.show();
     delay(wait);
  };
};
The code above works great. Fades the blue in and out infinity times. However, I am wanting random LEDs to be off or have a lighter/darker shade of the color I choose (in the above it's blue).

Example:
Code: Select all
[off][blue][blue][off][off][dark blue][blue][off][dark blue][dark blue][off]
[blue][blue][blue][dark blue][off][blue][dark blue][blue][dark blue][off][off]
etc etc...
Would anyone happen to have code already like this?

Any help would be great! Thanks!
By satacoy
#181768
You'll need to create an array (or multiple arrays) to hold the current RGB values of each pixel. You'll also need to keep track of whether each pixel is getting brighter or darker. In pseudo code, it'd be something like the following. You'd have to expand it out if you wanted to do true RGB values.
Code: Select all
byte pixelBlue[numPixels];
byte blueIncrement[numPixels];

Assign random values from 0 to 255 to each pixelBlue element.
Assign either a 1 or 0 to each blueIncrement element.

Loop through each pixel.
  Add the increment value to the color value.  
  If it's greater than 255
    Set color to 255
    Set increment to -1
  else if it's smaller than 0
   Set color to 0
   Set increment to 1
  Set the pixel to the new calculated value
End loop
It gets trickier if you want to do full RGB values, because you'd have to keep track of the R, G, and B values. If you wanted the colors to remain consistent as they cycled in brightness, you'd have to do a bit more math to calculate the brightness of the color.