State machine

Hey everyone,
can anyone help me with a method to implement FSM on the Arduino, I've got 4 push buttons that are placed serially.
When the buttons are pressed in the order

*)1,2,3,4 it should denote as forward.

*) 4,3,2,1 it should denote as backward.

I'm using the FSM library that is available on the Arduino playground site.

Thank you in advance.

You don't need the library: implement what I explained in your other post with booleans to denote the states, and ifs to control what happens when you read a particular button while in that state, ie what boolean variable you set to denote the state you go to.

I heard that implementing using FSM is more efficient. thank you

yashz123:
I heard that implementing using FSM is more efficient.

Not if you can't get it to work.

You could easily have implemented my suggestion in the meantime, using simple coding that you probably know already.

manor_royal:
Not if you can't get it to work.

Doesn't that just say it all for libraries :slight_smile:

...R

Will do that thank you.

Easier solution: Use a 4 byte long circular buffer to store the ID number of past button presses. Each time you press a button, overwrite the oldest entry with the new button number, then check if you have the ascending or descending sequence yet.

Doing this with a state machine is just going to be a pain in the ass.

Jiggy-Ninja:
Easier solution: Use a 4 byte long circular buffer

Doing this with a state machine is just going to be a pain in the ass.

You're probably right on both counts, and certainly I wouldn't want to do it my long-hand way for a zillion buttons.

BUT OP is very new to Arduino and programming it seems (see his/her other thread) and my way is at least do-able with basic coding of boolean variables and ifs. Right now I doubt if OP knows what a circular buffer is, let alone how to code one, and I say that with all due respect to him/her.

I tried it out with the buffer and it works fine.
Thank you all.
I'm new to the Arduino but got no problem with c, can python be used to code for the Arduino?
By the way is there a way to close the post.

yashz123:
I tried it out with the buffer and it works fine.
I'm new to the Arduino but got no problem with c,

Ok I was wrong about you being new to coding not just Arduino :wink:

Glad you got it going.

yashz123:
By the way is there a way to close the post.

Not as such, since threads remain open for further posting indefinitely. But you can add [solved] or whatever to the subject in the opening post.

And it's always nice if working code can be posted to help the next person with a problem hint hint.

No worries and thank you for taking your time to solve a problem I had, and by the way, your method to determine the timing at which a sensor triggered worked.

Just for the hell of it I did it as a state machine with bools and loads of ifs, so that only legitimate transitions are allowed, and each state has the opportunity to do something, in my case only show the state by leds.

Will you be able to send me the code for the state machine implementation?
Thank You.

jurs:
What do you want to create?

See #8: seems he sorted it out

Jiggy-Ninja:
Easier solution: Use a 4 byte long circular buffer to store the ID number of past button presses. Each time you press a button, overwrite the oldest entry with the new button number, then check if you have the ascending or descending sequence yet.

Further simplification: use a single byte, and shift the button numbers (2 bits) into it. Then you have two patterns for valid input, i.e. 0b00011011 for up and 0b11100100 for down.

DrDiettrich:
Further simplification: use a single byte, and shift the button numbers (2 bits) into it. Then you have two patterns for valid input, i.e. 0b00011011 for up and 0b11100100 for down.

While acceptable for this use, that optimization is heavily dependent on the fact that there are only 4 different buttons with a sequence length of 4. If either of those were increased, it wouldn't fit in a byte anymore.

The buffer width has to be adopted for every sequence length, even if an array is used.

I don't say that my suggestion should be used, I only wanted to point out a more sophisticated and economic solution. It may or may not fit a special task, but it may also be useful in very different cases.