FSM Library (playground)- how to use "current state"?

Hello,

i'd like to use the FSM Library.

http://playground.arduino.cc/Code/FiniteStateMachine

Everything is fine, but i don't understand this function:

State& getCurrentState()

"Returns the current state"

May someone give me an example so i can understand how to check the current state of a FSM?

Don't know how to write it down. :~

Greetz Chris

The definition of the function is:

State& getCurrentState()

This means that the function returns a reference to a previously defined object. So it hands you back a reference to one of the State objects you created. It's pretty much the same as a pointer, but it's safer because you have to initialize a reference, so you don't have to check for NULL pointers. For example:

State goingOnce = State(firstChance);
State goingTwice = State(secondChance);
State gone = State(tooLate);
FSM auctionMachine = FSM(goingOnce);

void loop() {
  if (digitalRead(BID_PIN) == HIGH) {
    State& currentState = auctionMachine.getCurrentState();      
    // currentState is now a reference to one of the three State objects I declared above
    // either goingOnce, goingTwice or gone

    if (currentState === gone) {        // I used exactly equal but you don't have to
      Serial.println("Too late!");
    }
  }
}

void firstChance(void) {
}

void secondChance(void) {
}

void tooLate(void) {
}

Thank you so much! Now it’s clear! :slight_smile:

Do you know other examples refering to this library?

I only found the example at the playground.

Greetz Chris

Glad it helped. No, sorry, I haven't seen examples of anyone else using it.

This doesn’t compile

Of course not. There is no setup() function, and no #include statements. http://snippets-r-us.com is down the road a ways.