Can I use integers in this way?

J-M-L:
that's why I'm suggesting a boolean :slight_smile:

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.

Aren't enums constant?

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() {}

you'll see in the console (set at 115200 bauds)

Size of enum = 2
Size of bool = 1

J-M-L:
With an enum, that memory will be an int (so 2 or 4 bytes depending on your arduino).

In C++11 you can specify the underlying type:

enum something : unsigned char { ... };

will take 1 byte.

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)

Thanks for the all the feedback on the sketch. I never expected to learn so much by asking just a single question!

This is a video of the project where the program was for: