Adding an int to the word "led"

Hello everyone!

I am doing something with a for loop and have several pins labeled led1, led2, led3 etc.

What I want to do to is to have a digitalWrite command inside a for loop so that I can have

digitalWrite(ledx, HIGH);

where x is the a number I have given in the for loop. So that if I gave the number 3, x would be 1,2,3 in sequence. This will then turn on led1, 2, and 3. The best way I can think to do this is to add the number x to the word led so that I get the name of my pin ie. "led1" or "led2". How can I do this?

Sorry if this is unclear, I've found this really hard to describe for some reason!

Thanks very much

Use arrays -

#define ARRAY_ENTRIES_COUNT(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinLED_1    = 3;
const uint8_t   pinLED_2    = 4;
const uint8_t   pinLED_3    = 5;

const uint8_t   arrayLEDS[] = { pinLED_1, pinLED_2, pinLED_3 };

void loop()
{
    ... some code ...

    for ( size_t i = 0; i < ARRAY_ENTRIES_COUNT(arrayLEDS); i++ )
    {
        digitalWrite(arrayLEDS[i], HIGH);
    }
    
    ... some code ...
}

As soon as you find yourself numbering variables (such as led1, led2, led3, ...), it is time to learn about arrays. lloyddean has shown one good way to use an array.

Thanks guys, I'll check out arrays :slight_smile: