Questions about enum's, arrays and const's

Hi, I'm pretty new with the Arduino and I got a few questions.

My first one is why can't I do that:

enum Params
{
  char Name[16],
  int switchPin,
  int ledPin
};

const Buttons[][Params]
{
  { "btnLivingRoom", 2, 8 },
  { "btnBathRoom", 3, 9 },
  { "btnBedRoom", 4, 10 },
  { "btnKitchen", 5, 11 },
  { "btnParking", 6, 12 }
};

void setup() { ... }

void loop() { ... }

And what the difference between:

new const [] {...}
// Or
const [] {...}

Please explain carefuly :smiley_cat:
Thanks in advance.

My first one is why can't I do that:

Because that is NOT what an enum is for. That looks more like a struct.

const [][Params]
{

So, now you want a variable, of type undefined, with no name, that is constant and a 2D array. No way, Jose.

And what the difference between

One is a steaming pile of stinky crap and the other is a stinky pile of steaming crap.

PaulS:
Because that is NOT what an enum is for. That looks more like a struct.

const [][Params]

{



So, now you want a variable, of type undefined, with no name, that is constant and a 2D array. No way, Jose.
One is a steaming pile of stinky crap and the other is a stinky pile of steaming crap.

Well, I'm not sure if I do understand.
Can you show me please the right way to do that (if possible)?

Can you show me please the right way to do that (if possible)?

To do what?

A variable declaration needs a type, first and foremost.

A variable declaration needs a name.

There can then be modifiers like const, volatile, etc.

So, an array of ints, for instance, looks like:

  int coords[8][8];

You can't use Params as a size. That makes no sense.

You can't make an array of Params, because the definition of Params is all wrong.

Firstly, let me say Im no expert (unless you require expertise in Ale, or mobile comms). However, if it were me, I’d be looking more towards a code like this;

enum Rooms : byte {Bathroom, Dungeon, Shed, Bedroom}; // create the labels

struct Room { // Structure to hold the room entities and the data associated with them
  byte roomEnum; //uses the labels you declared in the enum. pretty sure you could do this as a pointer....
  byte buttonPIN; // the digital pin for the button for that room.
  byte ledPIN; // the digital pin that has the led connected for that room
} ;

// declare each room structure, note the different case on first letter. Cant re-use the exact same label.
Room bathroom = { Bedroom, 2, 6};
Room bedroom = { Bathroom, 3, 7};
Room dungeon = { Dungeon, 4, 8};
Room shed = {Shed, 5, 9};

Now you can refer to each element within the structure by using eg dungeon.buttonPIN or dungeon.ledPIN

ps. if you do have an actual dungeon, we’d like to see pictures :slight_smile:

PaulS:
To do what?

A variable declaration needs a type, first and foremost.

A variable declaration needs a name.

There can then be modifiers like const, volatile, etc.

So, an array of ints, for instance, looks like:

  int coords[8][8];

You can't use Params as a size. That makes no sense.

You can't make an array of Params, because the definition of Params is all wrong.

Thanks, that helps me alot but I'm still not sure how to continue from here.
Maybe I didn't explained myself good.

scrumfled:
Firstly, let me say Im no expert (unless you require expertise in Ale, or mobile comms). However, if it were me, I'd be looking more towards a code like this;

enum Rooms : byte {Bathroom, Dungeon, Shed, Bedroom}; // create the labels

struct Room { // Structure to hold the room entities and the data associated with them
 byte roomEnum; //uses the labels you declared in the enum. pretty sure you could do this as a pointer....
 byte buttonPIN; // the digital pin for the button for that room.
 byte ledPIN; // the digital pin that has the led connected for that room
} ;

// declare each room structure, note the different case on first letter. Cant re-use the exact same label.
Room bathroom = { Bedroom, 2, 6};
Room bedroom = { Bathroom, 3, 7};
Room dungeon = { Dungeon, 4, 8};
Room shed = {Shed, 5, 9};




Now you can refer to each element within the structure by using eg dungeon.buttonPIN or dungeon.ledPIN

ps. if you do have an actual dungeon, we'd like to see pictures :)

That's great, It's pretty much what I meant to do, but what if I want to loop through all the ledPINs to set them as OUTPUTS (just to be efficient)?

Oh and about the dungeon, I don't have it yet :smiley:

In the spirit of aiding understanding, try doing a serial print of eachroomname.roomEnum. You'll get an understanding of how the compiler allocates numbering in an enum (unless you over-ride it) and it'll be obvious how to do it.

ps. I can assure you my understanding is basic, Im sure others here could build a far more elegant code.

Recommend you look into arrays, structures, C++ classes, class constructors with initialization lists.

A lLOT to learn all at once, it is ...

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

class widgetT
{
    const uint8_t   _pinSWITCH;
    const uint8_t   _pinLED;
    char            _psz[16];

    widgetT();
    widgetT(int);

public:
    // 'widgetT' constructor
    widgetT(uint8_t pinSwitch, uint8_t pinLED, const char* pszName)
        : _pinSWITCH(pinSwitch)
        , _pinLED(pinLED)
    {
        strcpy(_psz, pszName);
    }

    void begin()
    {
        pinMode(_pinSWITCH, INPUT);
        pinMode(_pinLED, OUTPUT);
    }
};

widgetT widgets[] =
{
      widgetT(2,  8, "LivingRoom")
    , widgetT(3,  9, "BathRoom")
    , widgetT(4, 10, "BedRoom")
    , widgetT(5, 11, "Kitchen")
    , widgetT(6, 12, "Parking")
};

void loop()
{   }

void setup()
{
    for ( size_t i = NUM_ENTRIES(widgets); i--; )
    {
        widgets[i].begin();
    }
}

Warning: thread wandering about to occur!

Firstly, thanks for chiming in, its nice to see those with better knowledge adding to the discussions rather than sniping :slight_smile:

Some questions from me for Lloyd ( or any of the many other gurus);

  1. can you recommend a good resource that highlights the pros/cons of struct/array/class.

  2. Comparing the class method vs the struct I used (which i accept is very basic).

Struct: 444 (storage), 9 (global vars)
Class : 676, 143

The main difference I can see between the two methods is that this implementation of class method is retaining the human readable label. Are there other benefits?

lloyddean:
Recommend you look into arrays, structures, C++ classes, class constructors with initialization lists.

A lLOT to learn all at once, it is ...

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

class widgetT
{
   const uint8_t   _pinSWITCH;
   const uint8_t   _pinLED;
   char            _psz[16];

widgetT();
   widgetT(int);

public:
   // 'widgetT' constructor
   widgetT(uint8_t pinSwitch, uint8_t pinLED, const char* pszName)
       : _pinSWITCH(pinSwitch)
       , _pinLED(pinLED)
   {
       strcpy(_psz, pszName);
   }

void begin()
   {
       pinMode(_pinSWITCH, INPUT);
       pinMode(_pinLED, OUTPUT);
   }
};

widgetT widgets =
{
     widgetT(2,  8, "LivingRoom")
   , widgetT(3,  9, "BathRoom")
   , widgetT(4, 10, "BedRoom")
   , widgetT(5, 11, "Kitchen")
   , widgetT(6, 12, "Parking")
};

void loop()
{   }

void setup()
{
   for ( size_t i = NUM_ENTRIES(widgets); i--; )
   {
       widgets[i].begin();
   }
}

Sorry for the delay, I'm really appreciate your help!
Well, you was right, that was a lot to learn from a bunch lines of code :smiley:

Thank you, it solved my problem.