every byte of RAM counts on small microprocessors and on a 8 bit processor asking the CPU to handle 16 or 32 bits operations is slower than 8 bit operations... so when you can stick to bytes and when things are constant, tell the compiler, it will use that to optimize code too.
The literals in the enum themselves indeed are handled at compile time but you need a memory space to store your variable state. With an enum, that memory will be an int (so 2 or 4 bytes depending on your arduino).
if you run that little program on a UNO
enum motorState { ON, OFF } stepperState = OFF;
boolean isMotorOn = false;
void setup() {
Serial.begin(115200);
Serial.print(F("Size of enum = ")); Serial.println(sizeof(stepperState));
Serial.print(F("Size of bool = ")); Serial.println(sizeof(isMotorOn));
}
void loop() {}
oqibidipo:
In C++11 you can specify the underlying type
good point!
enum MotorStatus_t : byte { ON, OFF } stepperState = OFF; // or enum : byte { ON, OFF } stepperState = OFF;
boolean isMotorOn = false;
void setup() {
Serial.begin(115200);
Serial.print(F("Size of enum = ")); Serial.println(sizeof(stepperState));
Serial.print(F("Size of bool = ")); Serial.println(sizeof(isMotorOn));
}
void loop() {}
will print
[color=blue]Size of enum = 1
Size of bool = 1
[/color]
so that's an OK option too this way - makes code more verbose
if ([color=blue]stepperState == ON)[/color] {...} else {...}
versus
if ([color=blue]motorIsOn[/color]) {...} else {...}
but both are very readable (and if you need more states for your motor, like "broken", "moving", "atHome" etc for a state machine then it becomes a good option)