Randomly choosing an array

Hi!

I'm relatively new to programming so this may be a stupid question but alas im here and stuck.

I have 12 Arrays[3] which contain RGB values for different colours.

Example:

red[3] = {255,0,0}; orange[3] = {255,128,0}; and so on

I want to assign a random colour from my colour pool to my RGB Led strip that i control via PWM.

Example:

analogWrite(ledPinR, randomcolour[0]); analogWrite(ledPinG, randomcolour[1]); analogWrite(ledPinB, randomcolour[2]);

I tried to do this with the switch case function but it doesn't return arrays apparently :/

How can i efficiently solve this problem?

I tried to do this with the switch case function but it doesn't return arrays apparently

But you couldn't be bothered to post what your tried?

How can i efficiently solve this problem?

What problem?

What problem?

This problem:

I want to assign a random colour from my colour pool to my RGB Led strip that i control via PWM.

But you couldn’t be bothered to post what your tried?

My attempt:

switch (random(12))
      {
        case '0':
        {
        int colour[3] = {255,0,0}; //red
        }
        break;
        case '1':
        {
        int colour[3] = {255,128,0}; //orange
        }
        break;
        case '2':
        {
        int colour[3] = {255,255,0}; //yellow
        }
        break;
        case '3':
        {
        int colour[3] = {128,255,0}; //lime
        }
        break;
        case '4':
        {
        int colour[3] = {0,255,0}; //green
        }
        break;
        case '5':
        {
        int colour[3] = {0,255,128}; //lightgreen
        }
        break;
        case '6':
        {
        int colour[3] = {0,255,255}; //turquoise
        }
        break;
        case '7':
        {
        int colour[3] = {0,128,255}; //blue
        }
        break;
        case '8':
        {
        int colour[3] = {0,0,255}; //darkblue
        }
        break;
        case '9':
        {
        int colour[3] = {127,0,255}; //violet
        }
        break;
        case 10:
        {
        int colour[3] = {255,0,255}; //pink
        }
        break;
        case 11:
        {
        int colour[3] = {255,0,127}; //rose
        }
        break;
      }
      
      analogWrite(ledPinR, colour[0]);
      analogWrite(ledPinG, colour[1]);
      analogWrite(ledPinB, colour[2]);

The whole code before anyone asks:

int analogPin = 0; // MSGEQ7 OUT
int strobePin = 2; // MSGEQ7 STROBE
int resetPin = 4; // MSGEQ7 RESET
int spectrumValue[7];
int colour[3];
 
// MSGEQ7 OUT pin produces values around 50-80
// when there is no input, so use this value to
// filter out a lot of the chaff.
int filterValue = 85;
int k = 10; //Brightness
 
// LED pins connected to the PWM pins on the Arduino

int ledPinG = 9;
int ledPinR = 10;
int ledPinB = 11;
 
void setup()
{
  //Serial.begin(9600);

  pinMode(17, INPUT);
  pinMode(16, OUTPUT);
  digitalWrite(16, LOW);
  digitalWrite(17, HIGH);
  
  // Read from MSGEQ7 OUT
  pinMode(analogPin, INPUT);
  
  // Write to MSGEQ7 STROBE and RESET
  pinMode(strobePin, OUTPUT);
  pinMode(resetPin, OUTPUT);

  // Create an initial state for our pins
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, LOW);
  delay        (1);
 
  // Reset the MSGEQ7 as per the datasheet timing diagram
  digitalWrite (resetPin,  HIGH);
  delay        (1);
  digitalWrite (resetPin,  LOW);
  digitalWrite (strobePin, HIGH); 
  delay        (1);
 
  // Set analogPin's reference voltage
  analogReference(DEFAULT); // 5V
}
 
