Pages: [1]   Go Down
Author Topic: State Machines  (Read 839 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I have formatted my program like so:


Code:
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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This compiles:

Code:
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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46296
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 126
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the clarification, guys.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Examples can be found in your IDE.

Pages: [1]   Go Up
Jump to: