Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Enum Resource for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com


Navigation


Description

In computer programming, an enumerated type is a data type (usually user-defined) consisting of a set of named constants called enumerators. The act of creating an enumerated type defines an enumeration. When an identifier such as a variable is declared having an enumerated type, the variable can be assigned any of the enumerators as a value. Wikipedia

It enables the programmer to create a variable that can be a selected set of values.

For instance, it might be useful to have a ternary variable with the three possible states/values: unknown, true, false


Declaration

enum ternary {
  unknown,
  true,
  false
};


Creation and usage

ternary variable = unknown;

if (variable==unknown){
  //do code
  variable = (condition ? true : false);
}


FAQ

How can I use enums as function returntypes or as parameters to functions?

The usual arduino workaround/hack is to have all functions that requires custom datatstructures to be placed in an additional .h file. Just create a new tab in the IDE and give it a name.h then #include "name.h"

ternary isReady(); //returns unknown, true or false

void doCriticalAction(ternary t); //could check to see if t == true, then do the critical action. If t != true, return.


Links


Information about this page

Part of AlphaBeta Resources.
Last Modified: March 09, 2010, at 03:08 AM
By: AlphaBeta