Make a list of readable byte commands?

So I want to make a list of byte commands and avoid confusion by making them readable as this seems like good coding practise. So I can make an array and simply reference the value by the array index. eg

byte someCommands[10] = {0x01, 0x02, 0x03, 0x04, 0x05};
myCommand = someCommands[2];

but is there a way to reference lists of byte commands in a better way. eg

apples = 0x01;
oranges = 0x02;
pears = 0x03;
bananas = 0x04;
plums = 0x05;

So using words as references to bytes, where you can use those words in comparator operations, eg

if(myCommand = apples)
{}

An enum seems ideally suited to what you are trying to do.

So would it be just,

enum myEnum
{
 apples = 0x01;
 oranges = 0x02;
 pears = 0x03;
 bananas = 0x04;
 plums = 0x05;
};

currentState myState = apples;

if(myCommand == myState/apples?)

So i want a direct comparision to apples. So that its readible?

You completely missed the point of an enum... you don't want to specify values. Those are generated automatically.

aarg: You completely missed the point of an enum... you don't want to specify values. Those are generated automatically.

There are plenty of cases where the programmer wants to assign values. The default numbering starts at 0, and increments by 1. If that is not what you want, then you need to assign at least one value. Assigning all of them is OK.

So i want a direct comparision to apples. So that its readible?

The first part is a statement that I can agree with. The second is a question that only you can answer.

if(myCommand == myState/apples?)

The myState/ part is wrong. The compiler knows that apples is a member of an enum, and even knows which enum it is a member of. The ? is obviously wrong.

PaulS: There are plenty of cases where the programmer wants to assign values. The default numbering starts at 0, and increments by 1. If that is not what you want, then you need to assign at least one value. Assigning all of them is OK.

For the "fruit" example, the whole point seems to be that you would not need to give them values. I'm trying to convey the beauty of this philosophy.

For the “fruit” example, the whole point seems to be that you would not need to give them values. I’m trying to convey the beauty of this philosophy.

Then perhaps coming at it from a somewhat different direction (not the “hey, idiot” direction; I’m using that lane) might be better.

Using a pure number hardly makes something more readable, which is why they are suggesting an enum. For example, which is easier to read: Code with pure numbers in the switch or:

enum myEnum
{
 apples = 0x01,
 oranges = 0x02,
 pears = 0x03,
 bananas = 0x04,
 plums = 0x05
};


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Enter a number between 1 and 5:");
}

void loop() {
  // put your main code here, to run repeatedly:
  int choice;
  
  if (Serial.available() > 0) {
    choice = Serial.read() - '0';
    switch (choice) {
      case apples:
        Serial.println("Apples");
        break;

      case oranges:
        Serial.println("oranges");
        break;

      case pears:
        Serial.println("pears");
        break;

      case bananas:
        Serial.println("bananas");
        break;

      case plums:
        Serial.println("plums");
        break;

      default:
        Serial.println("You can't follow directions.");
        break;    
    }
    
  }
}

Using the enumerations in the case expressions makes it easier to understand the switch states.

Indeed, and the first Google hit I got for enum explained things quite well. But I thought that referencing that would be a kind of "hey, idiot" approach...

For example, which is easier to read: Code with pure numbers in the switch or:

Sorry, that's a lousy example. If the user enters 3, he/she rather expects it to match case 3, not case pears.

Where the enum makes the most sense is when the names in an enum match names the user sees, such as in a menu. The user selected (the value that corresponds to) pears in the menu, and then expects to see it match case pears:, not case 3:.

Sorry, that's a lousy example.

First, I know you well enough to know you're not sorry. Second, instead of bitching at someone who is trying to help, write a better example. Third, I was simply trying to illustrate how an enum can be used.

Second, instead of bitching at someone who is trying to help, write a better example.

The places where I use enums don't lend themselves to extracting as snippets. The context would make no sense.

A menu example is the simplest thing I can think of, and that doesn't lend itself to a short sequence of code, either.

The places where I use enums don't lend themselves to extracting as snippets. The context would make no sense.

A menu example is the simplest thing I can think of, and that doesn't lend itself to a short sequence of code, either.

Then I would say: Quit bitching unless you have something better to add.