3

Hey all I have been trying to figure out a way to random set patterns for my fade in/ out code below:

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:

[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!

Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Apr 27, 2015 at 4:11

1 Answer 1

2

To make randomly-changing colours for the whole strip, change:

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

to:

void loop() {
 rgbFadeInAndOut(random (256), 
 random (256), 
 random (256), ledFadeTime); // random colour
}

If you want to make individual pixels different colours you could set up an array of colours, randomly initialize it, and use that. Like this:

#include <Adafruit_NeoPixel.h>
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(ledFadeTime);
}
void rgbFadeInAndOut(uint8_t wait) {
 uint8_t red [totalLEDs];
 uint8_t green [totalLEDs];
 uint8_t blue [totalLEDs];
 // randomly set each colour
 for (int i = 0; i < totalLEDs; i++)
 {
 red [i] = random(256);
 green [i] = random(256);
 blue [i] = random(256);
 }
 for(uint8_t b = 0; b <255; b++) {
 for(uint8_t i=0; i < strip.numPixels(); i++) {
 strip.setPixelColor(i, red [i] * b/255, green [i] * b/255, blue [i] * 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 [i] * b/255, green [i] * b/255, blue [i] * b/255);
 }
 strip.show();
 delay(wait);
 }
}
answered Jan 30, 2016 at 3:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.