How to create array to store pin assignments?

Hello,

I would like to do pin assignments by using an array.
So I'm looking for a way to and store the pin names in the array.

Example:

Ordinary code would look like this to assign pin A1 to a certain actor:
...
#define actor2 A1; // actor 2 is connected to A1
digitalWrite(actor, HIGH);
...

I would like to use an array like this:
...
int nmbr_actors =8; // number of actors: 8
'string' actor_pin[nmbr_actors]; // an array containing the pin for each actor, identified by the actor number
....
actor_pin[2] = A1; // assigning pin A1 to actor 2
...
act_actor = 2; // actual actor to control is actor 2
digitalWrite(actor_pin[act_actor], HIGH);
...

Thank you for any help,

Volker

consider

byte pinsOut [] = { 10, 11 };

#define N_OUT   sizeof(pinsOut)

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (115200);
    for (unsigned n = 0; n < N_OUT; n++)  {
        pinMode (pinsOut [n], OUTPUT);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    delay (500);
    digitalWrite (pinsOut [0], ! digitalRead (pinsOut [0]));
    delay (500);
    digitalWrite (pinsOut [1], ! digitalRead (pinsOut [1]));
}

That helps, thanks a lot!

Volker

The analog pin names, A0, A1, etc, are actually referring to numbers, so you would not treat them as strings.

In the case of A1, the following code is found in the pins_arduino.h file for the UNO/Nano:

#define PIN_A1   (15) // this defines PIN_A1 as the number 15

static const uint8_t A1 = PIN_A1; // this creates a constant named A1 and initializes it with the value of PIN_A1, which is 15

It may appear a bit convoluted, but the reason for doing it this way is that analog port A1 does not have the same numeric value on all Arduino boards.

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