Go Down

Topic: State Machines (Read 1 time) previous topic - next topic

danieljay

So I am trying to put together a state machine, and am running into some difficultly.

I have formatted my program like so:


Code: [Select]
public enum states { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

states state = new states;


switch (state){

case STATE_1 :
// if this does that, do something
}
state = STATE_2;
break;

case STATE_2:
// if this does that, do something
}
state = STATE_3;
break;
//etc etc


but I am receiving an error saying "cannot find anything named "state". What am I missing?

Cheers!

Nick Gammon

This compiles:

Code: [Select]

enum states { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };

states state;

void setup ()
  {
 
  }  // end of setup

void loop ()
  {
  switch (state)
    {

    case STATE_1 :
      // if this does that, do something
 
      state = STATE_2;
      break;
 
    case STATE_2:
      // if this does that, do something
 
      state = STATE_3;
      break;
    }  // end of switch

  }  // end of loop

danieljay

EEk well I feel ridiculous. This question was actually intended for the Processing forum. I posted it there, but the problem lies in implementing this in Processing.

PaulS

Code: [Select]
public enum states { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, MAX_STATES };
Well, now, it's quite clear what each of those states means. Not.

danieljay

Hey PaulS,

I was going to transition to each case depending on specific arguments. What exactly do you mean by,
Quote
Well, now, it's quite clear what each of those states means. Not.
.

Was just curious.

Nick Gammon

I imagine PaulS means to name the states in a meaningful way, like:

Code: [Select]
BUTTON_PRESSED, DOOR_OPENED, BUTTON_RELEASED, DOOR_CLOSED, GOAT_ESCAPED


That sort of thing. Calling them STATE_1, STATE_2 etc. is a bit like calling variables: variable1, variable2, etc. which is pretty useless.

danieljay

Thanks for the clarification, guys.

GoForSmoke

All a state machine is is a loop that has some/all parts that run on the value of a variable.

The rest is in how you do it though keep in mind that any task has a least amount of work to accomplish, it's funny how many people not-think/supposed-to a whole lot of extra steps to do the same job.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

johncc


All a state machine is is a loop that has some/all parts that run on the value of a variable.

The rest is in how you do it though keep in mind that any task has a least amount of work to accomplish, it's funny how many people not-think/supposed-to a whole lot of extra steps to do the same job.




Sorry, this doesn't compile:

post 136723.msg1078711: syntax error at " people not-think/supposed-to a whole lot of extra steps"

Please post the code that you're actually using to make this point  :smiley-roll:

GoForSmoke

Not-thinking is for example what you just did.
Supposed-to is like how I'm supposed to hand you code.

You don't have to use switch-case in a state machine nor does state have to be limited to a single action. I'm not being much more specific than your original post which is very general, just giving you tips. Your tip-compiler seems to be turned off or powered down.

You want to find code examples, Google arduino state machine. Maybe add nick gammon to the search words, he's gifted at making explanations.
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up