Offline
Full Member
Karma: 0
Posts: 107
|
 |
« on: December 10, 2012, 12:04:03 am » |
So I am trying to put together a state machine, and am running into some difficultly. I have formatted my program like so: 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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: December 10, 2012, 12:08:26 am » |
This compiles: 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
Full Member
Karma: 0
Posts: 107
|
 |
« Reply #2 on: December 10, 2012, 12:13:40 am » |
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
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #3 on: December 10, 2012, 05:12:03 am » |
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
Full Member
Karma: 0
Posts: 107
|
 |
« Reply #4 on: January 19, 2013, 12:22:48 am » |
Hey PaulS, I was going to transition to each case depending on specific arguments. What exactly do you mean by, Well, now, it's quite clear what each of those states means. Not. . Was just curious.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: January 19, 2013, 02:56:34 am » |
I imagine PaulS means to name the states in a meaningful way, like: 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
Full Member
Karma: 0
Posts: 107
|
 |
« Reply #6 on: January 19, 2013, 10:11:16 am » |
Thanks for the clarification, guys.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #7 on: January 19, 2013, 10:24:51 am » |
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 at Learning in the Main Site and at the Playground
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #8 on: January 19, 2013, 12:42:29 pm » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #9 on: January 19, 2013, 08:00:23 pm » |
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 at Learning in the Main Site and at the Playground
|
|
|
|
|