Hi there,
I don't really understand coding at all; I've just pulled this one off the internet and altered it to display the colors I want. Beyond that, I've done a lot of searching but probably am not even wording things accurately within this unfamiliar context to find anything, so I'm sorry if this is dumb... but its time to just ask:
as is, the pixels all change color together at a set interval, but
I'd like the color changes of the individual pixels to happen separately and at random. Any help is much appreciated. thanks!
#include <Adafruit_NeoPixel.h>
#define PIN 7 // GPIO pin the neopixels are attached
#define NUMPIXELS 11 // number of pixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int COLOR_COUNT = 5;
enum colors { RED = 0, GREEN, BLUE, YELLOW, PURPLE };
const int SLEEP_AMOUNT = 1000; //wait until changing the next random pixel
const uint8_t BRIGHTNESS = 50; //0-255 scale
void setup()
{
pixels.begin();
}
void loop()
{
// Method 1: pick a random pixel and change it
// int pixelId = random(pixels.numPixels());
// pixels.setPixelColor(pixelId, getRandomColor());
// Method 2: change every pixel to a random color
for(int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, getRandomColor());
}
pixels.setBrightness(BRIGHTNESS);
pixels.show();
delay(SLEEP_AMOUNT);
}
uint32_t getRandomColor()
{
switch(random(COLOR_COUNT))
{
case RED: { return pixels.Color(245, 195, 191); }
case GREEN: { return pixels.Color(216, 245, 191); }
case BLUE: { return pixels.Color(125, 209, 245); }
case YELLOW: { return pixels.Color(231, 245, 171); }
case PURPLE: { return pixels.Color(228, 188, 245); }
// should never happen
default:{ return pixels.Color(255, 255, 255); }
}
}
Hey sorry here's more info:
I'm on an Arduino Uno with a strip of 11 RGB pixels (just for trying out codes for now)- Ultimately it'll be 22 RGB Smart NeoPixels.
The code shows random color (from among 5 specified) on random pixels (which is great), but they all switch together to the next random color every second; I'd like them each to switch at random intervals.
That is what "Method 2" does. It switches all pixels in a loop:
// Method 2: change every pixel to a random color
for (int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, getRandomColor());
}
If you want to change one at a time, use "Method 1" (and comment out the Method 2 code):
// Method 1: pick a random pixel and change it
int pixelId = random(pixels.numPixels());
pixels.setPixelColor(pixelId, getRandomColor());
// Method 2: change every pixel to a random color
// for (int i = 0; i < pixels.numPixels(); i++)
// {
// pixels.setPixelColor(i, getRandomColor());
// }
I've added and changed a few colors and each pixel is switching between them randomly, one pixel at a time and at random intervals. Perfect.
Now I'm wondering if it's possible to keep all of that going as is but also to randomly synchronize two or more random pixels for random intervals at random times. Would this be an interrupt?
#define PIN 7 // GPIO pin the neopixels are attached
#define NUMPIXELS 11 // number of pixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int COLOR_COUNT = 8;
enum colors { RED = 0, GREEN, BLUE, YELLOW, PURPLE, BLACK, INDIGO, ORANGE, };
const int SLEEP_AMOUNT = 1000; //wait until changing the next random pixel
const uint8_t BRIGHTNESS = 50; //0-255 scale
void setup()
{
pixels.begin();
}
void loop()
{
// Method 1: pick a random pixel and change it
int pixelId = random(pixels.numPixels());
pixels.setPixelColor(pixelId, getRandomColor());
// Method 2: change every pixel to a random color
//for(int i = 0; i < pixels.numPixels(); i++)
//{
// pixels.setPixelColor(i, getRandomColor());
pixels.setBrightness(BRIGHTNESS);
pixels.show();
delay(SLEEP_AMOUNT);
}
uint32_t getRandomColor()
{
switch(random(COLOR_COUNT))
{
case RED: { return pixels.Color(216, 212, 217); }
case GREEN: { return pixels.Color(216, 245, 191); }
case BLUE: { return pixels.Color(125, 209, 245); }
case YELLOW: { return pixels.Color(231, 245, 171); }
case PURPLE: { return pixels.Color(250, 250, 250); }
case BLACK: { return pixels.Color(0, 0, 0); }
case INDIGO: { return pixels.Color(126, 175, 252); }
case ORANGE: { return pixels.Color(209, 137, 107); }
// should never happen
default:{ return pixels.Color(255, 255, 255); }
}
}
type or paste code here
isn't it just your algorithm for determining the color of each pixel? when setting the color of a pixel, randomly decide if you want it to be the same color as another pixel that has already been assigned a color and set it to that color
I'm wanting the synchronization to apply to the transition to different colors too- so that while two or more pixels are synched, they would also be changing to their (same) next random color at the same time, overriding the 'one at a time' function, but just for the pixels synched during the random timeframe they're synched- otherwise all movement would be solitary.
and sorry-
I shouldn't guess. I've read the guidelines and see now that it's discouraged. Thanks for your patience with me
It sounds like you need to keep a list of the "two or more random pixels" that are currently synchronized. Then, when you set the color of a pixel, check if it is in the list and, if so, set the other pixels in the list to the same color.
Then it is a matter of deciding when to change the list of synchronized pixels.
boolean synchronized[NUMPIXELS];
void ChangeSynchronized()
{
for (int i=0; i<NUMPIXELS; i++)
synchronized[i] = random(NUMPIXELS) < 2; // Roughly 2 out of NUMPIXELS
}
// Method 1: pick a random pixel and change it
int pixelId = random(pixels.numPixels());
uint32_t color = getRandomColor();
pixels.setPixelColor(pixelId, color);
if (synchronized[pixelId])
{
for (int i=0; i< NUMPIXELS; i++)
{
if (synchronized[i])
pixels.setPixelColor(i, color);
}
}