-1

Is it possible to assign a random color to a string of leds? I have a string of 10 WS2812 leds, and they can be any color I stated in the array With the FastLed lib a color can be a name.

colorLed = {Red, Blue, Green, Purple}; 

I would like to random pick a color for each led separate, not the whole string just on one color. It's not a problem is two or more leds have the same color.

I tried something like this: trying to assing a color to a number, and giving the led a random number. The code doesn't validate, it's not possible to combine an int with a random number (at least not the way I tried)

#include "FastLED.h" #define NUM_LEDS 10 CRGB leds[NUM_LEDS]; #define DATA_PIN 2 long randNumber; long randNumber0 = CRGB::Red; long randNumber1 = CRGB::White; long randNumber2 = CRGB::Blue; long randNumber3 = CRGB::Red; long randNumber4 = CRGB::White; long randNumber5 = CRGB::Blue; long randNumber6 = CRGB::Red; long randNumber7 = CRGB::White; long randNumber8 = CRGB::Blue; long randNumber9 = CRGB::Red; #define INTERVAL_MESSAGE1 5000 unsigned long time_1 = 0; void setup() { FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); Serial.begin(9600); } void loop() { if (millis() >= time_1 + INTERVAL_MESSAGE1) { time_1 += INTERVAL_MESSAGE1; leds[0] = randNumber.random(0,9); leds[1] = randNumber.random(0,9); leds[2] = randNumber.random(0,9); leds[3] = randNumber.random(0,9); leds[4] = randNumber.random(0,9); leds[5] = randNumber.random(0,9); leds[6] = randNumber.random(0,9); leds[7] = randNumber.random(0,9); leds[8] = randNumber.random(0,9); leds[9] = randNumber.random(0,9); FastLED.show(55); } } 
0

1 Answer 1

0

Assuming that you have 4 colors in your colorLed array, you could enumerate them and do something like:

# define MAX_COLORS 4 // Assuming you have 4 colors only enum ColorList{ Red = 1, Blue, Green, Purple }; 

Then used them as a data type in a struct (simply because I love compartmentalising data into classes and data structures).

I've put together the following code. Please note that I've tested it on CodeLite for C++ and haven't tried it on an Arduino. However, I am sure that it will work just fine if you adapt it to your needs.

/* ColoPicker.cpp*/ #include <iostream> #include <random> #include <stdio.h> #include <stdlib.h> #include <string> #include <time.h> using namespace std; #define MAX_COLORS 4 char textOut[100]; int cycles; string colorNames[4] = { "RED", "BLUE", "GREEN", "PURPLE" }; typedef enum { RED, BLUE, GREEN, PURPLE } ColorList; struct ColorsGroup { ColorList colorCode; string name; }; ColorsGroup colorLED[4]; // Methods int random(int, int); ColorList retrieveColor(int); void fillColors(void); void printColors(int); void setup() { fillColors(); cycles = 0; } int main() { cout << "********** Color picker *********" << endl; setup(); while(cycles < 10) { fillColors(); printColors(cycles); cycles++; } return 0; } // From: https://stackoverflow.com/questions/7560114/random-number-c-in-some-range int random(int min, int max) { static bool first = true; if(first) { srand(time(NULL)); first = false; } return min + rand() % ((max + 1) - min); } void fillColors(void) { for(int idx = 0; idx < MAX_COLORS; idx++) { ColorList newColor = retrieveColor(random(0, MAX_COLORS - 1)); colorLED[idx].colorCode = newColor; colorLED[idx].name = colorNames[newColor]; } } void printColors(int i) { sprintf(textOut, "%d. colorLED >> ", i); cout << textOut ; for(int idx = 0; idx < MAX_COLORS; idx++) { const char* nameStr = colorLED[idx].name.c_str(); // or &colorLED[idx].name[0]; sprintf(textOut, "%s[%d]", nameStr, colorLED[idx].colorCode); cout << textOut; if(idx <= MAX_COLORS-2){ sprintf(textOut, ", "); cout << textOut; }else{ cout << ";" << endl; } } } ColorList retrieveColor(int col) { switch(col) { case 0: return RED; break; case 1: return BLUE; break; case 2: return GREEN; break; case 3: return PURPLE; break; default: return RED; // for the sake of completeness break; } } 

And the output looks like this:

Console Output for ColorPicker

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.