Lot easier to follow words in the code vs just numbers.
"I'm in State 3 - what's that one doing?"
vs
"I'm in Standby - now I'm waiting for an input to kick me into Moving."
Using enums, you can restrict the application to use only the defined values, which have default numbers assigned, based on the position in the enum. Far better than name = value substitution at compile time.
One way, use appropriate names for your application:
//define the available machine states for this sketch
//add more states as needed
enum States{
stateStart, stateOne, stateTwo, stateThree, stateFour, stateFive};
//mState is "machine State"
States mState = stateStart; //we start out in this machine state
Okay, I don’t know if this is going to help in any way at all, but here’s my main (Quadruped Robot) header file:
//Onyx library creates classes to represent the various functions and systems of the Onyx Robotic Quadruped.
//The current CPU is an arduino-like Chipkit Uno32
#ifndef _Onyx_H
#define _Onyx_H
#if (ARDUINO >= 100)
# include <Arduino.h>
#endif
//-Extra Includes-
#include "SSC32.h"
//Onyx Definitions
#define baudrate 115200 //Symbols per second. Default hardware set 115200
#define sscReturn 0 //Is SSC32 Connected bi-directionally? (default "0" = no)
//Physical Dimensions //all in floating point millimetres accurate to the thousandth
#define Trochanter_X 61.40
#define Trochanter_Y 4.34 //CAD Model Incorrect!!
#define Femur_X 24.29
#define Femur_Y 50.78
#define Femur 56.29 //The L1 Value
#define Tibia 41.02
#define Tarsus 62.00 //Tibia + Tarsus = L2 Value
#define Body_Width 139.72
#define Body_Length 106.25
//----------------------------------------------------------------------------
//Servo-Power Relay Pin
#define relayServos 27
//Leg Indicator LED Pins
#define iFL 35
#define iFR 34
#define iRR 8
#define iRL 9
//-------------------------------------------------------------------------------------------------------------
//On Off Toggle References
#define ON 1
#define OFF 0
#define HIGH 1
#define LOW 0
#define TOGGLE 2
//runtime modes
#define REBOOT 0
#define LOWPOWER 1
#define STARTUP 2
#define STANDBY 3
#define STOPPED 4
#define MOVING 5
#define DEBUG 6
#define VIRTUAL 7
#define SHUTDOWN 8
#define ERROR 9
//move types
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define STAND 4
#define pi 3.14159;
class Onyx {
private:
int _mode;
int _ServoList[12];
void startup();
public:
Onyx();
void setMode(int mode); //sets runtime state
void listen();
void reconnectSSC(); //reinitialize SSC instance communication
void newMove(int type);
void move();
};
class Power {
private:
int Main_Relay;
bool _powerMain; //Power State (ON,OFF)
public:
Power(int Main_Relay);
void setMainPin(int pin);
void ToggleMain(int state);
};
class Servo {
private:
int _pin;
bool _isExternal;
bool _isGroup; //Is the servo part of a group?
int _groupNumber;
public:
Servo(int pin);
void move(int pulse);
void setGroup(int groupNumber);
};
class Leg {
private:
float _Trochanter_X;
float _Trochanter_Y;
float _Femur_X;
float _Femur_Y;
float _Femur;
float _L1;
float _Tibia;
float _Tarsus;
float _L2;
int _quadrant;
bool _isGroup;
public:
Leg(int index);
};
#endif
My real problem is that I’ve only just recently gotten into programming, so I’m sitting here, bashing my keyboard and typing things trying to get the damn program to compile every once in awhile.
I’m not quite sure I understand how I would how to substitute enums for my current state tracking method.
Is state tracking really just a preference thing? I mean, it makes sense to me as it stands, I just couldn't describe it to someone. It's my personal style.
However, if I'm doing something INCORRECTLY (not preference based) then I want to know.
I've reviewed a couple different libraries now, they seem to do something (almost exactly) like I'm doing.
I am a great believer in meaningful names for everything, and short functions with a single purpose.
I suspect a large number of states (even if suitably named) will be confusing unless the code in which they are used is very carefully organized. Life will be simpler if any part of the code only needs to deal with a few states.
If your code is doing what you want, there really is no reason to change anything. If it isn't, please describe what it is actually doing, and how that differs from what you want.
SilentDemon555:
Dammit, no one here said #define = enums!!!
I know this is the generic "Google it" answer, but jesus thank you!
Edit: I know that enums != defines, but generally they're the same concept.
This was the answer I wanted
You are correct , #define != enums!!!
How you propose to do similar code (attached) using enum since by definitions enums are NOT numbers / integers and without knowing their position in enum assignment you do not automatically know the value anyway.