void loop()
{
  int swtch = digitalRead(17);

  // Set reset pin low to enable strobe
  digitalWrite(resetPin, HIGH);
  digitalWrite(resetPin, LOW);
 
  // Get all 7 spectrum values from the MSGEQ7
  for (int i = 0; i < 7; i++)
  {
    digitalWrite(strobePin, LOW);
    delayMicroseconds(30); // Allow output to settle
 
    spectrumValue[i] = analogRead(analogPin);
 
    // Constrain any value above 1023 or below filterValue
    spectrumValue[i] = constrain(spectrumValue[i], filterValue, 1023);

    // Remap the value to a number between 0 and 255
    spectrumValue[i] = map(spectrumValue[i], filterValue, 1023, 0, 255);

    // Remove serial stuff after debugging
    //Serial.print(spectrumValue[i]);
    //Serial.print(" ");
    digitalWrite(strobePin, HIGH);
   }
 
   if (swtch == HIGH)
   {
   // Write the PWM values to the LEDs
   // I find that with three LEDs, these three spectrum values work the best
   analogWrite(ledPinR, spectrumValue[0]*k);
   analogWrite(ledPinR, spectrumValue[1]*k); 
   
   //analogWrite(ledPinG, spectrumValue[3]*k);
   analogWrite(ledPinG, spectrumValue[4]*k); 
  
   analogWrite(ledPinB, spectrumValue[5]*k);
   analogWrite(ledPinB, spectrumValue[6]*k);
   }
   
   else if (spectrumValue[0]>=30)
   {
      switch (random(12))
      {
        case '0':
        {
        int colour[3] = {255,0,0}; //red
        }
        break;
        case '1':
        {
        int colour[3] = {255,128,0}; //orange
        }
        break;
        case '2':
        {
        int colour[3] = {255,255,0}; //yellow
        }
        break;
        case '3':
        {
        int colour[3] = {128,255,0}; //lime
        }
        break;
        case '4':
        {
        int colour[3] = {0,255,0}; //green
        }
        break;
        case '5':
        {
        int colour[3] = {0,255,128}; //lightgreen
        }
        break;
        case '6':
        {
        int colour[3] = {0,255,255}; //turquoise
        }
        break;
        case '7':
        {
        int colour[3] = {0,128,255}; //blue
        }
        break;
        case '8':
        {
        int colour[3] = {0,0,255}; //darkblue
        }
        break;
        case '9':
        {
        int colour[3] = {127,0,255}; //violet
        }
        break;
        case 10:
        {
        int colour[3] = {255,0,255}; //pink
        }
        break;
        case 11:
        {
        int colour[3] = {255,0,127}; //rose
        }
        break;
      }
      
      analogWrite(ledPinR, colour[0]);
      analogWrite(ledPinG, colour[1]);
      analogWrite(ledPinB, colour[2]);
   }
}
switch (random(12))
      {
        case '0':

random(12) doesn't return ASCII characters. Try plain zero instead.

Then you've got the very limited scope of "colour" to resolve.

I'd just form a large 2D array of colours, and index that with your random number

Use a two-imensional array.

// all your colors; only showing four but you can expand 'indefinitely'
byte colors[][3] =
{
  {255, 0, 0}, // red
  {255, 128, 0}, // orange
  {0, 0, 255}, // blue
  {0, 255, 0}, // green
};


void setup()
{
  Serial.begin(115200);
}

void loop()
{
  // pick a random number between 0 and the number of colors in the two-dimensional array
  int num = random(0, sizeof(colors) / 3);

  Serial.print("Index "); Serial.println(num);
  Serial.print("R "); Serial.println(colors[num][0]);
  Serial.print("G "); Serial.println(colors[num][1]);
  Serial.print("B "); Serial.println(colors[num][2]);


  delay(1000);
}

It should be self-explaining, else ask.

random(12) doesn't return ASCII characters. Try plain zero instead.

Thanks, i missed that!

Use a two-imensional array.

Looks like it really was a stupid question. Thank you very much!

With regards to reply #2. The variable colour that you declare inside the cases is only known inside the cases. So the variable colour that you use outside the switch is another variable. The only way to solve that is to use the analogWrite statements inside each case.

Not a stupid question for somebody engaged in learning. There are several ways to solve this. Two more that come to my still-newboid mind are: String all of them together in one long array, and offset you index by multiples of 3 to access each color. Or (probably simpler) use a struct: http://www.cplusplus.com/doc/tutorial/structures/ Speaking as somebody who's still on the lower slopes of the learning curve, I like to see alternate solutions to things.

best, Michael

String all of them together in one long array, and offset you index by multiples of 3 to access each color.

That's why we have 2D arrays, so you don't have to do the arithmetic.

Or (probably simpler) use a struct:

Better still, an array of struct.

So the variable colour that you use outside the switch is another variable.

Yeah i noticed that after my first try. :sweat_smile:

Or (probably simpler) use a struct: http://www.cplusplus.com/doc/tutorial/structures/

That's helpful, thanks! Completely forgot about those.

As a follow-up, here is my solution:

// Beat/Bass visualisation
   else if (spectrumValue[0]>=30)
   {
      byte colour[12][3] =
      {
        {255,0,0},    // red
        {255,128,0},  // orange
        {255,255,0},  // yellow
        {128,255,0},  // lime
        {0,255,0},    // green
        {0,255,128},  // lightgreen
        {0,255,255},  // turquoise
        {0,128,255},  // blue
        {0,0,255},    // darkblue
        {127,0,255},  // violet
        {255,0,255},  // pink
        {255,0,127},  // rose
      };
      
      int r = random(12);
      
      analogWrite(ledPinR, colour[r][0]);
      analogWrite(ledPinG, colour[r][1]);
      analogWrite(ledPinB, colour[r][2]);
   }

It looks and works WAY better than just some random RGB colours. My next problem now, is to figure out a way to implement a better beat detection than a simple threshold value.