Enum Resource for Arduino Author: Alexander Brevig Contact: alexanderbrevig@gmail.com
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
enum ternary { unknown, true, false };
ternary variable = unknown; if (variable==unknown){ //do code variable = (condition ? true : false); }
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.
Part of AlphaBeta Resources. | |
Last Modified: | March 09, 2010, at 03:08 AM |
By: | AlphaBeta |