Again passing argument to function

to ask for help again. It is not entire my sketch, because I'm workin with small pieces to build whole latter - like a Lego.
I have two RGB LED diodes, one is A another is B. In declaration section I have:

constexpr auto RED_A = 31;
constexpr auto GREEN_A = 33;
constexpr auto BLUE_A = 35;

constexpr auto RED_B = 30;
constexpr auto GREEN_B = 32;
constexpr auto BLUE_B = 34;

in setup() I have

pinMode(RED_A, OUTPUT);
    pinMode(GREEN_A, OUTPUT);
    pinMode(BLUE_A, OUTPUT);

    pinMode(RED_B, OUTPUT);
    pinMode(GREEN_B, OUTPUT);
    pinMode(BLUE_B, OUTPUT);

    digitalWrite(RED_A, HIGH);
    digitalWrite(GREEN_A, LOW);         
    digitalWrite(BLUE_A, HIGH);

    digitalWrite(RED_B, HIGH);
    digitalWrite(GREEN_B, LOW);         
    digitalWrite(BLUE_B, HIGH)

I created one function

void RGB_LED(char X, int R, int G, int B)
{
	if (X == 'A')
	{
		digitalWrite(RED_A, R);
		digitalWrite(GREEN_A, G);         // Green On - Timer is avaiable
		digitalWrite(BLUE_A, B);
	}

	if (X == 'B')
	{
		digitalWrite(RED_B, R);
		digitalWrite(GREEN_B, G);         // Green On - Timer is avaiable
		digitalWrite(BLUE_B, B);
	}

}

and I use it as

RGB_LED('A', 0, 0, 1);

Is it possible to simplified function void RGB_LED(char X, int R, int G, int B)
to eliminate ifs. I like to add more LEDs and like to have clearer this function.
So the question is how to pass char X directly to digitalWrite.

const byte LED_A_Pins[] = {31, 33, 35};
const byte LED_B_Pins[] = {30, 32, 34};

void RGB_LED(const byte pins[3], int R, int G, int B)
{
  digitalWrite(pins[0], R);
  digitalWrite(pins[1], G);         // Green On - Timer is available
  digitalWrite(pins[2], B);
}
1 Like

Thank you. I will try. Looks simply.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.