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?

    Serial.flush();

Block until all pending serial data has been sent. Since you haven’t sent any, please explain how this is useful.

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

Just how big is val?

    if(val[1] != 'a')

That would have thrown an array out of bounds exception, if the Arduino had exceptions.

I expected each character I input through serial would be put into an array, is this not the case? for example if I put "r2" it would mean val = { "r", 2};

If that's not how it works, how do I make it do that?

expected each character I input through serial would be put into an array, is this not the case? for example if I put "r2" it would mean val = { "r", 2};

It would, IF val was big enough. How big IS val?

It's your code. You're supposed to know.

Also keep in mind that Serial data transmission is ssslllooowww. The Arduino can read it orders of magnitude faster than it arrives. You need some way to tell the Arduino to read and store data until the end of the packet arrives, and then use that packet.

the biggest I need val is 2 bytes, would adding a delay after the first read make a difference?

So something like

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

would adding a delay after the first read make a difference?

No. That is not the correct approach.

If your commands are always 2 characters, don't read anything until there are two characters to read.

If your commands are variable length, add an end of packet marker (the CR, LF or CR and LF that the serial monitor adds automatically make fine end of packet markers).

So how would I go about doing it?

So how would I go about doing it?

Properly.

Doing what?

When you define char val = "o"; it creates an array with a single element. And I believe it should be 'o' with single quotes.

You need an array with 2 elements if you need to receive 2 characters char val[2] = {'o', 'o'};

The when serial.available >= 2 you need to do serial.read() twice once to put the first char in val[0] and once to put the second char in val[1].

Then your tests for what is in val will also need to be changed.

Unfortunately Serial.flush() does not do what a normal person would think it should do, and you don't need it.

...R