I am using LEDs that have two pins, and two colours, red and green, depending on polarity.
I have used these to indicate power on = red, connection to MQTT = green.
I now want to use 5 of these LEDs. The function I have for one LED is:
#define PIN_LED_GREEN A0 // MQTT connected
#define PIN_LED_RED A1 // Power ON
void led_power_mqtt (uint8_t led_state)
{
switch (led_state)
{
case 1:
// red on
digitalWrite (PIN_LED_GREEN, LOW);
digitalWrite (PIN_LED_RED, HIGH);
break;
case 2:
// green on
digitalWrite (PIN_LED_RED, LOW);
digitalWrite (PIN_LED_GREEN, HIGH);
break;
case 0:
default:
// off
digitalWrite (PIN_LED_RED, LOW);
digitalWrite (PIN_LED_GREEN, LOW);
break;
}
}
I'd like to get to a function call like this:
led_red_green_off (led_name, [0..2])
(That is function name, pass in the LED identifier, and provide a state)
However, it seems there is no way of building variable names, hence, I seemingly need to have a function for each LED.
This could be a case for a multi-dimensional array, but I am not sure how to apply it.
Thinking out loud, I could write the 5 red/green in a single function and pass in the LED identifier and state... but it is still code smell.
Any pointers appreciated.