pointer to Class instance.

I'm using the FiniteStateMachine library http://playground.arduino.cc/code/FiniteStateMachine#.Uy4kzvldWSo for a project. In a nutshell, you instantiate a State class for each of your states and each instance has up to three funtcions (enter, update, exit) from which it can transition to another state. You can transition to a new state, but there's no functionality to return to the previous state without knowing where you came from.

I thought I could implement this by having a pointer called previousStatePtr which pointed to the previous State instance. When transitioning to a new state, instead of giving the new state as an argument, you would give it a pointer which had been set to the previous state. Perhaps something like this:

State powerOnState = State(powerOnEnter,powerOnUpdate,NULL); // Declare some State instances
State mainMenuState = State(mainMenuEnter,mainMenuUpdate,mainMenuExit);
State someState = State(someEnter,someUpdate,someExit);
FiniteStateMachine stateMachine = FiniteStateMachine(powerOnState);
void processStateMachine() { // Call this from loop() to update  state machine.
  stateMachine.update();
}

// Try to create a pointer which can point to a State instance
State * previousStatePtr = &powerOnState; // This compiles
// but this doesn't
State * previousStatePtr; // Create a pointer which points to a State object
previousStatePtr = &powerOnState;  // Specify which object's address to point to.
//error: expected constructor, destructor, or type conversion before '=' token

Right now I have code like this further down

void powerOnEnter(){   // do something };
void powerOnUpdate(){
  if ( digitalRead(11) ) stateMachine.transitionTo(someState);
  else stateMachine.transitionTo(mainMenuState);
void mainMenuEnter(){ // do something};
void mainMenuUpdate{
  if ( digitalRead(11) ) stateMachine.transitionTo(someState);
};

I'd love to be able to do this:

void someEnter(){ }
void  someUpdate() {
    if ( digitalRead(11) ) stateMachine.transitionTo(previosuStatePtr); // Go back to previous state without having to know what it was
}
void someExit(){ // This executes before we transition to the next state.
  previousStatePtr  = &someState;
}

As a completely separate issue, I'd love to be able to pass data between states rather than rely on globals, but C++ isn't a language I've used much.

If you want to keep it in the form of a finite state machine you should split "someState" into "someMenuState" and "somePowerOnState" That way the state knows what state to transition to.

void someMenuEnter(){ }
void  someMenuUpdate() {
    if ( digitalRead(11) ) stateMachine.transitionTo(mainMenuState); // Go back to previous state
}
void someMenuExit(){}

void somePowerOnEnter(){ }
void  somePowerOnUpdate() {
    if ( digitalRead(11) ) stateMachine.transitionTo(powerOnState); // Go back to previous state
}
void somePowerOnExit(){}

I don't think this would be practical in my case. The number of permutations are too high. For example, I've got a state that is used to take data entry from the user. Once done it needs to return to one of many states which could have called it. I'd like to avoid having 30 different data entry states which differ only by where they go when done. If I could just have a pointer to the next state I think it would work.

Maybe you should implement the state machine without the library. All you need is a state variable and a large case statement. Then the state that transitions to the input state can set a ReturnState variable that the input state can use to return to the right state.

@RyanN

Would it be okay if you uploaded all your code ? Im attempting to create a state machine using the FSM library. My code set up is very similar to yours, but the state machine isn’t working correctly, and im hoping by looking at yours ill notice where ive gone wrong.

Thanks