SOLVED:How does one select a line at random from an array?

I have been back and forth with this code for the better part of the morning. Basically I want to get a variable named ‘Final’ to obtain whatever the random array line is (as determined by random()).

How do I assign the full line of the Array to a variable so I can load it in the LED setting?

eg, variable should show as Final should have the value of “0,255,0” with the commas included.

Thank you

#include "FastLED.h"

//#define COLOR_ORDER GRB
#define NUM_LEDS 3
#define LED_PIN 6
#define BRIGHTNESS 20

// Define the array of leds
CRGB leds[NUM_LEDS];
CRGBPalette16 gPal;

             int Colours[5][3] =
                   {
                    {0,255,0}, //RED
                    {80,255,0}, // ORANGE
                    {100,255,0}, // LIGHT ORANGE
                    {140,255,0}, // YELLOW
                    {255,255,255}, //WHITE             
                  };   

           

void setup() { 
  
             Serial.begin(115200);
             FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);  
             FastLED.setBrightness( BRIGHTNESS );

         
 

              }  
void loop() 
{
 int i = random(5);
int r = Colours[i][0];
int g = Colours[i][1];
int b = Colours[i][2];
int Final = (r,g,b);
// Serial.print(i);
//Serial.print("\n");
//Serial.print(Colours[i][0]);
//Serial.print("\n");
 Serial.print(Final);
 Serial.print("\n");
leds[2] = CRGB(Final);
      

 delay(1000);

}

Have you considered to change your int array to a CRGB array? I do not know if this will work, but it does compile.

#include "FastLED.h"

//#define COLOR_ORDER GRB
#define NUM_LEDS 3
#define LED_PIN 6
#define BRIGHTNESS 20

// Define the array of leds
CRGB leds[NUM_LEDS];
CRGBPalette16 gPal;


CRGB colours[] =
{
  CRGB(0, 255, 0),  // RED
  CRGB(80, 255, 0), // ORANGE
};


void setup()
 {

  Serial.begin(115200);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
}

void loop()
{
  int i = random(sizeof(colours)/sizeof(colours[0]));
  leds[2] = colours[i];

  delay(1000);
}

Note:
Only 2 colours. The sizeof construct makes that the random number automatically scales; so if you add a colour, you don’t need to adjust anuthing in the rest of the code.

Thank you, Didn't know CRGB arrays exited.. so new to this.

Seems logical, however all I am getting is a return of 1.

Serial.print(Colours*);*
No LEDS are activating
Still nopt parsing the actual line from the array as yet.

The colour is not showing because you're missing a call to FastLED.show();; your original code also missed that.

Serial does not have a method to print CRGB; not surehow to work that one out.

You can basically make arrays of anything.

An int cannot be egual to an array. If you want something egualnto an array it has to be an array too. For example if I have an array a [3]={1,2_3}; and I want an array b egual to a I have to do

byte b[3]={a[1], a[2],a[3]};

[quote[The colour is not showing because you’re missing a call to FastLED.show();; your original code also missed that.

That will do it :slight_smile:

Must have deleted it in amongst a bunch of commented out code.

Colors were all wonky, because I thought order GRB was at fault, fixed now and this code is working

#include "FastLED.h"

//#define COLOR_ORDER GRB
#define NUM_LEDS 3
#define LED_PIN 6
#define BRIGHTNESS 10

// Define the array of leds
CRGB leds[NUM_LEDS];
CRGBPalette16 gPal;

            CRGB Colours[] =
                   {
                    CRGB(255,0,0), //RED
                    CRGB(255,80,0), // ORANGE
                    CRGB(255,100,0), // LIGHT ORANGE
                    CRGB(255,140,0), // YELLOW
                    CRGB(255,255,255), //WHITE             
                  };   

           

void setup() { 
  
             Serial.begin(115200);
             FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);  
             FastLED.setBrightness( BRIGHTNESS );
}  
void loop() 
{
  int i = random(sizeof(Colours)/sizeof(Colours[0]));
  //middle horn
  leds[1] = Colours[i];
  FastLED.show();
  // below for edge horns to be same
  i = random(sizeof(Colours)/sizeof(Colours[0]));
  leds[0] = Colours[i];
  leds[2] = Colours[i];
  FastLED.show();

if  (i == 4) {

delay(1);
               }
if (i != 4)
              {
 
 delay(50);

              }
 
}

Now to figure out how to see if I can get the white (if picked) to delay quicker…I assume I’ll have to do an if you picked white statement delay x else …

figured it out… any suggestions as to making the brightness drop and come back up between colors?