Tag

enum

Browsing

Flag variables are great, and totally not evil, when you just have two states: ON or OFF. What about when you have multiple states? Is there an option better than creating multiple flag variables?

The C-language has a declaration type just for this purpose. It is called an enumeration, or enum.

Setting up a state machine with enum is a surprisingly simple. Arduino and embedded programmers should use them!

All you need to do is create descriptive tag names, and let the compiler assign them an integer value. Unlike a #define which is just a macro replacement, the compiler treats an enum as your personal variable type.

This behavior comes in handy when you’re creating states for a state machine. I show how to create a simple state machine with enum, to blink an LED with millis(), in this post.