Array not working as intended?

int redGroup[] = {2, 5, 8, 14, 17};
int greenGroup[] = {3, 6, 9, 15, 18};
int blueGroup[] = {4, 7, 10, 16, 19};

int ledCount = 5;

char val[] = "o";


void setup() {

  for (int i = 0; i < ledCount; i++)
  {
    pinMode(redGroup[i], OUTPUT);
    pinMode(greenGroup[i], OUTPUT);
    pinMode(blueGroup[i], OUTPUT);

    digitalWrite(redGroup[i], 0);
    digitalWrite(greenGroup[i], 0);
    digitalWrite(blueGroup[i], 0);
  }

  Serial.begin(9600);


}

void loop() {


  if (Serial.available() > 0)
  {
    Serial.flush();
    
    for (int i = 0; i < Serial.available(); i++)
    {
      val[i] = Serial.read();
    }
    Serial.write(val);
  }



  if (val[0] == 'o') {
    for (int i = 0; i < ledCount; i++)
    {
      digitalWrite(redGroup[i], 0);
      digitalWrite(greenGroup[i], 0);
      digitalWrite(blueGroup[i], 0);
    }

  }


  else if (val[0] == 'r')
  {
    if(val[1] != 'a')
    {
      digitalWrite(redGroup[val[1]], 1);
    }
    else
    {
      for (int i = 0; i < ledCount; i++)
      {
        digitalWrite(redGroup[i], 1);
      }
    }
    

  }

  else if (val[0] == 'g')
  {
    for (int i = 0; i < ledCount; i++)
    {
      digitalWrite(greenGroup[i], 1);
    }
  }

  else if (val[0] == 'b')
  {
    for (int i = 0; i < ledCount; i++)
    {
      digitalWrite(blueGroup[i], 1);
    }
  }

  else if (val[0] == 'w') {
    for (int i = 0; i < ledCount; i++)
    {
      digitalWrite(redGroup[i], 1);
      digitalWrite(greenGroup[i], 1);
      digitalWrite(blueGroup[i], 1);
    }

  }

  


}

You can see the setup from the picture, sorry about all the wires. But you can see each group of 5 RGB LEDs. I intend to be able to control each groups colour individually or all together. The problem is if I input, for example, “r2” group 2 should light up the Red LEDs, but only the first group lights up. Even if I input “ra” all of the groups of LEDs should light up red but they aren’t

Is there something wrong with my code that I’m missing?

You can see the setup from the picture,

Wrong, I can see very little.

What I am failing to see is very many current limiting resistors. Each RGB LED needs three resistors. You can not use one resistor with one RGB LED.

Hi AppleAssasin,

You can use one resistor for each rgb led for the moment while you test and fix your current code/wiring problem. One resistor will work ok if you only only want either red, green or blue (although they may not be equal brightness). When you come to try mixing other colours like yellow, cyan, magenta or white that you will see why Mike is right and have to use 3 resistors per rgb led (and the resistors on the reds will probably need to be a little higher than those on the greens & blues). But cross that bridge when you come to it.

I can see a problem with your sketch that may explain why "r2" does not work. When you put "redGroup [val [1]]" the problem is that the character "2" does not have the value 2. Its value is the ASCII code for the character "2". C/C++ will not do this conversion for you implicity like other languages do. You need to do it yourself. Simplest way is to subtract the ASCII code for "0", for example "redGroup [val [1]-"0"]".

But that doesn't explain why "ra" does not work. Have you tested your wiring? Take each jumper lead, in turn, out of its Arduino digital output and touch it to +5V to check that a led lights up.

Paul

Instead of spending hours trying to fix this, I've opted to just give each colour and group a number, i.e val == '1' is redGroup[0] and val=='2' is redGroup[1] etc.

It isn't the most elegant solution, but it does make it work