Struct member names

Hi folks, I am trying to make a data structure of booleans to hold the status of all the IO pins. First time using struct as well as fairly new to pointers etc, so a little help to "point" me in the right direction is very much appriciated.
Is there a way to access a member of the data structure using a variable? I have 30 pins and would like them in a structure where they can be accessed when a request comes in over UPD (a whole other problem :slight_smile: )
Please forgive my terrible coding! and shout at me for doing something daft...

struct pinInfo {
  bool relay1;
  bool relay2;
  bool relay3;
}ioPins;

char *pinNames[3] = {"relay1", "relay2", "relay3"};

void setup() {
  Serial.begin(9600);
  ioPins.relay1 = false;
  ioPins.relay2 = true;
  ioPins.relay3 = false;
  
  for (int i = 0; i <= 2; i++) {
    Serial.println(ioPins.pinNames[i]);   //compiler doesn't like this
  }
 
}

I thought that the pinNames array would come in handy when responing to a UDP request allowing me to send a string pin name and its status.

Thanks!

make one structure and than use an array:

struct  Pin{
  byte gpio;
  byte state;
  char *name;
};

Pin ioPin[] {
 {2, false, "relay1"},
 {3, false, "relay2"},
 {10, false, "relay3"},
};

void setup() {
  Serial.begin(9600);
  ioPin[1].state = false;

  
  for (int i = 0; i <= 2; i++) {
    Serial.println(ioPin[i].name);   //compiler should like this
  }
 
}

void loop() {
  // put your main code here, to run repeatedly:

}

further more, don't hard code the number of your elements (e.g. in the for loop).
Read about the range based for loop

  for (auto &i : ioPin)  // poor man's for each in c++ ;-)
  {
    Serial.println(i.name);
  }

Your 'pinNames' array is not a member of the struct so the compiler doesn't like it when you act like it is. You can make it a member:

struct pinInfo {
  bool relay1;
  bool relay2;
  bool relay3;
  char *pinNames[3] = {"relay1", "relay2", "relay3"};
} ioPins;

Or you can use it as it is:
Serial.println(pinNames[i]);

consider

struct  Relay{
    byte        pin;;
    const char *name;
    byte        state;
};

Relay relay [] {
    {  2, "relay1" },
    {  3, "relay2" },
    { 10, "relay3" },
};
const size_t Nrelay = sizeof(relay)/sizeof(Relay);

enum { RelayOn = LOW, RelayOff = HIGH };

// -------------------------------------
void
relaySet (
    Relay   *r,
    int      on )
{
    digitalWrite (r->pin, r->state = on);
    Serial.print ("relaySet: ");
    Serial.print (RelayOn == r->state ? "On " : "Off ");
    Serial.println (r->name);
}

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin(9600);

    Relay *r = relay;
    for (size_t i = 0; i < Nrelay; i++, r++)
        relaySet (r, RelayOff);
}

// -----------------------------------------------------------------------------
void loop() {
}

Thank you! this is exactly what I needed, getting to understand how things work now, thankyou.

Ok, I understand, thank you!

Nice one, thank you, that helps!

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