Fun with Enums

I was messing around with a project that scroll through several LCD displays, getting frustrated that I had to constantly update the field of contents of an enum when I wanted to debug a certain display output.

I worked out this little sort-of-scalable thingy I thought I would share:

enum MyEnum{
  FIRST,
  SECOND,
  /*THIRD,*/
  FOURTH,
  FIFTH,
  END_OF_OPTIONS
};

MyEnum myEnum = static_cast<MyEnum>(END_OF_OPTIONS - 1);

void setup()
{
  Serial.begin(9600);
  Serial.println();
}

void loop()
{
  int mode = static_cast<int>(myEnum);
  myEnum = static_cast<MyEnum>(++mode % END_OF_OPTIONS);
  switch (myEnum)
  {
    case FIRST:
      Serial.println();
      Serial.println("first");
      break;
    case SECOND:
      Serial.println("second");
      break;
/*    case THIRD:
      Serial.println("third");
      break; */
    case FOURTH:
      Serial.println("fourth");
      break;
    case FIFTH:
      Serial.println("fifth");
      break;
  }
  delay(1000);
}

maybe someone else will find it useful in a similar project.

thanks - that's a smart idea

myEnum = static_cast<MyEnum>(++mode >= END_OF_OPTIONS ? 0 : mode);

…will generate less code / be much faster.

Thanks. I was positive someone could improve it!