Random Color

Hello everyone, I am trying to create a function that shows a random color among three colors, however it is not working well, I do not understand specifically what the error is, but I think the Color () function is not working well, the parameter g, that is, the green color does not work. I tried it two ways but both give the same error. Does anyone know where the error comes from? or if there is some other way to do a function that does what I want to achieve?

this is the code

#include <Adafruit_NeoPixel.h>

unsigned long tiempoCambio = 0; // que transcurre tras el ultimo cambio
unsigned long tiempoTranscurrido = 0; // Desde que inicio arduino
unsigned long ultimoCambio = 0;

int posicion = 0;
int tiempo = 1000;
int tiempo2 = tiempo + 500;
Adafruit_NeoPixel tira = Adafruit_NeoPixel( 14, 9 , NEO_GBR + NEO_KHZ800);
int arrayRandom[]= {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12};

/*
int color1 = tira.Color(0, 0, 180);
int color2 = tira.Color(0, 129, 0);
int color3 = tira.Color(100, 100, 100);

int ColorT[]={color1, color2, color3};*/


int ColorT[]= { tira.Color(0, 0, 180), tira.Color(0, 129, 0), tira.Color(100, 100, 100)};

void RandonColor( int Numero){
  
 
        int posicionRandom = arrayRandom[random(12)];
        int ColorRandom = ColorT[random(0,2)];
 tiempoTranscurrido = millis();
   
    if (tiempoTranscurrido - tiempoCambio > tiempo2) {
     
        tira.setPixelColor(posicionRandom, ColorRandom );
        tira.show();
        tira.setPixelColor(posicionRandom, 0, 0, 0);
        

    tiempoCambio = tiempoTranscurrido;

   posicion++;
   if( posicion > Numero){
      posicion = 0;
   
    }  
}
}

void setup() {

  tira.begin();
  tira.show();
  tira.setBrightness(70);
  randomSeed(analogRead(0));
}

void loop() {


RandonColor(10);

}

Thank you for all the knowledge you can give me.

Hi,
Can you post your complete code so we can see how you have setup your variables, functions and libraries please?

Thanks.. Tom.. :slight_smile:

Hi Tom, I already updated the original post with all the variables and libraries. :slight_smile:

'arrayRandom' is not defined.

"random(0, 2)" (same as "random(2)") only returns 0 or 1 so the third element of ColorT is not used.

I forget to put the array, however the random position works well what does not work correctly is the attribution of the colors, when I write 0, 250, 0, it does not give me green, red if it works perfectly and blue doesn't work well. For example, in color3 which is 100, 100, 100, it should give white but it gives a type of yellow Thanks John

int arrayRandom[]= {0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12};
  int posicionRandom = arrayRandom[random(12)];

Why? Just use 'int posicionRandom = random(12);' to get random integers from 0 to 11.

NOTE: tiara.Color() returns a 32-bit value ('unsigned long' or 'uint32_t') and will NOT fit into an 'int'. Try changing all your 'color' variables to type 'Adafruit_NeoPixel::Color' instead of 'int'.

You have the correct posicionRandom = arrayRandom[random (11)]; I already corrected this detail.

On the other hand, I also used the variable type 'uint32_t 2' instead of 'int', but it still worked the same.

I dont understand so good how change the variables to type 'Adafruit_NeoPixel::Color' or you mean change int to Unit32_t2?

It already works, I was missing changing the array variable from type int to unit32_t, thanks John for the help. It was very easy, sorry, I'm a beginner.